| Conditions | 58 |
| Paths | 10593 |
| Total Lines | 321 |
| Lines | 100 |
| Ratio | 31.15 % |
| 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 |
||
| 32 | public static function analyze( |
||
| 33 | StatementsAnalyzer $statements_analyzer, |
||
| 34 | PhpParser\Node\Expr\StaticPropertyFetch $stmt, |
||
| 35 | Context $context |
||
| 36 | ) : bool { |
||
| 37 | if (!$stmt->class instanceof PhpParser\Node\Name) { |
||
| 38 | self::analyzeVariableStaticPropertyFetch($statements_analyzer, $stmt->class, $stmt, $context); |
||
| 39 | return true; |
||
| 40 | } |
||
| 41 | |||
| 42 | $fq_class_name = null; |
||
|
|
|||
| 43 | |||
| 44 | $codebase = $statements_analyzer->getCodebase(); |
||
| 45 | |||
| 46 | if (count($stmt->class->parts) === 1 |
||
| 47 | && in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true) |
||
| 48 | ) { |
||
| 49 | if ($stmt->class->parts[0] === 'parent') { |
||
| 50 | $fq_class_name = $statements_analyzer->getParentFQCLN(); |
||
| 51 | |||
| 52 | if ($fq_class_name === null) { |
||
| 53 | if (IssueBuffer::accepts( |
||
| 54 | new ParentNotFound( |
||
| 55 | 'Cannot check property fetch on parent as this class does not extend another', |
||
| 56 | new CodeLocation($statements_analyzer->getSource(), $stmt) |
||
| 57 | ), |
||
| 58 | $statements_analyzer->getSuppressedIssues() |
||
| 59 | )) { |
||
| 60 | return false; |
||
| 61 | } |
||
| 62 | |||
| 63 | return true; |
||
| 64 | } |
||
| 65 | } else { |
||
| 66 | $fq_class_name = (string)$context->self; |
||
| 67 | } |
||
| 68 | |||
| 69 | if ($context->isPhantomClass($fq_class_name)) { |
||
| 70 | return true; |
||
| 71 | } |
||
| 72 | } else { |
||
| 73 | $aliases = $statements_analyzer->getAliases(); |
||
| 74 | |||
| 75 | if ($context->calling_method_id |
||
| 76 | && !$stmt->class instanceof PhpParser\Node\Name\FullyQualified |
||
| 77 | ) { |
||
| 78 | $codebase->file_reference_provider->addMethodReferenceToClassMember( |
||
| 79 | $context->calling_method_id, |
||
| 80 | 'use:' . $stmt->class->parts[0] . ':' . \md5($statements_analyzer->getFilePath()) |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | |||
| 84 | $fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject( |
||
| 85 | $stmt->class, |
||
| 86 | $aliases |
||
| 87 | ); |
||
| 88 | |||
| 89 | if ($context->isPhantomClass($fq_class_name)) { |
||
| 90 | return true; |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($context->check_classes) { |
||
| 94 | if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName( |
||
| 95 | $statements_analyzer, |
||
| 96 | $fq_class_name, |
||
| 97 | new CodeLocation($statements_analyzer->getSource(), $stmt->class), |
||
| 98 | $context->self, |
||
| 99 | $context->calling_method_id, |
||
| 100 | $statements_analyzer->getSuppressedIssues(), |
||
| 101 | false |
||
| 102 | ) !== true) { |
||
| 103 | return false; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($fq_class_name |
||
| 109 | && $codebase->methods_to_move |
||
| 110 | && $context->calling_method_id |
||
| 111 | && isset($codebase->methods_to_move[$context->calling_method_id]) |
||
| 112 | ) { |
||
| 113 | $destination_method_id = $codebase->methods_to_move[$context->calling_method_id]; |
||
| 114 | |||
| 115 | $codebase->classlikes->airliftClassLikeReference( |
||
| 116 | $fq_class_name, |
||
| 117 | explode('::', $destination_method_id)[0], |
||
| 118 | $statements_analyzer->getFilePath(), |
||
| 119 | (int) $stmt->class->getAttribute('startFilePos'), |
||
| 120 | (int) $stmt->class->getAttribute('endFilePos') + 1 |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($fq_class_name) { |
||
| 125 | $statements_analyzer->node_data->setType( |
||
| 126 | $stmt->class, |
||
| 127 | new Type\Union([new TNamedObject($fq_class_name)]) |
||
| 128 | ); |
||
| 129 | } |
||
| 130 | |||
| 131 | View Code Duplication | if ($stmt->name instanceof PhpParser\Node\VarLikeIdentifier) { |
|
| 132 | $prop_name = $stmt->name->name; |
||
| 133 | } elseif (($stmt_name_type = $statements_analyzer->node_data->getType($stmt->name)) |
||
| 134 | && $stmt_name_type->isSingleStringLiteral() |
||
| 135 | ) { |
||
| 136 | $prop_name = $stmt_name_type->getSingleStringLiteral()->value; |
||
| 137 | } else { |
||
| 138 | $prop_name = null; |
||
| 139 | } |
||
| 140 | |||
| 141 | View Code Duplication | if (!$prop_name) { |
|
| 142 | if ($fq_class_name) { |
||
| 143 | $codebase->analyzer->addMixedMemberName( |
||
| 144 | strtolower($fq_class_name) . '::$', |
||
| 145 | $context->calling_method_id ?: $statements_analyzer->getFileName() |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | return true; |
||
| 150 | } |
||
| 151 | |||
| 152 | if (!$fq_class_name |
||
| 153 | || !$context->check_classes |
||
| 154 | || !$context->check_variables |
||
| 155 | || ExpressionAnalyzer::isMock($fq_class_name) |
||
| 156 | ) { |
||
| 157 | return true; |
||
| 158 | } |
||
| 159 | |||
| 160 | $var_id = ExpressionIdentifier::getVarId( |
||
| 161 | $stmt, |
||
| 162 | $context->self ?: $statements_analyzer->getFQCLN(), |
||
| 163 | $statements_analyzer |
||
| 164 | ); |
||
| 165 | |||
| 166 | $property_id = $fq_class_name . '::$' . $prop_name; |
||
| 167 | |||
| 168 | View Code Duplication | if ($codebase->store_node_types |
|
| 169 | && !$context->collect_initializations |
||
| 170 | && !$context->collect_mutations |
||
| 171 | ) { |
||
| 172 | $codebase->analyzer->addNodeReference( |
||
| 173 | $statements_analyzer->getFilePath(), |
||
| 174 | $stmt->name, |
||
| 175 | $property_id |
||
| 176 | ); |
||
| 177 | } |
||
| 178 | |||
| 179 | if ($context->mutation_free) { |
||
| 180 | if (IssueBuffer::accepts( |
||
| 181 | new \Psalm\Issue\ImpureStaticProperty( |
||
| 182 | 'Cannot use a static property in a mutation-free context', |
||
| 183 | new CodeLocation($statements_analyzer, $stmt) |
||
| 184 | ), |
||
| 185 | $statements_analyzer->getSuppressedIssues() |
||
| 186 | )) { |
||
| 187 | // fall through |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | if ($var_id && $context->hasVariable($var_id, $statements_analyzer)) { |
||
| 192 | $stmt_type = $context->vars_in_scope[$var_id]; |
||
| 193 | |||
| 194 | // we don't need to check anything |
||
| 195 | $statements_analyzer->node_data->setType($stmt, $stmt_type); |
||
| 196 | |||
| 197 | if ($codebase->collect_references) { |
||
| 198 | // log the appearance |
||
| 199 | $codebase->properties->propertyExists( |
||
| 200 | $property_id, |
||
| 201 | true, |
||
| 202 | $statements_analyzer, |
||
| 203 | $context, |
||
| 204 | $codebase->collect_locations |
||
| 205 | ? new CodeLocation($statements_analyzer->getSource(), $stmt) |
||
| 206 | : null |
||
| 207 | ); |
||
| 208 | } |
||
| 209 | |||
| 210 | View Code Duplication | if ($codebase->store_node_types |
|
| 211 | && !$context->collect_initializations |
||
| 212 | && !$context->collect_mutations |
||
| 213 | && ($stmt_type = $statements_analyzer->node_data->getType($stmt)) |
||
| 214 | ) { |
||
| 215 | $codebase->analyzer->addNodeType( |
||
| 216 | $statements_analyzer->getFilePath(), |
||
| 217 | $stmt->name, |
||
| 218 | $stmt_type->getId() |
||
| 219 | ); |
||
| 220 | } |
||
| 221 | |||
| 222 | return true; |
||
| 223 | } |
||
| 224 | |||
| 225 | if (!$codebase->properties->propertyExists( |
||
| 226 | $property_id, |
||
| 227 | true, |
||
| 228 | $statements_analyzer, |
||
| 229 | $context, |
||
| 230 | $codebase->collect_locations |
||
| 231 | ? new CodeLocation($statements_analyzer->getSource(), $stmt) |
||
| 232 | : null |
||
| 233 | ) |
||
| 234 | ) { |
||
| 235 | if ($context->inside_isset) { |
||
| 236 | return true; |
||
| 237 | } |
||
| 238 | |||
| 239 | if (IssueBuffer::accepts( |
||
| 240 | new UndefinedPropertyFetch( |
||
| 241 | 'Static property ' . $property_id . ' is not defined', |
||
| 242 | new CodeLocation($statements_analyzer->getSource(), $stmt), |
||
| 243 | $property_id |
||
| 244 | ), |
||
| 245 | $statements_analyzer->getSuppressedIssues() |
||
| 246 | )) { |
||
| 247 | // fall through |
||
| 248 | } |
||
| 249 | |||
| 250 | return true; |
||
| 251 | } |
||
| 252 | |||
| 253 | View Code Duplication | if (ClassLikeAnalyzer::checkPropertyVisibility( |
|
| 254 | $property_id, |
||
| 255 | $context, |
||
| 256 | $statements_analyzer, |
||
| 257 | new CodeLocation($statements_analyzer->getSource(), $stmt), |
||
| 258 | $statements_analyzer->getSuppressedIssues() |
||
| 259 | ) === false) { |
||
| 260 | return false; |
||
| 261 | } |
||
| 262 | |||
| 263 | $declaring_property_class = $codebase->properties->getDeclaringClassForProperty( |
||
| 264 | $fq_class_name . '::$' . $prop_name, |
||
| 265 | true, |
||
| 266 | $statements_analyzer |
||
| 267 | ); |
||
| 268 | |||
| 269 | if ($declaring_property_class === null) { |
||
| 270 | return false; |
||
| 271 | } |
||
| 272 | |||
| 273 | $declaring_property_id = strtolower($declaring_property_class) . '::$' . $prop_name; |
||
| 274 | |||
| 275 | View Code Duplication | if ($codebase->alter_code) { |
|
| 276 | $moved_class = $codebase->classlikes->handleClassLikeReferenceInMigration( |
||
| 277 | $codebase, |
||
| 278 | $statements_analyzer, |
||
| 279 | $stmt->class, |
||
| 280 | $fq_class_name, |
||
| 281 | $context->calling_method_id |
||
| 282 | ); |
||
| 283 | |||
| 284 | if (!$moved_class) { |
||
| 285 | foreach ($codebase->property_transforms as $original_pattern => $transformation) { |
||
| 286 | if ($declaring_property_id === $original_pattern) { |
||
| 287 | list($old_declaring_fq_class_name) = explode('::$', $declaring_property_id); |
||
| 288 | list($new_fq_class_name, $new_property_name) = explode('::$', $transformation); |
||
| 289 | |||
| 290 | $file_manipulations = []; |
||
| 291 | |||
| 292 | if (strtolower($new_fq_class_name) !== strtolower($old_declaring_fq_class_name)) { |
||
| 293 | $file_manipulations[] = new \Psalm\FileManipulation( |
||
| 294 | (int) $stmt->class->getAttribute('startFilePos'), |
||
| 295 | (int) $stmt->class->getAttribute('endFilePos') + 1, |
||
| 296 | Type::getStringFromFQCLN( |
||
| 297 | $new_fq_class_name, |
||
| 298 | $statements_analyzer->getNamespace(), |
||
| 299 | $statements_analyzer->getAliasedClassesFlipped(), |
||
| 300 | null |
||
| 301 | ) |
||
| 302 | ); |
||
| 303 | } |
||
| 304 | |||
| 305 | $file_manipulations[] = new \Psalm\FileManipulation( |
||
| 306 | (int) $stmt->name->getAttribute('startFilePos'), |
||
| 307 | (int) $stmt->name->getAttribute('endFilePos') + 1, |
||
| 308 | '$' . $new_property_name |
||
| 309 | ); |
||
| 310 | |||
| 311 | FileManipulationBuffer::add($statements_analyzer->getFilePath(), $file_manipulations); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | $class_storage = $codebase->classlike_storage_provider->get($declaring_property_class); |
||
| 318 | $property = $class_storage->properties[$prop_name]; |
||
| 319 | |||
| 320 | if ($var_id) { |
||
| 321 | if ($property->type) { |
||
| 322 | $context->vars_in_scope[$var_id] = \Psalm\Internal\Type\TypeExpander::expandUnion( |
||
| 323 | $codebase, |
||
| 324 | clone $property->type, |
||
| 325 | $class_storage->name, |
||
| 326 | $class_storage->name, |
||
| 327 | $class_storage->parent_class |
||
| 328 | ); |
||
| 329 | } else { |
||
| 330 | $context->vars_in_scope[$var_id] = Type::getMixed(); |
||
| 331 | } |
||
| 332 | |||
| 333 | $stmt_type = clone $context->vars_in_scope[$var_id]; |
||
| 334 | |||
| 335 | $statements_analyzer->node_data->setType($stmt, $stmt_type); |
||
| 336 | |||
| 337 | View Code Duplication | if ($codebase->store_node_types |
|
| 338 | && !$context->collect_initializations |
||
| 339 | && !$context->collect_mutations |
||
| 340 | ) { |
||
| 341 | $codebase->analyzer->addNodeType( |
||
| 342 | $statements_analyzer->getFilePath(), |
||
| 343 | $stmt->name, |
||
| 344 | $stmt_type->getId() |
||
| 345 | ); |
||
| 346 | } |
||
| 347 | } else { |
||
| 348 | $statements_analyzer->node_data->setType($stmt, Type::getMixed()); |
||
| 349 | } |
||
| 350 | |||
| 351 | return true; |
||
| 352 | } |
||
| 353 | |||
| 436 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.