Conditions | 52 |
Paths | 123 |
Total Lines | 308 |
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 |
||
89 | public static function getFormula( |
||
90 | int $object_id, |
||
91 | PhpParser\Node\Expr $conditional, |
||
92 | $this_class_name, |
||
93 | FileSource $source, |
||
94 | Codebase $codebase = null, |
||
95 | bool $inside_negation = false, |
||
96 | bool $cache = true |
||
97 | ) { |
||
98 | if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd || |
||
99 | $conditional instanceof PhpParser\Node\Expr\BinaryOp\LogicalAnd |
||
100 | ) { |
||
101 | $left_assertions = self::getFormula( |
||
102 | $object_id, |
||
103 | $conditional->left, |
||
104 | $this_class_name, |
||
105 | $source, |
||
106 | $codebase, |
||
107 | $inside_negation, |
||
108 | $cache |
||
109 | ); |
||
110 | |||
111 | $right_assertions = self::getFormula( |
||
112 | $object_id, |
||
113 | $conditional->right, |
||
114 | $this_class_name, |
||
115 | $source, |
||
116 | $codebase, |
||
117 | $inside_negation, |
||
118 | $cache |
||
119 | ); |
||
120 | |||
121 | return array_merge( |
||
122 | $left_assertions, |
||
123 | $right_assertions |
||
124 | ); |
||
125 | } |
||
126 | |||
127 | if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr || |
||
128 | $conditional instanceof PhpParser\Node\Expr\BinaryOp\LogicalOr |
||
129 | ) { |
||
130 | // at the moment we only support formulae in CNF |
||
131 | |||
132 | $left_clauses = self::getFormula( |
||
133 | $object_id, |
||
134 | $conditional->left, |
||
135 | $this_class_name, |
||
136 | $source, |
||
137 | $codebase, |
||
138 | $inside_negation, |
||
139 | $cache |
||
140 | ); |
||
141 | |||
142 | $right_clauses = self::getFormula( |
||
143 | $object_id, |
||
144 | $conditional->right, |
||
145 | $this_class_name, |
||
146 | $source, |
||
147 | $codebase, |
||
148 | $inside_negation, |
||
149 | $cache |
||
150 | ); |
||
151 | |||
152 | return self::combineOredClauses($left_clauses, $right_clauses); |
||
153 | } |
||
154 | |||
155 | if ($conditional instanceof PhpParser\Node\Expr\BooleanNot) { |
||
156 | if ($conditional->expr instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr) { |
||
157 | $and_expr = new PhpParser\Node\Expr\BinaryOp\BooleanAnd( |
||
158 | new PhpParser\Node\Expr\BooleanNot( |
||
159 | $conditional->expr->left, |
||
160 | $conditional->getAttributes() |
||
161 | ), |
||
162 | new PhpParser\Node\Expr\BooleanNot( |
||
163 | $conditional->expr->right, |
||
164 | $conditional->getAttributes() |
||
165 | ), |
||
166 | $conditional->expr->getAttributes() |
||
167 | ); |
||
168 | |||
169 | return self::getFormula( |
||
170 | $object_id, |
||
171 | $and_expr, |
||
172 | $this_class_name, |
||
173 | $source, |
||
174 | $codebase, |
||
175 | $inside_negation, |
||
176 | false |
||
177 | ); |
||
178 | } |
||
179 | |||
180 | if ($conditional->expr instanceof PhpParser\Node\Expr\Isset_ |
||
181 | && count($conditional->expr->vars) > 1 |
||
182 | ) { |
||
183 | $assertions = null; |
||
184 | |||
185 | if ($cache && $source instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) { |
||
186 | $assertions = $source->node_data->getAssertions($conditional->expr); |
||
187 | } |
||
188 | |||
189 | if ($assertions === null) { |
||
190 | $assertions = AssertionFinder::scrapeAssertions( |
||
191 | $conditional->expr, |
||
192 | $this_class_name, |
||
193 | $source, |
||
194 | $codebase, |
||
195 | $inside_negation, |
||
196 | $cache |
||
197 | ); |
||
198 | |||
199 | if ($cache && $source instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) { |
||
200 | $source->node_data->setAssertions($conditional->expr, $assertions); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | if ($assertions !== null) { |
||
205 | $clauses = []; |
||
206 | |||
207 | foreach ($assertions as $var => $anded_types) { |
||
208 | $redefined = false; |
||
209 | |||
210 | if ($var[0] === '=') { |
||
211 | /** @var string */ |
||
212 | $var = substr($var, 1); |
||
213 | $redefined = true; |
||
214 | } |
||
215 | |||
216 | foreach ($anded_types as $orred_types) { |
||
217 | $clauses[] = new Clause( |
||
218 | [$var => $orred_types], |
||
219 | false, |
||
220 | true, |
||
221 | $orred_types[0][0] === '=' |
||
222 | || $orred_types[0][0] === '~' |
||
223 | || (strlen($orred_types[0]) > 1 |
||
224 | && ($orred_types[0][1] === '=' |
||
225 | || $orred_types[0][1] === '~')), |
||
226 | $redefined ? [$var => true] : [], |
||
227 | $object_id |
||
228 | ); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | return self::negateFormula($clauses); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | if ($conditional->expr instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd) { |
||
237 | $and_expr = new PhpParser\Node\Expr\BinaryOp\BooleanOr( |
||
238 | new PhpParser\Node\Expr\BooleanNot( |
||
239 | $conditional->expr->left, |
||
240 | $conditional->getAttributes() |
||
241 | ), |
||
242 | new PhpParser\Node\Expr\BooleanNot( |
||
243 | $conditional->expr->right, |
||
244 | $conditional->getAttributes() |
||
245 | ), |
||
246 | $conditional->expr->getAttributes() |
||
247 | ); |
||
248 | |||
249 | return self::getFormula( |
||
250 | $object_id, |
||
251 | $and_expr, |
||
252 | $this_class_name, |
||
253 | $source, |
||
254 | $codebase, |
||
255 | $inside_negation, |
||
256 | false |
||
257 | ); |
||
258 | } |
||
259 | |||
260 | return self::negateFormula( |
||
261 | self::getFormula( |
||
262 | $object_id, |
||
263 | $conditional->expr, |
||
264 | $this_class_name, |
||
265 | $source, |
||
266 | $codebase, |
||
267 | !$inside_negation |
||
268 | ) |
||
269 | ); |
||
270 | } |
||
271 | |||
272 | if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical |
||
273 | || $conditional instanceof PhpParser\Node\Expr\BinaryOp\Equal |
||
274 | ) { |
||
275 | $false_pos = AssertionFinder::hasFalseVariable($conditional); |
||
276 | $true_pos = AssertionFinder::hasTrueVariable($conditional); |
||
277 | |||
278 | if ($false_pos === AssertionFinder::ASSIGNMENT_TO_RIGHT |
||
279 | && ($conditional->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd |
||
280 | || $conditional->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr) |
||
281 | ) { |
||
282 | $inside_negation = !$inside_negation; |
||
283 | |||
284 | return self::getFormula( |
||
285 | $object_id, |
||
286 | $conditional->left, |
||
287 | $this_class_name, |
||
288 | $source, |
||
289 | $codebase, |
||
290 | $inside_negation, |
||
291 | $cache |
||
292 | ); |
||
293 | } |
||
294 | |||
295 | if ($false_pos === AssertionFinder::ASSIGNMENT_TO_LEFT |
||
296 | && ($conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd |
||
297 | || $conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr) |
||
298 | ) { |
||
299 | $inside_negation = !$inside_negation; |
||
300 | |||
301 | return self::getFormula( |
||
302 | $object_id, |
||
303 | $conditional->right, |
||
304 | $this_class_name, |
||
305 | $source, |
||
306 | $codebase, |
||
307 | $inside_negation, |
||
308 | $cache |
||
309 | ); |
||
310 | } |
||
311 | |||
312 | if ($true_pos === AssertionFinder::ASSIGNMENT_TO_RIGHT |
||
313 | && ($conditional->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd |
||
314 | || $conditional->left instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr) |
||
315 | ) { |
||
316 | return self::getFormula( |
||
317 | $object_id, |
||
318 | $conditional->left, |
||
319 | $this_class_name, |
||
320 | $source, |
||
321 | $codebase, |
||
322 | $inside_negation, |
||
323 | $cache |
||
324 | ); |
||
325 | } |
||
326 | |||
327 | if ($true_pos === AssertionFinder::ASSIGNMENT_TO_LEFT |
||
328 | && ($conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd |
||
329 | || $conditional->right instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr) |
||
330 | ) { |
||
331 | return self::getFormula( |
||
332 | $object_id, |
||
333 | $conditional->right, |
||
334 | $this_class_name, |
||
335 | $source, |
||
336 | $codebase, |
||
337 | $inside_negation, |
||
338 | $cache |
||
339 | ); |
||
340 | } |
||
341 | } |
||
342 | |||
343 | $assertions = null; |
||
344 | |||
345 | if ($cache && $source instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) { |
||
346 | $assertions = $source->node_data->getAssertions($conditional); |
||
347 | } |
||
348 | |||
349 | if ($assertions === null) { |
||
350 | $assertions = AssertionFinder::scrapeAssertions( |
||
351 | $conditional, |
||
352 | $this_class_name, |
||
353 | $source, |
||
354 | $codebase, |
||
355 | $inside_negation, |
||
356 | $cache |
||
357 | ); |
||
358 | |||
359 | if ($cache && $source instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) { |
||
360 | $source->node_data->setAssertions($conditional, $assertions); |
||
361 | } |
||
362 | } |
||
363 | |||
364 | if ($assertions) { |
||
365 | $clauses = []; |
||
366 | |||
367 | foreach ($assertions as $var => $anded_types) { |
||
368 | $redefined = false; |
||
369 | |||
370 | if ($var[0] === '=') { |
||
371 | /** @var string */ |
||
372 | $var = substr($var, 1); |
||
373 | $redefined = true; |
||
374 | } |
||
375 | |||
376 | foreach ($anded_types as $orred_types) { |
||
377 | $clauses[] = new Clause( |
||
378 | [$var => $orred_types], |
||
379 | false, |
||
380 | true, |
||
381 | $orred_types[0][0] === '=' |
||
382 | || $orred_types[0][0] === '~' |
||
383 | || (strlen($orred_types[0]) > 1 |
||
384 | && ($orred_types[0][1] === '=' |
||
385 | || $orred_types[0][1] === '~')), |
||
386 | $redefined ? [$var => true] : [], |
||
387 | $object_id |
||
388 | ); |
||
389 | } |
||
390 | } |
||
391 | |||
392 | return $clauses; |
||
393 | } |
||
394 | |||
395 | return [new Clause([], true)]; |
||
396 | } |
||
397 | |||
865 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.