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
fe81c47b
Commit
fe81c47b
authored
7 years ago
by
Jan Kuchař
Browse files
Options
Downloads
Patches
Plain Diff
moved logic into autoloaded class (performance)
parent
fd789ca5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/SignatureAssertionUtil.php
+112
-0
112 additions, 0 deletions
src/SignatureAssertionUtil.php
src/exceptions.php
+40
-0
40 additions, 0 deletions
src/exceptions.php
src/functions.php
+9
-66
9 additions, 66 deletions
src/functions.php
with
161 additions
and
66 deletions
src/SignatureAssertionUtil.php
0 → 100644
+
112
−
0
View file @
fe81c47b
<?php
declare
(
strict_types
=
1
);
/**
* This file is part of assert-function-signature.
*/
namespace
Grifart\AssertFunction
;
final
class
SignatureAssertionUtil
{
private
function
__construct
()
{
}
public
static
function
checkSignature
(
callable
$function
,
array
$parameters
,
?string
$expectedReturnType
):
void
{
$reflection
=
new
\ReflectionFunction
(
$function
);
// NUMBER OF PARAMETERS CHECK:
$numberOfParameters
=
$reflection
->
getNumberOfParameters
();
if
(
$numberOfParameters
!==
count
(
$parameters
))
{
throw
FunctionAssertionError
::
wrongNumberOrArguments
(
$reflection
,
count
(
$parameters
),
$numberOfParameters
);
}
// PARAMETER TYPES CHECK:
$i
=
0
;
/** @var string[] $parameters */
foreach
(
$parameters
AS
$parameter
)
{
assert
(
is_string
(
$parameter
));
$parameterReflection
=
$reflection
->
getParameters
()[
$i
++
];
assert
(
$parameterReflection
instanceof
\ReflectionParameter
);
self
::
checkFunctionParameter
(
$reflection
,
$parameterReflection
,
...
self
::
parseType
(
$parameter
)
);
}
// RETURN TYPE:
if
(
$expectedReturnType
!==
NULL
)
{
if
(
!
$reflection
->
hasReturnType
())
{
throw
FunctionAssertionError
::
missingReturnType
(
$reflection
,
$expectedReturnType
);
}
if
(
$reflection
->
hasReturnType
())
{
list
(
$expectedReturnType
,
$expectingNullable
)
=
self
::
parseType
(
$expectedReturnType
);
$returnTypeReflection
=
$reflection
->
getReturnType
();
$actuallyNullable
=
$returnTypeReflection
->
allowsNull
();
if
(
$expectingNullable
!==
$actuallyNullable
)
{
throw
FunctionAssertionError
::
wrongReturnTypeNullability
(
$reflection
,
$expectingNullable
,
$actuallyNullable
);
}
if
(
$expectedReturnType
!==
(
string
)
$reflection
->
getReturnType
())
{
throw
FunctionAssertionError
::
wrongReturnType
(
$reflection
,
$expectedReturnType
,
(
string
)
$reflection
->
getReturnType
()
);
}
}
}
}
private
static
function
parseType
(
string
$type
):
array
{
$cleanType
=
$type
;
// e.g. ?Namespace\Class
$optional
=
FALSE
;
if
(
$type
[
0
]
===
'?'
)
{
$optional
=
TRUE
;
$cleanType
=
substr
(
$type
,
1
);
// without leading '?'
}
return
[
$cleanType
,
$optional
];
}
private
static
function
checkFunctionParameter
(
\ReflectionFunction
$functionReflection
,
\ReflectionParameter
$parameterReflection
,
string
$expectedParameterType
,
bool
$expectedOptional
)
:
void
{
$reflectionType
=
$parameterReflection
->
getType
();
assert
(
$reflectionType
instanceof
\ReflectionType
);
// checks:
$actuallyOptional
=
$reflectionType
->
allowsNull
();
if
(
$expectedOptional
!==
$actuallyOptional
)
{
throw
FunctionAssertionError
::
parameterNullability
(
$functionReflection
,
$parameterReflection
,
$expectedOptional
,
$actuallyOptional
);
}
if
(
$expectedParameterType
!==
(
string
)
$reflectionType
)
{
throw
FunctionAssertionError
::
wrongParameterType
(
$functionReflection
,
$parameterReflection
,
$expectedParameterType
,
$reflectionType
);
}
}
}
This diff is collapsed.
Click to expand it.
src/exceptions.php
+
40
−
0
View file @
fe81c47b
...
...
@@ -48,4 +48,44 @@ final class FunctionAssertionError extends \AssertionError {
return
implode
(
'/'
,
$pathParts
);
// normalized shortened readable path
}
public
static
function
parameterNullability
(
\ReflectionFunction
$functionReflection
,
\ReflectionParameter
$param
,
bool
$expectedOptional
,
bool
$actuallyOptional
)
:
self
{
return
new
self
(
$functionReflection
,
$expectedOptional
?
"Function should have parameter
{
$param
->
getName
()
}
optional but it is not."
:
"Function should have parameter
{
$param
->
getName
()
}
required but it is not."
);
}
public
static
function
wrongParameterType
(
\ReflectionFunction
$functionReflection
,
\ReflectionParameter
$parameterReflection
,
string
$expectedParameterType
,
\ReflectionType
$actualParameterType
)
:
self
{
return
new
self
(
$functionReflection
,
"Wrong parameter type given in function
{
$parameterReflection
}
.
{
$expectedParameterType
}
expected.
{
$actualParameterType
}
defined in function."
);
}
public
static
function
wrongReturnTypeNullability
(
\ReflectionFunction
$reflectionFunction
,
bool
$expectingNullable
,
bool
$actuallyNullable
)
:
self
{
return
new
self
(
$reflectionFunction
,
$expectingNullable
?
'Wrong return type nullability. Expecting nullable, but was required.'
:
'Wrong return type nullability. Expecting required, but was nullable.'
);
}
public
static
function
wrongReturnType
(
\ReflectionFunction
$reflection
,
string
$expectedReturnType
,
string
$actualReturnType
)
:
self
{
return
new
self
(
$reflection
,
"Expected return type of type '
$expectedReturnType
', but function declares '
$actualReturnType
'"
);
}
};
This diff is collapsed.
Click to expand it.
src/functions.php
+
9
−
66
View file @
fe81c47b
...
...
@@ -3,9 +3,7 @@
* Used to load functions.
*/
// todo: use only assert( ... ) instead of exceptions; to be compatible without configuration with PHP env
// todo: [performance] separate code itself into autoloaded class
// todo: better assertion messages + throw FunctionAssertionError + catch in fn + pass it to assert -> compatible with PHP assert config
// todo: TESTS: better assertion messages + throw FunctionAssertionError + catch in fn + pass it to assert -> compatible with PHP assert config
// todo: covariance and contra-variance
namespace
Grifart\AssertFunction
;
...
...
@@ -22,71 +20,16 @@ function nullable(string $classType): string
function
assertSignature
(
callable
$function
,
array
$parameters
,
?string
$expectedReturnType
):
void
{
// todo: call assert(__impl());
$reflection
=
new
\ReflectionFunction
(
$function
);
// NUMBER OF PARAMETERS CHECK:
$numberOfParameters
=
$reflection
->
getNumberOfParameters
();
assert
(
$numberOfParameters
===
count
(
$parameters
),
FunctionAssertionError
::
wrongNumberOrArguments
(
$reflection
,
count
(
$parameters
),
$numberOfParameters
)
);
if
(
$numberOfParameters
!==
count
(
$parameters
))
{
return
;
}
// PARAMETER TYPES CHECK:
$i
=
0
;
/** @var string[] $parameters */
foreach
(
$parameters
AS
$parameter
)
{
assert
(
is_string
(
$parameter
));
$parameterReflection
=
$reflection
->
getParameters
()[
$i
++
];
assert
(
$parameterReflection
instanceof
\ReflectionParameter
);
__checkFunctionParameter
(
$parameterReflection
,
...
__parseType
(
$parameter
));
// todo: move to helper class
}
// RETURN TYPE:
if
(
$expectedReturnType
!==
NULL
)
{
assert
(
$reflection
->
hasReturnType
(),
FunctionAssertionError
::
missingReturnType
(
$reflection
,
$expectedReturnType
)
try
{
SignatureAssertionUtil
::
checkSignature
(
$function
,
$parameters
,
$expectedReturnType
);
if
(
$reflection
->
hasReturnType
())
{
list
(
$_returnType
,
$_optional
)
=
__parseType
(
$expectedReturnType
);
$returnTypeReflection
=
$reflection
->
getReturnType
();
assert
(
$_optional
===
$returnTypeReflection
->
allowsNull
());
assert
(
$_returnType
===
(
string
)
$reflection
->
getReturnType
());
}
}
}
function
__parseType
(
string
$type
):
array
{
$cleanType
=
$type
;
// e.g. ?Namespace\Class
$optional
=
FALSE
;
if
(
$type
[
0
]
===
'?'
)
{
$optional
=
TRUE
;
$cleanType
=
substr
(
$type
,
1
);
// without leading '?'
}
catch
(
FunctionAssertionError
$error
)
{
// todo: add callee position
assert
(
FALSE
,
$error
);
}
return
[
$cleanType
,
$optional
];
}
function
__checkFunctionParameter
(
\ReflectionParameter
$parameterReflection
,
string
$parameterType
,
bool
$optional
)
:
void
{
$reflectionType
=
$parameterReflection
->
getType
();
assert
(
$reflectionType
instanceof
\ReflectionType
);
// checks:
assert
(
$optional
===
$reflectionType
->
allowsNull
());
assert
(
$parameterType
===
(
string
)
$reflectionType
);
}
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