| Conditions | 1 |
| Paths | 1 |
| Total Lines | 137 |
| Code Lines | 52 |
| 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 |
||
| 45 | public function functionsToBeTested() : array |
||
| 46 | { |
||
| 47 | $astLocator = (new BetterReflection())->astLocator(); |
||
| 48 | |||
| 49 | $fromLocator = new StringSourceLocator( |
||
| 50 | <<<'PHP' |
||
| 51 | <?php |
||
| 52 | |||
| 53 | namespace { |
||
| 54 | function changed(int $a, int $b) {} |
||
| 55 | function untouched(int $a, int $b) {} |
||
| 56 | } |
||
| 57 | |||
| 58 | namespace N1 { |
||
| 59 | class A {} |
||
| 60 | function changed(A $a, A $b) {} |
||
| 61 | function untouched(A $a, A $b) {} |
||
| 62 | } |
||
| 63 | |||
| 64 | namespace N2 { |
||
| 65 | class A {} |
||
| 66 | function changed(A $a, A $b) {} |
||
| 67 | function untouched(A $a, A $b) {} |
||
| 68 | } |
||
| 69 | |||
| 70 | namespace N3 { |
||
| 71 | function changed(?int $a, ?int $b) {} |
||
| 72 | function untouched(?int $a, ?int $b) {} |
||
| 73 | } |
||
| 74 | |||
| 75 | namespace N4 { |
||
| 76 | class C { |
||
| 77 | static function changed1($a, $b) {} |
||
| 78 | function changed2($a, $b) {} |
||
| 79 | } |
||
| 80 | } |
||
| 81 | PHP |
||
| 82 | , |
||
| 83 | $astLocator |
||
| 84 | ); |
||
| 85 | |||
| 86 | $toLocator = new StringSourceLocator( |
||
| 87 | <<<'PHP' |
||
| 88 | <?php |
||
| 89 | |||
| 90 | namespace { |
||
| 91 | function changed(float $a, float $b) {} |
||
| 92 | function untouched(int $a, int $b) {} |
||
| 93 | } |
||
| 94 | |||
| 95 | namespace N1 { |
||
| 96 | class A {} |
||
| 97 | function changed(\N2\A $a, \N2\A $b) {} |
||
| 98 | function untouched(A $a, A $b) {} |
||
| 99 | } |
||
| 100 | |||
| 101 | namespace N2 { |
||
| 102 | class A {} |
||
| 103 | function changed(\N3\A $b) {} |
||
| 104 | function untouched(A $a, A $b) {} |
||
| 105 | } |
||
| 106 | |||
| 107 | namespace N3 { |
||
| 108 | class A {} |
||
| 109 | function changed(int $d, int $e, int $f) {} |
||
| 110 | function untouched(?int $a, ?int $b) {} |
||
| 111 | } |
||
| 112 | |||
| 113 | namespace N4 { |
||
| 114 | class C { |
||
| 115 | static function changed1(int $a, int $b) {} |
||
| 116 | function changed2(int $a, int $b) {} |
||
| 117 | } |
||
| 118 | } |
||
| 119 | PHP |
||
| 120 | , |
||
| 121 | $astLocator |
||
| 122 | ); |
||
| 123 | |||
| 124 | $fromClassReflector = new ClassReflector($fromLocator); |
||
| 125 | $toClassReflector = new ClassReflector($toLocator); |
||
| 126 | $fromReflector = new FunctionReflector($fromLocator, $fromClassReflector); |
||
| 127 | $toReflector = new FunctionReflector($toLocator, $toClassReflector); |
||
| 128 | |||
| 129 | $functions = [ |
||
| 130 | 'changed' => [ |
||
| 131 | '[BC] CHANGED: The parameter $a of changed() changed from int to float', |
||
| 132 | '[BC] CHANGED: The parameter $b of changed() changed from int to float', |
||
| 133 | ], |
||
| 134 | 'untouched' => [], |
||
| 135 | 'N1\changed' => [ |
||
| 136 | '[BC] CHANGED: The parameter $a of N1\changed() changed from N1\A to N2\A', |
||
| 137 | '[BC] CHANGED: The parameter $b of N1\changed() changed from N1\A to N2\A', |
||
| 138 | ], |
||
| 139 | 'N1\untouched' => [], |
||
| 140 | 'N2\changed' => [ |
||
| 141 | '[BC] CHANGED: The parameter $a of N2\changed() changed from N2\A to N3\A', |
||
| 142 | ], |
||
| 143 | 'N2\untouched' => [], |
||
| 144 | 'N3\changed' => [ |
||
| 145 | '[BC] CHANGED: The parameter $a of N3\changed() changed from ?int to int', |
||
| 146 | '[BC] CHANGED: The parameter $b of N3\changed() changed from ?int to int', |
||
| 147 | ], |
||
| 148 | 'N3\untouched' => [], |
||
| 149 | ]; |
||
| 150 | |||
| 151 | return array_merge( |
||
| 152 | array_combine( |
||
| 153 | array_keys($functions), |
||
| 154 | array_map( |
||
| 155 | function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array { |
||
| 156 | return [ |
||
| 157 | $fromReflector->reflect($function), |
||
| 158 | $toReflector->reflect($function), |
||
| 159 | $errorMessages, |
||
| 160 | ]; |
||
| 161 | }, |
||
| 162 | array_keys($functions), |
||
| 163 | $functions |
||
| 164 | ) |
||
| 165 | ), |
||
| 166 | [ |
||
| 167 | 'N4\C::changed1' => [ |
||
| 168 | $fromClassReflector->reflect('N4\C')->getMethod('changed1'), |
||
| 169 | $toClassReflector->reflect('N4\C')->getMethod('changed1'), |
||
| 170 | [ |
||
| 171 | '[BC] CHANGED: The parameter $a of N4\C::changed1() changed from no type to int', |
||
| 172 | '[BC] CHANGED: The parameter $b of N4\C::changed1() changed from no type to int', |
||
| 173 | |||
| 174 | ], |
||
| 175 | ], |
||
| 176 | 'N4\C#changed2' => [ |
||
| 177 | $fromClassReflector->reflect('N4\C')->getMethod('changed2'), |
||
| 178 | $toClassReflector->reflect('N4\C')->getMethod('changed2'), |
||
| 179 | [ |
||
| 180 | '[BC] CHANGED: The parameter $a of N4\C#changed2() changed from no type to int', |
||
| 181 | '[BC] CHANGED: The parameter $b of N4\C#changed2() changed from no type to int', |
||
| 182 | ] |
||
| 188 |