| Conditions | 144 |
| Paths | > 20000 |
| Total Lines | 887 |
| 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 |
||
| 107 | public function analyze( |
||
| 108 | Context $class_context = null, |
||
| 109 | Context $global_context = null |
||
| 110 | ) { |
||
| 111 | $class = $this->class; |
||
| 112 | |||
| 113 | if (!$class instanceof PhpParser\Node\Stmt\Class_) { |
||
| 114 | throw new \LogicException('Something went badly wrong'); |
||
| 115 | } |
||
| 116 | |||
| 117 | $fq_class_name = $class_context && $class_context->self ? $class_context->self : $this->fq_class_name; |
||
| 118 | |||
| 119 | $storage = $this->storage; |
||
| 120 | |||
| 121 | if ($storage->has_visitor_issues) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | |||
| 125 | if ($class->name |
||
| 126 | && (preg_match( |
||
| 127 | '/(^|\\\)(int|float|bool|string|void|null|false|true|object|mixed)$/i', |
||
| 128 | $fq_class_name |
||
| 129 | ) || strtolower($fq_class_name) === 'resource') |
||
| 130 | ) { |
||
| 131 | $class_name_parts = explode('\\', $fq_class_name); |
||
| 132 | $class_name = array_pop($class_name_parts); |
||
| 133 | |||
| 134 | if (IssueBuffer::accepts( |
||
| 135 | new ReservedWord( |
||
| 136 | $class_name . ' is a reserved word', |
||
| 137 | new CodeLocation( |
||
| 138 | $this, |
||
| 139 | $class->name, |
||
| 140 | null, |
||
| 141 | true |
||
| 142 | ), |
||
| 143 | $class_name |
||
| 144 | ), |
||
| 145 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 146 | )) { |
||
| 147 | // fall through |
||
| 148 | } |
||
| 149 | |||
| 150 | return null; |
||
| 151 | } |
||
| 152 | |||
| 153 | $project_analyzer = $this->file_analyzer->project_analyzer; |
||
| 154 | $codebase = $this->getCodebase(); |
||
| 155 | |||
| 156 | if ($codebase->alter_code && $class->name && $codebase->classes_to_move) { |
||
| 157 | if (isset($codebase->classes_to_move[strtolower($this->fq_class_name)])) { |
||
| 158 | $destination_class = $codebase->classes_to_move[strtolower($this->fq_class_name)]; |
||
| 159 | |||
| 160 | $source_class_parts = explode('\\', $this->fq_class_name); |
||
| 161 | $destination_class_parts = explode('\\', $destination_class); |
||
| 162 | |||
| 163 | array_pop($source_class_parts); |
||
| 164 | array_pop($destination_class_parts); |
||
| 165 | |||
| 166 | $source_ns = implode('\\', $source_class_parts); |
||
| 167 | $destination_ns = implode('\\', $destination_class_parts); |
||
| 168 | |||
| 169 | if (strtolower($source_ns) !== strtolower($destination_ns)) { |
||
| 170 | if ($storage->namespace_name_location) { |
||
| 171 | $bounds = $storage->namespace_name_location->getSelectionBounds(); |
||
| 172 | |||
| 173 | $file_manipulations = [ |
||
| 174 | new \Psalm\FileManipulation( |
||
| 175 | $bounds[0], |
||
| 176 | $bounds[1], |
||
| 177 | $destination_ns |
||
| 178 | ) |
||
| 179 | ]; |
||
| 180 | |||
| 181 | \Psalm\Internal\FileManipulation\FileManipulationBuffer::add( |
||
| 182 | $this->getFilePath(), |
||
| 183 | $file_manipulations |
||
| 184 | ); |
||
| 185 | } elseif (!$source_ns) { |
||
| 186 | $first_statement_pos = $this->getFileAnalyzer()->getFirstStatementOffset(); |
||
| 187 | |||
| 188 | if ($first_statement_pos === -1) { |
||
| 189 | $first_statement_pos = (int) $class->getAttribute('startFilePos'); |
||
| 190 | } |
||
| 191 | |||
| 192 | $file_manipulations = [ |
||
| 193 | new \Psalm\FileManipulation( |
||
| 194 | $first_statement_pos, |
||
| 195 | $first_statement_pos, |
||
| 196 | 'namespace ' . $destination_ns . ';' . "\n\n", |
||
| 197 | true |
||
| 198 | ) |
||
| 199 | ]; |
||
| 200 | |||
| 201 | \Psalm\Internal\FileManipulation\FileManipulationBuffer::add( |
||
| 202 | $this->getFilePath(), |
||
| 203 | $file_manipulations |
||
| 204 | ); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | $codebase->classlikes->handleClassLikeReferenceInMigration( |
||
| 210 | $codebase, |
||
| 211 | $this, |
||
| 212 | $class->name, |
||
| 213 | $this->fq_class_name, |
||
| 214 | null |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | |||
| 218 | foreach ($storage->docblock_issues as $docblock_issue) { |
||
| 219 | IssueBuffer::add($docblock_issue); |
||
| 220 | } |
||
| 221 | |||
| 222 | $classlike_storage_provider = $codebase->classlike_storage_provider; |
||
| 223 | |||
| 224 | $parent_fq_class_name = $this->parent_fq_class_name; |
||
| 225 | |||
| 226 | if ($class->extends) { |
||
| 227 | if (!$parent_fq_class_name) { |
||
| 228 | throw new \UnexpectedValueException('Parent class should be filled in for ' . $fq_class_name); |
||
| 229 | } |
||
| 230 | |||
| 231 | $parent_reference_location = new CodeLocation($this, $class->extends); |
||
| 232 | |||
| 233 | if (self::checkFullyQualifiedClassLikeName( |
||
| 234 | $this->getSource(), |
||
| 235 | $parent_fq_class_name, |
||
| 236 | $parent_reference_location, |
||
| 237 | null, |
||
| 238 | null, |
||
| 239 | $storage->suppressed_issues + $this->getSuppressedIssues(), |
||
| 240 | false |
||
| 241 | ) === false) { |
||
| 242 | return false; |
||
| 243 | } |
||
| 244 | |||
| 245 | if ($codebase->alter_code && $codebase->classes_to_move) { |
||
| 246 | $codebase->classlikes->handleClassLikeReferenceInMigration( |
||
| 247 | $codebase, |
||
| 248 | $this, |
||
| 249 | $class->extends, |
||
| 250 | $parent_fq_class_name, |
||
| 251 | null |
||
| 252 | ); |
||
| 253 | } |
||
| 254 | |||
| 255 | try { |
||
| 256 | $parent_class_storage = $classlike_storage_provider->get($parent_fq_class_name); |
||
| 257 | |||
| 258 | $code_location = new CodeLocation( |
||
| 259 | $this, |
||
| 260 | $class->extends, |
||
| 261 | $class_context ? $class_context->include_location : null, |
||
| 262 | true |
||
| 263 | ); |
||
| 264 | |||
| 265 | if ($parent_class_storage->is_trait || $parent_class_storage->is_interface) { |
||
| 266 | if (IssueBuffer::accepts( |
||
| 267 | new UndefinedClass( |
||
| 268 | $parent_fq_class_name . ' is not a class', |
||
| 269 | $code_location, |
||
| 270 | $parent_fq_class_name . ' as class' |
||
| 271 | ), |
||
| 272 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 273 | )) { |
||
| 274 | // fall through |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | if ($parent_class_storage->final) { |
||
| 279 | if (IssueBuffer::accepts( |
||
| 280 | new InvalidExtendClass( |
||
| 281 | 'Class ' . $fq_class_name . ' may not inherit from final class ' . $parent_fq_class_name, |
||
| 282 | $code_location, |
||
| 283 | $fq_class_name |
||
| 284 | ), |
||
| 285 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 286 | )) { |
||
| 287 | // fall through |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | if ($parent_class_storage->deprecated) { |
||
| 292 | if (IssueBuffer::accepts( |
||
| 293 | new DeprecatedClass( |
||
| 294 | $parent_fq_class_name . ' is marked deprecated', |
||
| 295 | $code_location, |
||
| 296 | $parent_fq_class_name |
||
| 297 | ), |
||
| 298 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 299 | )) { |
||
| 300 | // fall through |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | if ($parent_class_storage->internal) { |
||
| 305 | $code_location = new CodeLocation( |
||
| 306 | $this, |
||
| 307 | $class->extends, |
||
| 308 | $class_context ? $class_context->include_location : null, |
||
| 309 | true |
||
| 310 | ); |
||
| 311 | if (! NamespaceAnalyzer::nameSpaceRootsMatch($fq_class_name, $parent_fq_class_name)) { |
||
| 312 | if (IssueBuffer::accepts( |
||
| 313 | new InternalClass( |
||
| 314 | $parent_fq_class_name . ' is marked internal', |
||
| 315 | $code_location, |
||
| 316 | $parent_fq_class_name |
||
| 317 | ), |
||
| 318 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 319 | )) { |
||
| 320 | // fall through |
||
| 321 | } |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | if ($parent_class_storage->psalm_internal && |
||
| 326 | ! NamespaceAnalyzer::isWithin($fq_class_name, $parent_class_storage->psalm_internal) |
||
| 327 | ) { |
||
| 328 | if (IssueBuffer::accepts( |
||
| 329 | new InternalClass( |
||
| 330 | $parent_fq_class_name . ' is internal to ' . $parent_class_storage->psalm_internal, |
||
| 331 | $code_location, |
||
| 332 | $parent_fq_class_name |
||
| 333 | ), |
||
| 334 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 335 | )) { |
||
| 336 | // fall through |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | if ($parent_class_storage->external_mutation_free |
||
| 341 | && !$storage->external_mutation_free |
||
| 342 | ) { |
||
| 343 | if (IssueBuffer::accepts( |
||
| 344 | new MissingImmutableAnnotation( |
||
| 345 | $parent_fq_class_name . ' is marked immutable, but ' |
||
| 346 | . $fq_class_name . ' is not marked immutable', |
||
| 347 | $code_location |
||
| 348 | ), |
||
| 349 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 350 | )) { |
||
| 351 | // fall through |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | if ($storage->mutation_free |
||
| 356 | && !$parent_class_storage->mutation_free |
||
| 357 | ) { |
||
| 358 | if (IssueBuffer::accepts( |
||
| 359 | new MutableDependency( |
||
| 360 | $fq_class_name . ' is marked immutable but ' . $parent_fq_class_name . ' is not', |
||
| 361 | $code_location |
||
| 362 | ), |
||
| 363 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 364 | )) { |
||
| 365 | // fall through |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | if ($codebase->store_node_types) { |
||
| 370 | $codebase->analyzer->addNodeReference( |
||
| 371 | $this->getFilePath(), |
||
| 372 | $class->extends, |
||
| 373 | $codebase->classlikes->classExists($parent_fq_class_name) |
||
| 374 | ? $parent_fq_class_name |
||
| 375 | : '*' . implode('\\', $class->extends->parts) |
||
| 376 | ); |
||
| 377 | } |
||
| 378 | |||
| 379 | $code_location = new CodeLocation( |
||
| 380 | $this, |
||
| 381 | $class->name ?: $class, |
||
| 382 | $class_context ? $class_context->include_location : null, |
||
| 383 | true |
||
| 384 | ); |
||
| 385 | |||
| 386 | if ($storage->template_type_extends_count !== null) { |
||
| 387 | $this->checkTemplateParams( |
||
| 388 | $codebase, |
||
| 389 | $storage, |
||
| 390 | $parent_class_storage, |
||
| 391 | $code_location, |
||
| 392 | $storage->template_type_extends_count |
||
| 393 | ); |
||
| 394 | } |
||
| 395 | } catch (\InvalidArgumentException $e) { |
||
| 396 | // do nothing |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 400 | foreach ($class->implements as $interface_name) { |
||
| 401 | $fq_interface_name = self::getFQCLNFromNameObject( |
||
| 402 | $interface_name, |
||
| 403 | $this->source->getAliases() |
||
| 404 | ); |
||
| 405 | |||
| 406 | $codebase->analyzer->addNodeReference( |
||
| 407 | $this->getFilePath(), |
||
| 408 | $interface_name, |
||
| 409 | $codebase->classlikes->interfaceExists($fq_interface_name) |
||
| 410 | ? $fq_interface_name |
||
| 411 | : '*' . implode('\\', $interface_name->parts) |
||
| 412 | ); |
||
| 413 | |||
| 414 | $interface_location = new CodeLocation($this, $interface_name); |
||
| 415 | |||
| 416 | if (self::checkFullyQualifiedClassLikeName( |
||
| 417 | $this, |
||
| 418 | $fq_interface_name, |
||
| 419 | $interface_location, |
||
| 420 | null, |
||
| 421 | null, |
||
| 422 | $this->getSuppressedIssues(), |
||
| 423 | false |
||
| 424 | ) === false) { |
||
| 425 | continue; |
||
| 426 | } |
||
| 427 | |||
| 428 | if ($codebase->store_node_types && $fq_class_name) { |
||
| 429 | $bounds = $interface_location->getSelectionBounds(); |
||
| 430 | |||
| 431 | $codebase->analyzer->addOffsetReference( |
||
| 432 | $this->getFilePath(), |
||
| 433 | $bounds[0], |
||
| 434 | $bounds[1], |
||
| 435 | $fq_interface_name |
||
| 436 | ); |
||
| 437 | } |
||
| 438 | |||
| 439 | $codebase->classlikes->handleClassLikeReferenceInMigration( |
||
| 440 | $codebase, |
||
| 441 | $this, |
||
| 442 | $interface_name, |
||
| 443 | $fq_interface_name, |
||
| 444 | null |
||
| 445 | ); |
||
| 446 | |||
| 447 | $fq_interface_name_lc = strtolower($fq_interface_name); |
||
| 448 | |||
| 449 | try { |
||
| 450 | $interface_storage = $classlike_storage_provider->get($fq_interface_name_lc); |
||
| 451 | } catch (\InvalidArgumentException $e) { |
||
| 452 | continue; |
||
| 453 | } |
||
| 454 | |||
| 455 | $code_location = new CodeLocation( |
||
| 456 | $this, |
||
| 457 | $interface_name, |
||
| 458 | $class_context ? $class_context->include_location : null, |
||
| 459 | true |
||
| 460 | ); |
||
| 461 | |||
| 462 | if (!$interface_storage->is_interface) { |
||
| 463 | if (IssueBuffer::accepts( |
||
| 464 | new UndefinedInterface( |
||
| 465 | $fq_interface_name . ' is not an interface', |
||
| 466 | $code_location, |
||
| 467 | $fq_interface_name |
||
| 468 | ), |
||
| 469 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 470 | )) { |
||
| 471 | // fall through |
||
| 472 | } |
||
| 473 | } |
||
| 474 | |||
| 475 | if (isset($storage->template_type_implements_count[$fq_interface_name_lc])) { |
||
| 476 | $expected_param_count = $storage->template_type_implements_count[$fq_interface_name_lc]; |
||
| 477 | |||
| 478 | $this->checkTemplateParams( |
||
| 479 | $codebase, |
||
| 480 | $storage, |
||
| 481 | $interface_storage, |
||
| 482 | $code_location, |
||
| 483 | $expected_param_count |
||
| 484 | ); |
||
| 485 | } |
||
| 486 | } |
||
| 487 | |||
| 488 | if ($storage->template_types) { |
||
| 489 | foreach ($storage->template_types as $param_name => $_) { |
||
| 490 | $fq_classlike_name = Type::getFQCLNFromString( |
||
| 491 | $param_name, |
||
| 492 | $this->getAliases() |
||
| 493 | ); |
||
| 494 | |||
| 495 | if ($codebase->classOrInterfaceExists($fq_classlike_name)) { |
||
| 496 | if (IssueBuffer::accepts( |
||
| 497 | new ReservedWord( |
||
| 498 | 'Cannot use ' . $param_name . ' as template name since the class already exists', |
||
| 499 | new CodeLocation($this, $this->class), |
||
| 500 | 'resource' |
||
| 501 | ), |
||
| 502 | $this->getSuppressedIssues() |
||
| 503 | )) { |
||
| 504 | // fall through |
||
| 505 | } |
||
| 506 | } |
||
| 507 | } |
||
| 508 | } |
||
| 509 | |||
| 510 | if ($storage->mixin && $storage->mixin_declaring_fqcln === $storage->name) { |
||
| 511 | $union = new Type\Union([$storage->mixin]); |
||
| 512 | $union->check( |
||
| 513 | $this, |
||
| 514 | new CodeLocation( |
||
| 515 | $this, |
||
| 516 | $class->name ?: $class, |
||
| 517 | null, |
||
| 518 | true |
||
| 519 | ), |
||
| 520 | $this->getSuppressedIssues() |
||
| 521 | ); |
||
| 522 | } |
||
| 523 | |||
| 524 | if ($storage->template_type_extends) { |
||
| 525 | foreach ($storage->template_type_extends as $type_map) { |
||
| 526 | foreach ($type_map as $atomic_type) { |
||
| 527 | $atomic_type->check( |
||
| 528 | $this, |
||
| 529 | new CodeLocation( |
||
| 530 | $this, |
||
| 531 | $class->name ?: $class, |
||
| 532 | null, |
||
| 533 | true |
||
| 534 | ), |
||
| 535 | $this->getSuppressedIssues() |
||
| 536 | ); |
||
| 537 | } |
||
| 538 | } |
||
| 539 | } |
||
| 540 | |||
| 541 | if ($storage->invalid_dependencies) { |
||
| 542 | return; |
||
| 543 | } |
||
| 544 | |||
| 545 | $class_interfaces = $storage->class_implements; |
||
| 546 | |||
| 547 | foreach ($class_interfaces as $interface_name) { |
||
| 548 | try { |
||
| 549 | $interface_storage = $classlike_storage_provider->get($interface_name); |
||
| 550 | } catch (\InvalidArgumentException $e) { |
||
| 551 | continue; |
||
| 552 | } |
||
| 553 | |||
| 554 | $code_location = new CodeLocation( |
||
| 555 | $this, |
||
| 556 | $class->name ? $class->name : $class, |
||
| 557 | $class_context ? $class_context->include_location : null, |
||
| 558 | true |
||
| 559 | ); |
||
| 560 | |||
| 561 | if ($interface_storage->deprecated) { |
||
| 562 | if (IssueBuffer::accepts( |
||
| 563 | new DeprecatedInterface( |
||
| 564 | $interface_name . ' is marked deprecated', |
||
| 565 | $code_location, |
||
| 566 | $interface_name |
||
| 567 | ), |
||
| 568 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 569 | )) { |
||
| 570 | // fall through |
||
| 571 | } |
||
| 572 | } |
||
| 573 | |||
| 574 | if ($interface_storage->external_mutation_free |
||
| 575 | && !$storage->external_mutation_free |
||
| 576 | ) { |
||
| 577 | if (IssueBuffer::accepts( |
||
| 578 | new MissingImmutableAnnotation( |
||
| 579 | $interface_name . ' is marked immutable, but ' |
||
| 580 | . $fq_class_name . ' is not marked immutable', |
||
| 581 | $code_location |
||
| 582 | ), |
||
| 583 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 584 | )) { |
||
| 585 | // fall through |
||
| 586 | } |
||
| 587 | } |
||
| 588 | |||
| 589 | foreach ($interface_storage->methods as $interface_method_name_lc => $interface_method_storage) { |
||
| 590 | if ($interface_method_storage->visibility === self::VISIBILITY_PUBLIC) { |
||
| 591 | $implementer_declaring_method_id = $codebase->methods->getDeclaringMethodId( |
||
| 592 | new \Psalm\Internal\MethodIdentifier( |
||
| 593 | $this->fq_class_name, |
||
| 594 | $interface_method_name_lc |
||
| 595 | ) |
||
| 596 | ); |
||
| 597 | |||
| 598 | $implementer_method_storage = null; |
||
| 599 | $implementer_classlike_storage = null; |
||
| 600 | |||
| 601 | if ($implementer_declaring_method_id) { |
||
| 602 | $implementer_fq_class_name = $implementer_declaring_method_id->fq_class_name; |
||
| 603 | $implementer_method_storage = $codebase->methods->getStorage( |
||
| 604 | $implementer_declaring_method_id |
||
| 605 | ); |
||
| 606 | $implementer_classlike_storage = $classlike_storage_provider->get( |
||
| 607 | $implementer_fq_class_name |
||
| 608 | ); |
||
| 609 | } |
||
| 610 | |||
| 611 | if (!$implementer_method_storage) { |
||
| 612 | if (IssueBuffer::accepts( |
||
| 613 | new UnimplementedInterfaceMethod( |
||
| 614 | 'Method ' . $interface_method_name_lc . ' is not defined on class ' . |
||
| 615 | $storage->name, |
||
| 616 | $code_location |
||
| 617 | ), |
||
| 618 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 619 | )) { |
||
| 620 | return false; |
||
| 621 | } |
||
| 622 | |||
| 623 | return null; |
||
| 624 | } |
||
| 625 | |||
| 626 | $implementer_appearing_method_id = $codebase->methods->getAppearingMethodId( |
||
| 627 | new \Psalm\Internal\MethodIdentifier( |
||
| 628 | $this->fq_class_name, |
||
| 629 | $interface_method_name_lc |
||
| 630 | ) |
||
| 631 | ); |
||
| 632 | |||
| 633 | $implementer_visibility = $implementer_method_storage->visibility; |
||
| 634 | |||
| 635 | if ($implementer_appearing_method_id |
||
| 636 | && $implementer_appearing_method_id !== $implementer_declaring_method_id |
||
| 637 | ) { |
||
| 638 | $appearing_fq_class_name = $implementer_appearing_method_id->fq_class_name; |
||
| 639 | $appearing_method_name = $implementer_appearing_method_id->method_name; |
||
| 640 | |||
| 641 | $appearing_class_storage = $classlike_storage_provider->get( |
||
| 642 | $appearing_fq_class_name |
||
| 643 | ); |
||
| 644 | |||
| 645 | if (isset($appearing_class_storage->trait_visibility_map[$appearing_method_name])) { |
||
| 646 | $implementer_visibility |
||
| 647 | = $appearing_class_storage->trait_visibility_map[$appearing_method_name]; |
||
| 648 | } |
||
| 649 | } |
||
| 650 | |||
| 651 | if ($implementer_visibility !== self::VISIBILITY_PUBLIC) { |
||
| 652 | if (IssueBuffer::accepts( |
||
| 653 | new InaccessibleMethod( |
||
| 654 | 'Interface-defined method ' . $implementer_method_storage->cased_name |
||
| 655 | . ' must be public in ' . $storage->name, |
||
| 656 | $code_location |
||
| 657 | ), |
||
| 658 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 659 | )) { |
||
| 660 | return false; |
||
| 661 | } |
||
| 662 | |||
| 663 | return null; |
||
| 664 | } |
||
| 665 | |||
| 666 | if ($interface_method_storage->is_static && !$implementer_method_storage->is_static) { |
||
| 667 | if (IssueBuffer::accepts( |
||
| 668 | new MethodSignatureMismatch( |
||
| 669 | 'Method ' . $implementer_method_storage->cased_name |
||
| 670 | . ' should be static like ' |
||
| 671 | . $storage->name . '::' . $interface_method_storage->cased_name, |
||
| 672 | $code_location |
||
| 673 | ), |
||
| 674 | $implementer_method_storage->suppressed_issues |
||
| 675 | )) { |
||
| 676 | return false; |
||
| 677 | } |
||
| 678 | } |
||
| 679 | |||
| 680 | if ($storage->abstract && $implementer_method_storage === $interface_method_storage) { |
||
| 681 | continue; |
||
| 682 | } |
||
| 683 | |||
| 684 | MethodComparator::compare( |
||
| 685 | $codebase, |
||
| 686 | $implementer_classlike_storage ?: $storage, |
||
| 687 | $interface_storage, |
||
| 688 | $implementer_method_storage, |
||
| 689 | $interface_method_storage, |
||
| 690 | $this->fq_class_name, |
||
| 691 | $implementer_visibility, |
||
| 692 | $code_location, |
||
| 693 | $implementer_method_storage->suppressed_issues, |
||
| 694 | false |
||
| 695 | ); |
||
| 696 | } |
||
| 697 | } |
||
| 698 | } |
||
| 699 | |||
| 700 | if (!$class_context) { |
||
| 701 | $class_context = new Context($this->fq_class_name); |
||
| 702 | $class_context->parent = $parent_fq_class_name; |
||
| 703 | } |
||
| 704 | |||
| 705 | if ($global_context) { |
||
| 706 | $class_context->strict_types = $global_context->strict_types; |
||
| 707 | } |
||
| 708 | |||
| 709 | if ($this->leftover_stmts) { |
||
| 710 | (new StatementsAnalyzer( |
||
| 711 | $this, |
||
| 712 | new \Psalm\Internal\Provider\NodeDataProvider() |
||
| 713 | ))->analyze( |
||
| 714 | $this->leftover_stmts, |
||
| 715 | $class_context |
||
| 716 | ); |
||
| 717 | } |
||
| 718 | |||
| 719 | if (!$storage->abstract) { |
||
| 720 | foreach ($storage->declaring_method_ids as $declaring_method_id) { |
||
| 721 | $method_storage = $codebase->methods->getStorage($declaring_method_id); |
||
| 722 | |||
| 723 | $declaring_class_name = $declaring_method_id->fq_class_name; |
||
| 724 | $method_name_lc = $declaring_method_id->method_name; |
||
| 725 | |||
| 726 | if ($method_storage->abstract) { |
||
| 727 | if (IssueBuffer::accepts( |
||
| 728 | new UnimplementedAbstractMethod( |
||
| 729 | 'Method ' . $method_name_lc . ' is not defined on class ' . |
||
| 730 | $this->fq_class_name . ', defined abstract in ' . $declaring_class_name, |
||
| 731 | new CodeLocation( |
||
| 732 | $this, |
||
| 733 | $class->name ? $class->name : $class, |
||
| 734 | $class_context->include_location, |
||
| 735 | true |
||
| 736 | ) |
||
| 737 | ), |
||
| 738 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 739 | )) { |
||
| 740 | return false; |
||
| 741 | } |
||
| 742 | } |
||
| 743 | } |
||
| 744 | } |
||
| 745 | |||
| 746 | self::addContextProperties( |
||
| 747 | $this, |
||
| 748 | $storage, |
||
| 749 | $class_context, |
||
| 750 | $this->fq_class_name, |
||
| 751 | $this->parent_fq_class_name |
||
| 752 | ); |
||
| 753 | |||
| 754 | $constructor_analyzer = null; |
||
| 755 | $member_stmts = []; |
||
| 756 | |||
| 757 | foreach ($class->stmts as $stmt) { |
||
| 758 | if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) { |
||
| 759 | $method_analyzer = $this->analyzeClassMethod( |
||
| 760 | $stmt, |
||
| 761 | $storage, |
||
| 762 | $this, |
||
| 763 | $class_context, |
||
| 764 | $global_context |
||
| 765 | ); |
||
| 766 | |||
| 767 | if ($stmt->name->name === '__construct') { |
||
| 768 | $constructor_analyzer = $method_analyzer; |
||
| 769 | } |
||
| 770 | } elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) { |
||
| 771 | if ($this->analyzeTraitUse( |
||
| 772 | $this->source->getAliases(), |
||
| 773 | $stmt, |
||
| 774 | $project_analyzer, |
||
| 775 | $storage, |
||
| 776 | $class_context, |
||
| 777 | $global_context, |
||
| 778 | $constructor_analyzer |
||
| 779 | ) === false) { |
||
| 780 | return false; |
||
| 781 | } |
||
| 782 | } elseif ($stmt instanceof PhpParser\Node\Stmt\Property) { |
||
| 783 | foreach ($stmt->props as $prop) { |
||
| 784 | if ($prop->default) { |
||
| 785 | $member_stmts[] = $stmt; |
||
| 786 | } |
||
| 787 | |||
| 788 | if ($codebase->alter_code) { |
||
| 789 | $property_id = strtolower($this->fq_class_name) . '::$' . $prop->name; |
||
| 790 | |||
| 791 | $property_storage = $codebase->properties->getStorage($property_id); |
||
| 792 | |||
| 793 | if ($property_storage->type |
||
| 794 | && $property_storage->type_location |
||
| 795 | && $property_storage->type_location !== $property_storage->signature_type_location |
||
| 796 | ) { |
||
| 797 | $replace_type = \Psalm\Internal\Type\TypeExpander::expandUnion( |
||
| 798 | $codebase, |
||
| 799 | $property_storage->type, |
||
| 800 | $this->getFQCLN(), |
||
| 801 | $this->getFQCLN(), |
||
| 802 | $this->getParentFQCLN() |
||
| 803 | ); |
||
| 804 | |||
| 805 | $codebase->classlikes->handleDocblockTypeInMigration( |
||
| 806 | $codebase, |
||
| 807 | $this, |
||
| 808 | $replace_type, |
||
| 809 | $property_storage->type_location, |
||
| 810 | null |
||
| 811 | ); |
||
| 812 | } |
||
| 813 | |||
| 814 | foreach ($codebase->properties_to_rename as $original_property_id => $new_property_name) { |
||
| 815 | if ($property_id === $original_property_id) { |
||
| 816 | $file_manipulations = [ |
||
| 817 | new \Psalm\FileManipulation( |
||
| 818 | (int) $prop->name->getAttribute('startFilePos'), |
||
| 819 | (int) $prop->name->getAttribute('endFilePos') + 1, |
||
| 820 | '$' . $new_property_name |
||
| 821 | ) |
||
| 822 | ]; |
||
| 823 | |||
| 824 | \Psalm\Internal\FileManipulation\FileManipulationBuffer::add( |
||
| 825 | $this->getFilePath(), |
||
| 826 | $file_manipulations |
||
| 827 | ); |
||
| 828 | } |
||
| 829 | } |
||
| 830 | } |
||
| 831 | } |
||
| 832 | } elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) { |
||
| 833 | $member_stmts[] = $stmt; |
||
| 834 | |||
| 835 | foreach ($stmt->consts as $const) { |
||
| 836 | $const_id = strtolower($this->fq_class_name) . '::' . $const->name; |
||
| 837 | |||
| 838 | foreach ($codebase->class_constants_to_rename as $original_const_id => $new_const_name) { |
||
| 839 | if ($const_id === $original_const_id) { |
||
| 840 | $file_manipulations = [ |
||
| 841 | new \Psalm\FileManipulation( |
||
| 842 | (int) $const->name->getAttribute('startFilePos'), |
||
| 843 | (int) $const->name->getAttribute('endFilePos') + 1, |
||
| 844 | $new_const_name |
||
| 845 | ) |
||
| 846 | ]; |
||
| 847 | |||
| 848 | \Psalm\Internal\FileManipulation\FileManipulationBuffer::add( |
||
| 849 | $this->getFilePath(), |
||
| 850 | $file_manipulations |
||
| 851 | ); |
||
| 852 | } |
||
| 853 | } |
||
| 854 | } |
||
| 855 | } |
||
| 856 | } |
||
| 857 | |||
| 858 | $statements_analyzer = new StatementsAnalyzer($this, new \Psalm\Internal\Provider\NodeDataProvider()); |
||
| 859 | $statements_analyzer->analyze($member_stmts, $class_context, $global_context, true); |
||
| 860 | |||
| 861 | $config = Config::getInstance(); |
||
| 862 | |||
| 863 | $this->checkPropertyInitialization( |
||
| 864 | $codebase, |
||
| 865 | $config, |
||
| 866 | $storage, |
||
| 867 | $class_context, |
||
| 868 | $global_context, |
||
| 869 | $constructor_analyzer |
||
| 870 | ); |
||
| 871 | |||
| 872 | foreach ($class->stmts as $stmt) { |
||
| 873 | if ($stmt instanceof PhpParser\Node\Stmt\Property && !isset($stmt->type)) { |
||
| 874 | $this->checkForMissingPropertyType($this, $stmt, $class_context); |
||
| 875 | } elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) { |
||
| 876 | foreach ($stmt->traits as $trait) { |
||
| 877 | $fq_trait_name = self::getFQCLNFromNameObject( |
||
| 878 | $trait, |
||
| 879 | $this->source->getAliases() |
||
| 880 | ); |
||
| 881 | |||
| 882 | try { |
||
| 883 | $trait_file_analyzer = $project_analyzer->getFileAnalyzerForClassLike($fq_trait_name); |
||
| 884 | } catch (\Exception $e) { |
||
| 885 | continue; |
||
| 886 | } |
||
| 887 | |||
| 888 | $trait_storage = $codebase->classlike_storage_provider->get($fq_trait_name); |
||
| 889 | $trait_node = $codebase->classlikes->getTraitNode($fq_trait_name); |
||
| 890 | $trait_aliases = $trait_storage->aliases; |
||
| 891 | |||
| 892 | if ($trait_aliases === null) { |
||
| 893 | continue; |
||
| 894 | } |
||
| 895 | |||
| 896 | $trait_analyzer = new TraitAnalyzer( |
||
| 897 | $trait_node, |
||
| 898 | $trait_file_analyzer, |
||
| 899 | $fq_trait_name, |
||
| 900 | $trait_aliases |
||
| 901 | ); |
||
| 902 | |||
| 903 | $fq_trait_name_lc = strtolower($fq_trait_name); |
||
| 904 | |||
| 905 | if (isset($storage->template_type_uses_count[$fq_trait_name_lc])) { |
||
| 906 | $expected_param_count = $storage->template_type_uses_count[$fq_trait_name_lc]; |
||
| 907 | |||
| 908 | $this->checkTemplateParams( |
||
| 909 | $codebase, |
||
| 910 | $storage, |
||
| 911 | $trait_storage, |
||
| 912 | new CodeLocation( |
||
| 913 | $this, |
||
| 914 | $trait |
||
| 915 | ), |
||
| 916 | $expected_param_count |
||
| 917 | ); |
||
| 918 | } |
||
| 919 | |||
| 920 | foreach ($trait_node->stmts as $trait_stmt) { |
||
| 921 | if ($trait_stmt instanceof PhpParser\Node\Stmt\Property) { |
||
| 922 | $this->checkForMissingPropertyType($trait_analyzer, $trait_stmt, $class_context); |
||
| 923 | } |
||
| 924 | } |
||
| 925 | |||
| 926 | $trait_file_analyzer->clearSourceBeforeDestruction(); |
||
| 927 | } |
||
| 928 | } |
||
| 929 | } |
||
| 930 | |||
| 931 | $pseudo_methods = $storage->pseudo_methods + $storage->pseudo_static_methods; |
||
| 932 | |||
| 933 | foreach ($pseudo_methods as $pseudo_method_name => $pseudo_method_storage) { |
||
| 934 | $pseudo_method_id = new \Psalm\Internal\MethodIdentifier( |
||
| 935 | $this->fq_class_name, |
||
| 936 | $pseudo_method_name |
||
| 937 | ); |
||
| 938 | |||
| 939 | $overridden_method_ids = $codebase->methods->getOverriddenMethodIds($pseudo_method_id); |
||
| 940 | |||
| 941 | if ($overridden_method_ids |
||
| 942 | && $pseudo_method_name !== '__construct' |
||
| 943 | && $pseudo_method_storage->location |
||
| 944 | ) { |
||
| 945 | foreach ($overridden_method_ids as $overridden_method_id) { |
||
| 946 | $parent_method_storage = $codebase->methods->getStorage($overridden_method_id); |
||
| 947 | |||
| 948 | $overridden_fq_class_name = $overridden_method_id->fq_class_name; |
||
| 949 | |||
| 950 | $parent_storage = $classlike_storage_provider->get($overridden_fq_class_name); |
||
| 951 | |||
| 952 | MethodComparator::compare( |
||
| 953 | $codebase, |
||
| 954 | $storage, |
||
| 955 | $parent_storage, |
||
| 956 | $pseudo_method_storage, |
||
| 957 | $parent_method_storage, |
||
| 958 | $this->fq_class_name, |
||
| 959 | $pseudo_method_storage->visibility ?: 0, |
||
| 960 | $storage->location ?: $pseudo_method_storage->location, |
||
| 961 | $storage->suppressed_issues, |
||
| 962 | true, |
||
| 963 | false |
||
| 964 | ); |
||
| 965 | } |
||
| 966 | } |
||
| 967 | } |
||
| 968 | |||
| 969 | $plugin_classes = $codebase->config->after_classlike_checks; |
||
| 970 | |||
| 971 | if ($plugin_classes) { |
||
| 972 | $file_manipulations = []; |
||
| 973 | |||
| 974 | foreach ($plugin_classes as $plugin_fq_class_name) { |
||
| 975 | if ($plugin_fq_class_name::afterStatementAnalysis( |
||
| 976 | $class, |
||
| 977 | $storage, |
||
| 978 | $this, |
||
| 979 | $codebase, |
||
| 980 | $file_manipulations |
||
| 981 | ) === false) { |
||
| 982 | return false; |
||
| 983 | } |
||
| 984 | } |
||
| 985 | |||
| 986 | if ($file_manipulations) { |
||
| 987 | \Psalm\Internal\FileManipulation\FileManipulationBuffer::add( |
||
| 988 | $this->getFilePath(), |
||
| 989 | $file_manipulations |
||
| 990 | ); |
||
| 991 | } |
||
| 992 | } |
||
| 993 | } |
||
| 994 | |||
| 2199 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.