| Conditions | 1 |
| Paths | 1 |
| Total Lines | 103 |
| 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 valueToReference($a) {} |
||
| 60 | function referenceToValue(& $a) {} |
||
| 61 | function valueToValue($a) {} |
||
| 62 | function referenceToReference(& $a) {} |
||
| 63 | function referenceOnRemovedParameter($a, & $b) {} |
||
| 64 | function referenceToValueOnRenamedParameter(& $a, & $b) {} |
||
| 65 | function addedParameter(& $a, & $b) {} |
||
| 66 | } |
||
| 67 | namespace N1 { |
||
| 68 | class C { |
||
| 69 | static function changed1($a) {} |
||
| 70 | function changed2($a) {} |
||
| 71 | } |
||
| 72 | } |
||
| 73 | PHP |
||
| 74 | , |
||
| 75 | $astLocator |
||
| 76 | ); |
||
| 77 | |||
| 78 | $toLocator = new StringSourceLocator( |
||
| 79 | <<<'PHP' |
||
| 80 | <?php |
||
| 81 | |||
| 82 | namespace { |
||
| 83 | function valueToReference(& $a) {} |
||
| 84 | function referenceToValue($a) {} |
||
| 85 | function valueToValue($a) {} |
||
| 86 | function referenceToReference(& $a) {} |
||
| 87 | function referenceOnRemovedParameter($a) {} |
||
| 88 | function referenceToValueOnRenamedParameter(& $b, $a) {} |
||
| 89 | function addedParameter(& $a, & $b, $c) {} |
||
| 90 | } |
||
| 91 | namespace N1 { |
||
| 92 | class C { |
||
| 93 | static function changed1(& $a) {} |
||
| 94 | function changed2(& $a) {} |
||
| 95 | } |
||
| 96 | } |
||
| 97 | PHP |
||
| 98 | , |
||
| 99 | $astLocator |
||
| 100 | ); |
||
| 101 | |||
| 102 | $fromClassReflector = new ClassReflector($fromLocator); |
||
| 103 | $toClassReflector = new ClassReflector($toLocator); |
||
| 104 | $fromReflector = new FunctionReflector($fromLocator, $fromClassReflector); |
||
| 105 | $toReflector = new FunctionReflector($toLocator, $toClassReflector); |
||
| 106 | |||
| 107 | $functions = [ |
||
| 108 | 'valueToReference' => [ |
||
| 109 | '[BC] CHANGED: The parameter $a of valueToReference() changed from by-value to by-reference', |
||
| 110 | ], |
||
| 111 | 'referenceToValue' => [ |
||
| 112 | '[BC] CHANGED: The parameter $a of referenceToValue() changed from by-reference to by-value', |
||
| 113 | ], |
||
| 114 | 'valueToValue' => [], |
||
| 115 | 'referenceToReference' => [], |
||
| 116 | 'referenceOnRemovedParameter' => [], |
||
| 117 | 'referenceToValueOnRenamedParameter' => [ |
||
| 118 | '[BC] CHANGED: The parameter $b of referenceToValueOnRenamedParameter() changed from by-reference to by-value', |
||
| 119 | ], |
||
| 120 | 'addedParameter' => [], |
||
| 121 | ]; |
||
| 122 | |||
| 123 | return array_merge( |
||
| 124 | array_combine( |
||
|
|
|||
| 125 | array_keys($functions), |
||
| 126 | array_map( |
||
| 127 | /** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */ |
||
| 128 | static function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array { |
||
| 129 | return [ |
||
| 130 | $fromReflector->reflect($function), |
||
| 131 | $toReflector->reflect($function), |
||
| 132 | $errorMessages, |
||
| 133 | ]; |
||
| 134 | }, |
||
| 135 | array_keys($functions), |
||
| 136 | $functions |
||
| 137 | ) |
||
| 138 | ), |
||
| 139 | [ |
||
| 140 | 'N1\C::changed1' => [ |
||
| 141 | $fromClassReflector->reflect('N1\C')->getMethod('changed1'), |
||
| 142 | $toClassReflector->reflect('N1\C')->getMethod('changed1'), |
||
| 143 | [ |
||
| 144 | '[BC] CHANGED: The parameter $a of N1\C::changed1() changed from by-value to by-reference', |
||
| 145 | |||
| 146 | ], |
||
| 147 | ], |
||
| 148 | 'N1\C#changed2' => [ |
||
| 149 | $fromClassReflector->reflect('N1\C')->getMethod('changed2'), |
||
| 150 | $toClassReflector->reflect('N1\C')->getMethod('changed2'), |
||
| 151 | [ |
||
| 152 | '[BC] CHANGED: The parameter $a of N1\C#changed2() changed from by-value to by-reference', |
||
| 153 | ], |
||
| 159 |