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