Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
assert-function-signature
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jan Kuchař
assert-function-signature
Commits
a62d0be1
Commit
a62d0be1
authored
7 years ago
by
Jan Kuchař
Browse files
Options
Downloads
Patches
Plain Diff
finished rename optional -> nullable
parent
980bcfc4
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/SignatureAssertionUtil.php
+8
-8
8 additions, 8 deletions
src/SignatureAssertionUtil.php
src/exceptions.php
+3
-3
3 additions, 3 deletions
src/exceptions.php
tests/fn.assertSignature.allNullable.phpt
+5
-5
5 additions, 5 deletions
tests/fn.assertSignature.allNullable.phpt
with
16 additions
and
16 deletions
src/SignatureAssertionUtil.php
+
8
−
8
View file @
a62d0be1
...
...
@@ -71,32 +71,32 @@ final class SignatureAssertionUtil
private
static
function
parseType
(
string
$type
):
array
{
$cleanType
=
$type
;
// e.g. ?Namespace\Class
$
optional
=
FALSE
;
$
nullable
=
FALSE
;
if
(
$type
[
0
]
===
'?'
)
{
$
optional
=
TRUE
;
$
nullable
=
TRUE
;
$cleanType
=
substr
(
$type
,
1
);
// without leading '?'
}
return
[
$cleanType
,
$
optional
];
return
[
$cleanType
,
$
nullable
];
}
private
static
function
checkFunctionParameter
(
\ReflectionFunction
$functionReflection
,
\ReflectionParameter
$parameterReflection
,
string
$expectedParameterType
,
bool
$expected
Optional
bool
$expected
Nullable
)
:
void
{
$reflectionType
=
$parameterReflection
->
getType
();
assert
(
$reflectionType
instanceof
\ReflectionType
);
// checks:
$actually
Optional
=
$reflectionType
->
allowsNull
();
if
(
$expected
Optional
!==
$actually
Optional
)
{
$actually
Nullable
=
$reflectionType
->
allowsNull
();
if
(
$expected
Nullable
!==
$actually
Nullable
)
{
throw
FunctionSignatureAssertionError
::
parameterNullability
(
$functionReflection
,
$parameterReflection
,
$expected
Optional
,
$actually
Optional
$expected
Nullable
,
$actually
Nullable
);
}
if
(
$expectedParameterType
!==
(
string
)
$reflectionType
)
{
...
...
This diff is collapsed.
Click to expand it.
src/exceptions.php
+
3
−
3
View file @
a62d0be1
...
...
@@ -50,12 +50,12 @@ final class FunctionSignatureAssertionError extends \AssertionError {
}
public
static
function
parameterNullability
(
\ReflectionFunction
$functionReflection
,
\ReflectionParameter
$param
,
bool
$expected
Optional
,
bool
$actually
Optional
)
:
self
public
static
function
parameterNullability
(
\ReflectionFunction
$functionReflection
,
\ReflectionParameter
$param
,
bool
$expected
Nullable
,
bool
$actually
Nullable
)
:
self
{
return
new
self
(
$functionReflection
,
$expected
Optional
?
"Function should have parameter
{
$param
->
getName
()
}
optional
but it is not."
$expected
Nullable
?
"Function should have parameter
{
$param
->
getName
()
}
nullable
but it is not."
:
"Function should have parameter
{
$param
->
getName
()
}
required but it is not."
);
}
...
...
This diff is collapsed.
Click to expand it.
tests/fn.assertSignature.all
Optional
.phpt
→
tests/fn.assertSignature.all
Nullable
.phpt
+
5
−
5
View file @
a62d0be1
...
...
@@ -8,20 +8,20 @@ use Tester\Assert;
$fn
=
function
(
?T1
$t1
,
?T2
$t2
):
?T3
{
return
new
T3
;};
assertSignature
(
$fn
,
params
(
nullable
(
T1
::
class
),
nullable
(
T2
::
class
)),
nullable
(
T3
::
class
));
//
missing optional check
Assert
::
exception
(
function
()
use
(
$fn
)
{
//
parameter is nullable, non-nullable is expected
Assert
::
exception
(
function
()
use
(
$fn
)
{
// 1st param
assertSignature
(
$fn
,
params
(
T1
::
class
,
nullable
(
T2
::
class
)),
nullable
(
T3
::
class
));
},
\AssertionError
::
class
/* todo: assert message */
);
Assert
::
exception
(
function
()
use
(
$fn
)
{
Assert
::
exception
(
function
()
use
(
$fn
)
{
// 2nd param
assertSignature
(
$fn
,
params
(
nullable
(
T1
::
class
),
T2
::
class
),
nullable
(
T3
::
class
));
},
\AssertionError
::
class
/* todo: assert message */
);
Assert
::
exception
(
function
()
use
(
$fn
)
{
Assert
::
exception
(
function
()
use
(
$fn
)
{
// return type
assertSignature
(
$fn
,
params
(
nullable
(
T1
::
class
),
nullable
(
T2
::
class
)),
T3
::
class
);
},
\AssertionError
::
class
/* todo: assert message */
);
// Wrong return type
type
// Wrong return type
Assert
::
exception
(
function
()
use
(
$fn
)
{
assertSignature
(
$fn
,
params
(
nullable
(
T1
::
class
),
nullable
(
T2
::
class
)),
nullable
(
T2
::
class
));
},
\AssertionError
::
class
/* todo: assert message */
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment