| Conditions | 1 |
| Paths | 1 |
| Total Lines | 102 |
| Code Lines | 47 |
| 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 changed($a = 1) {} |
||
| 60 | function defaultAdded($a) {} |
||
| 61 | function defaultRemoved($a = null) {} |
||
| 62 | function defaultTypeChanged($a = '1') {} |
||
| 63 | function notChanged($a = 1, $b = 2, $c = 3) {} |
||
| 64 | function namesChanged($a = 1, $b = 2, $c = 3) {} |
||
| 65 | function orderChanged($a = 1, $b = 2, $c = 3) {} |
||
| 66 | function positionOfOptionalParameterChanged($a = 2, $b, $c = 1) {} |
||
| 67 | class C { |
||
| 68 | static function changed1($a = 1) {} |
||
| 69 | function changed2($a = 1) {} |
||
| 70 | } |
||
| 71 | } |
||
| 72 | PHP |
||
| 73 | , |
||
| 74 | $astLocator |
||
| 75 | ); |
||
| 76 | |||
| 77 | $toLocator = new StringSourceLocator( |
||
| 78 | <<<'PHP' |
||
| 79 | <?php |
||
| 80 | |||
| 81 | namespace { |
||
| 82 | function changed($a = 2) {} |
||
| 83 | function defaultAdded($a = 1) {} |
||
| 84 | function defaultRemoved($a) {} |
||
| 85 | function defaultTypeChanged($a = 1) {} |
||
| 86 | function notChanged($a = 1, $b = 2, $c = 3) {} |
||
| 87 | function namesChanged($d = 1, $e = 2, $f = 3) {} |
||
| 88 | function orderChanged($c = 3, $b = 2, $a = 1) {} |
||
| 89 | function positionOfOptionalParameterChanged($a, $b = 2, $c = 1) {} |
||
| 90 | class C { |
||
| 91 | static function changed1($a = 2) {} |
||
| 92 | function changed2($a = 2) {} |
||
| 93 | } |
||
| 94 | } |
||
| 95 | PHP |
||
| 96 | , |
||
| 97 | $astLocator |
||
| 98 | ); |
||
| 99 | |||
| 100 | $fromClassReflector = new ClassReflector($fromLocator); |
||
| 101 | $toClassReflector = new ClassReflector($toLocator); |
||
| 102 | $fromReflector = new FunctionReflector($fromLocator, $fromClassReflector); |
||
| 103 | $toReflector = new FunctionReflector($toLocator, $toClassReflector); |
||
| 104 | |||
| 105 | $functions = [ |
||
| 106 | 'changed' => [ |
||
| 107 | '[BC] CHANGED: Default parameter value for for parameter $a of changed() changed from 1 to 2', |
||
| 108 | ], |
||
| 109 | 'defaultAdded' => [], |
||
| 110 | 'defaultRemoved' => [], |
||
| 111 | 'defaultTypeChanged' => [ |
||
| 112 | '[BC] CHANGED: Default parameter value for for parameter $a of defaultTypeChanged() changed from \'1\' to 1', |
||
| 113 | ], |
||
| 114 | 'notChanged' => [], |
||
| 115 | 'namesChanged' => [], |
||
| 116 | 'orderChanged' => [ |
||
| 117 | '[BC] CHANGED: Default parameter value for for parameter $a of orderChanged() changed from 1 to 3', |
||
| 118 | '[BC] CHANGED: Default parameter value for for parameter $c of orderChanged() changed from 3 to 1', |
||
| 119 | ], |
||
| 120 | 'positionOfOptionalParameterChanged' => [], |
||
| 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 | 'C::changed1' => [ |
||
| 141 | $fromClassReflector->reflect('C')->getMethod('changed1'), |
||
| 142 | $toClassReflector->reflect('C')->getMethod('changed1'), |
||
| 143 | [ |
||
| 144 | '[BC] CHANGED: Default parameter value for for parameter $a of C::changed1() changed from 1 to 2', |
||
| 145 | ], |
||
| 146 | ], |
||
| 147 | 'C#changed2' => [ |
||
| 148 | $fromClassReflector->reflect('C')->getMethod('changed2'), |
||
| 149 | $toClassReflector->reflect('C')->getMethod('changed2'), |
||
| 150 | [ |
||
| 151 | '[BC] CHANGED: Default parameter value for for parameter $a of C#changed2() changed from 1 to 2', |
||
| 152 | ], |
||
| 158 |