| Conditions | 1 |
| Paths | 1 |
| Total Lines | 107 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 50 | public function functionsToBeTested() : array |
||
| 51 | { |
||
| 52 | $astLocator = (new BetterReflection())->astLocator(); |
||
| 53 | |||
| 54 | $fromLocator = new StringSourceLocator( |
||
| 55 | <<<'PHP' |
||
| 56 | <?php |
||
| 57 | |||
| 58 | namespace { |
||
| 59 | function parametersIncreased($a, $b, $c) {} |
||
| 60 | function parametersReduced($a, $b, $c) {} |
||
| 61 | function parameterNamesChanged($a, $b, $c) {} |
||
| 62 | function optionalParameterAdded($a, $b, $c) {} |
||
| 63 | function noParametersToOneParameter() {} |
||
| 64 | function variadicParameterAdded($a, $b) {} |
||
| 65 | function variadicParameterMoved($a, ...$b) {} |
||
| 66 | function optionalParameterAddedInBetween($a, $b, $c) {} |
||
| 67 | function parameterMadeOptionalMidSignature($a, $b, $c) {} |
||
| 68 | function untouched($a, $b, $c) {} |
||
| 69 | } |
||
| 70 | |||
| 71 | namespace N1 { |
||
| 72 | class C { |
||
| 73 | static function changed1($a, $b, $c) {} |
||
| 74 | function changed2($a, $b, $c) {} |
||
| 75 | } |
||
| 76 | } |
||
| 77 | PHP |
||
| 78 | , |
||
| 79 | $astLocator |
||
| 80 | ); |
||
| 81 | |||
| 82 | $toLocator = new StringSourceLocator( |
||
| 83 | <<<'PHP' |
||
| 84 | <?php |
||
| 85 | |||
| 86 | namespace { |
||
| 87 | function parametersIncreased($a, $b, $c, $d) {} |
||
| 88 | function parametersReduced($a, $b) {} |
||
| 89 | function parameterNamesChanged($d, $e, $f) {} |
||
| 90 | function optionalParameterAdded($a, $b, $c, $d = null) {} |
||
| 91 | function noParametersToOneParameter($a) {} |
||
| 92 | function variadicParameterAdded($a, $b, ...$c) {} |
||
| 93 | function variadicParameterMoved($a, $b, ...$b) {} |
||
| 94 | function optionalParameterAddedInBetween($a, $b = null, $c, $d) {} |
||
| 95 | function parameterMadeOptionalMidSignature($a, $b = null, $c) {} |
||
| 96 | function untouched($a, $b, $c) {} |
||
| 97 | } |
||
| 98 | |||
| 99 | namespace N1 { |
||
| 100 | class C { |
||
| 101 | static function changed1($a, $b, $c, $d) {} |
||
| 102 | function changed2($a, $b, $c, $d) {} |
||
| 103 | } |
||
| 104 | } |
||
| 105 | PHP |
||
| 106 | , |
||
| 107 | $astLocator |
||
| 108 | ); |
||
| 109 | |||
| 110 | $fromClassReflector = new ClassReflector($fromLocator); |
||
| 111 | $toClassReflector = new ClassReflector($toLocator); |
||
| 112 | $fromReflector = new FunctionReflector($fromLocator, $fromClassReflector); |
||
| 113 | $toReflector = new FunctionReflector($toLocator, $toClassReflector); |
||
| 114 | |||
| 115 | $functions = [ |
||
| 116 | 'parametersIncreased' => ['[BC] CHANGED: The number of required arguments for parametersIncreased() increased from 3 to 4'], |
||
| 117 | 'parametersReduced' => [], |
||
| 118 | 'parameterNamesChanged' => [], |
||
| 119 | 'optionalParameterAdded' => [], |
||
| 120 | 'noParametersToOneParameter' => ['[BC] CHANGED: The number of required arguments for noParametersToOneParameter() increased from 0 to 1'], |
||
| 121 | 'variadicParameterAdded' => [], |
||
| 122 | 'variadicParameterMoved' => ['[BC] CHANGED: The number of required arguments for variadicParameterMoved() increased from 1 to 2'], |
||
| 123 | 'optionalParameterAddedInBetween' => ['[BC] CHANGED: The number of required arguments for optionalParameterAddedInBetween() increased from 3 to 4'], |
||
| 124 | 'parameterMadeOptionalMidSignature' => [], |
||
| 125 | 'untouched' => [], |
||
| 126 | ]; |
||
| 127 | |||
| 128 | return array_merge( |
||
| 129 | array_combine( |
||
|
|
|||
| 130 | array_keys($functions), |
||
| 131 | array_map( |
||
| 132 | /** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */ |
||
| 133 | function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array { |
||
| 134 | return [ |
||
| 135 | $fromReflector->reflect($function), |
||
| 136 | $toReflector->reflect($function), |
||
| 137 | $errorMessages, |
||
| 138 | ]; |
||
| 139 | }, |
||
| 140 | array_keys($functions), |
||
| 141 | $functions |
||
| 142 | ) |
||
| 143 | ), |
||
| 144 | [ |
||
| 145 | 'N1\C::changed1' => [ |
||
| 146 | $fromClassReflector->reflect('N1\C')->getMethod('changed1'), |
||
| 147 | $toClassReflector->reflect('N1\C')->getMethod('changed1'), |
||
| 148 | [ |
||
| 149 | '[BC] CHANGED: The number of required arguments for N1\C::changed1() increased from 3 to 4', |
||
| 150 | ], |
||
| 151 | ], |
||
| 152 | 'N1\C#changed2' => [ |
||
| 153 | $fromClassReflector->reflect('N1\C')->getMethod('changed2'), |
||
| 154 | $toClassReflector->reflect('N1\C')->getMethod('changed2'), |
||
| 155 | [ |
||
| 156 | '[BC] CHANGED: The number of required arguments for N1\C#changed2() increased from 3 to 4', |
||
| 157 | ], |
||
| 163 |