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