Conditions | 12 |
Paths | 91 |
Total Lines | 78 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
22 | public static function assertSignatureMatches(callable $expected, callable $actual, string $callablePropName): bool |
||
23 | { |
||
24 | if (!self::performAsserts()) { |
||
25 | return true; |
||
26 | } |
||
27 | $expectedReflection = self::getReflection($expected); |
||
28 | $actualReflection = self::getReflection($actual); |
||
29 | $messageBuilder = function (string $messagePrefix = '') use ($callablePropName, $expectedReflection, $actualReflection): string { |
||
30 | return sprintf( |
||
31 | '%s%s should be a callable with signature %s, however callable with signature %s provided', |
||
32 | empty($messagePrefix) ? $messagePrefix : trim($messagePrefix) . ' ', |
||
33 | $callablePropName, |
||
34 | self::getSignatureFromReflection($expectedReflection), |
||
35 | self::getSignatureFromReflection($actualReflection) |
||
36 | ); |
||
37 | }; |
||
38 | assert( |
||
39 | $expectedReflection->getNumberOfRequiredParameters() === $actualReflection->getNumberOfRequiredParameters(), |
||
40 | $messageBuilder('Incorrect Parameter Count') |
||
41 | ); |
||
42 | if ($expectedReflection->hasReturnType()) { |
||
43 | assert( |
||
44 | $actualReflection->hasReturnType(), |
||
45 | $messageBuilder('Missing Return Type') |
||
46 | ); |
||
47 | //TODO: improve this to check that the actual type does not return a childType; |
||
48 | $expectedReturnType = $expectedReflection->getReturnType(); |
||
49 | $actualReturnType = $actualReflection->getReturnType(); |
||
50 | $name = $expectedReturnType instanceof ReflectionNamedType ? |
||
51 | $expectedReturnType->getName() : |
||
52 | strval($expectedReturnType); |
||
53 | $actName = $actualReturnType instanceof ReflectionNamedType ? |
||
54 | $actualReturnType->getName() : |
||
55 | strval($actualReturnType); |
||
56 | |||
57 | assert( |
||
58 | $name === $actName, |
||
59 | $messageBuilder('IncorrectOrInvalid ReturnType') |
||
60 | ); |
||
61 | if (!$expectedReturnType->allowsNull()) { |
||
62 | assert( |
||
63 | !$actualReturnType->allowsNull(), |
||
64 | $messageBuilder('Nullable ReturnType Not allowed') |
||
65 | ); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | for ($i = 0; $i < $expectedReflection->getNumberOfParameters(); $i++) { |
||
70 | $expectedParm = $expectedReflection->getParameters()[$i]; |
||
71 | if ($expectedParm->hasType()) { |
||
72 | $actualParm = $actualReflection->getParameters()[$i]; |
||
73 | assert( |
||
74 | $actualParm->hasType(), |
||
75 | $messageBuilder(sprintf('Parameter %s Is missing TypeHint', $i)) |
||
76 | ); |
||
77 | $expectedParmType = $expectedParm->getType(); |
||
78 | $actualParmType = $actualParm->getType(); |
||
79 | $name = $expectedParmType instanceof ReflectionNamedType ? |
||
80 | $expectedParmType->getName() : |
||
81 | strval($expectedParmType); |
||
82 | $actName = $actualParmType instanceof ReflectionNamedType ? |
||
83 | $actualParmType->getName() : |
||
84 | strval($actualParmType); |
||
85 | |||
86 | //TODO: improve this to check that the actual type does not return a childType; |
||
87 | assert( |
||
88 | $name === $actName, |
||
89 | $messageBuilder(sprintf('Parameter %s has Incorrect Type', $i)) |
||
90 | ); |
||
91 | if (!$expectedParm->allowsNull()) { |
||
92 | assert( |
||
93 | !$actualParm->allowsNull(), |
||
94 | $messageBuilder(sprintf('Parameter %s should disallow Nulls', $i)) |
||
95 | ); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | return true; |
||
100 | } |
||
146 |