| Conditions | 153 |
| Paths | > 20000 |
| Total Lines | 714 |
| 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 |
||
| 122 | public function analyze( |
||
| 123 | Context $context, |
||
| 124 | \Psalm\Internal\Provider\NodeDataProvider $type_provider, |
||
| 125 | Context $global_context = null, |
||
| 126 | $add_mutations = false, |
||
| 127 | array $byref_uses = null |
||
| 128 | ) { |
||
| 129 | $storage = $this->storage; |
||
| 130 | |||
| 131 | $function_stmts = $this->function->getStmts() ?: []; |
||
| 132 | |||
| 133 | $hash = null; |
||
| 134 | $real_method_id = null; |
||
| 135 | $method_id = null; |
||
| 136 | |||
| 137 | $cased_method_id = null; |
||
| 138 | |||
| 139 | $appearing_class_storage = null; |
||
| 140 | |||
| 141 | if ($global_context) { |
||
| 142 | foreach ($global_context->constants as $const_name => $var_type) { |
||
| 143 | if (!$context->hasVariable($const_name)) { |
||
| 144 | $context->vars_in_scope[$const_name] = clone $var_type; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | $codebase = $this->codebase; |
||
| 150 | $project_analyzer = $this->getProjectAnalyzer(); |
||
| 151 | |||
| 152 | $implemented_docblock_param_types = []; |
||
| 153 | |||
| 154 | $classlike_storage_provider = $codebase->classlike_storage_provider; |
||
| 155 | |||
| 156 | if ($codebase->track_unused_suppressions && !isset($storage->suppressed_issues[0])) { |
||
| 157 | foreach ($storage->suppressed_issues as $offset => $issue_name) { |
||
| 158 | IssueBuffer::addUnusedSuppression($this->getFilePath(), $offset, $issue_name); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | foreach ($storage->docblock_issues as $docblock_issue) { |
||
| 163 | IssueBuffer::add($docblock_issue); |
||
| 164 | } |
||
| 165 | |||
| 166 | $overridden_method_ids = []; |
||
| 167 | |||
| 168 | if ($this->function instanceof ClassMethod) { |
||
| 169 | if (!$storage instanceof MethodStorage || !$this instanceof MethodAnalyzer) { |
||
| 170 | throw new \UnexpectedValueException('$storage must be MethodStorage'); |
||
| 171 | } |
||
| 172 | |||
| 173 | $real_method_id = $this->getMethodId(); |
||
|
|
|||
| 174 | |||
| 175 | $method_id = $this->getMethodId($context->self); |
||
| 176 | |||
| 177 | $fq_class_name = (string)$context->self; |
||
| 178 | $appearing_class_storage = $classlike_storage_provider->get($fq_class_name); |
||
| 179 | |||
| 180 | if ($add_mutations) { |
||
| 181 | if (!$context->collect_initializations) { |
||
| 182 | $hash = md5($real_method_id . '::' . $context->getScopeSummary()); |
||
| 183 | |||
| 184 | // if we know that the function has no effects on vars, we don't bother rechecking |
||
| 185 | if (isset(self::$no_effects_hashes[$hash])) { |
||
| 186 | return null; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } elseif ($context->self) { |
||
| 190 | if ($appearing_class_storage->template_types) { |
||
| 191 | $template_params = []; |
||
| 192 | |||
| 193 | foreach ($appearing_class_storage->template_types as $param_name => $template_map) { |
||
| 194 | $key = array_keys($template_map)[0]; |
||
| 195 | |||
| 196 | $template_params[] = new Type\Union([ |
||
| 197 | new Type\Atomic\TTemplateParam( |
||
| 198 | $param_name, |
||
| 199 | \reset($template_map)[0], |
||
| 200 | $key |
||
| 201 | ) |
||
| 202 | ]); |
||
| 203 | } |
||
| 204 | |||
| 205 | $this_object_type = new Type\Atomic\TGenericObject( |
||
| 206 | $context->self, |
||
| 207 | $template_params |
||
| 208 | ); |
||
| 209 | $this_object_type->was_static = true; |
||
| 210 | } else { |
||
| 211 | $this_object_type = new TNamedObject($context->self); |
||
| 212 | $this_object_type->was_static = true; |
||
| 213 | } |
||
| 214 | |||
| 215 | $context->vars_in_scope['$this'] = new Type\Union([$this_object_type]); |
||
| 216 | |||
| 217 | if ($storage->external_mutation_free |
||
| 218 | && !$storage->mutation_free_inferred |
||
| 219 | ) { |
||
| 220 | $context->vars_in_scope['$this']->reference_free = true; |
||
| 221 | |||
| 222 | if ($this->function->name->name !== '__construct') { |
||
| 223 | $context->vars_in_scope['$this']->allow_mutations = false; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | $context->vars_possibly_in_scope['$this'] = true; |
||
| 228 | } |
||
| 229 | |||
| 230 | if ($appearing_class_storage->has_visitor_issues) { |
||
| 231 | return null; |
||
| 232 | } |
||
| 233 | |||
| 234 | $cased_method_id = $fq_class_name . '::' . $storage->cased_name; |
||
| 235 | |||
| 236 | $overridden_method_ids = $codebase->methods->getOverriddenMethodIds($method_id); |
||
| 237 | |||
| 238 | if ($this->function->name->name === '__construct') { |
||
| 239 | $context->inside_constructor = true; |
||
| 240 | } |
||
| 241 | |||
| 242 | $codeLocation = new CodeLocation( |
||
| 243 | $this, |
||
| 244 | $this->function, |
||
| 245 | null, |
||
| 246 | true |
||
| 247 | ); |
||
| 248 | |||
| 249 | if ($overridden_method_ids |
||
| 250 | && $this->function->name->name !== '__construct' |
||
| 251 | && !$context->collect_initializations |
||
| 252 | && !$context->collect_mutations |
||
| 253 | ) { |
||
| 254 | foreach ($overridden_method_ids as $overridden_method_id) { |
||
| 255 | $parent_method_storage = $codebase->methods->getStorage($overridden_method_id); |
||
| 256 | |||
| 257 | $overridden_fq_class_name = $overridden_method_id->fq_class_name; |
||
| 258 | |||
| 259 | $parent_storage = $classlike_storage_provider->get($overridden_fq_class_name); |
||
| 260 | |||
| 261 | $implementer_visibility = $storage->visibility; |
||
| 262 | |||
| 263 | $implementer_appearing_method_id = $codebase->methods->getAppearingMethodId($method_id); |
||
| 264 | $implementer_declaring_method_id = $real_method_id; |
||
| 265 | |||
| 266 | $declaring_class_storage = $appearing_class_storage; |
||
| 267 | |||
| 268 | if ($implementer_appearing_method_id |
||
| 269 | && $implementer_appearing_method_id !== $implementer_declaring_method_id |
||
| 270 | ) { |
||
| 271 | $appearing_fq_class_name = $implementer_appearing_method_id->fq_class_name; |
||
| 272 | $appearing_method_name = $implementer_appearing_method_id->method_name; |
||
| 273 | |||
| 274 | $declaring_fq_class_name = $implementer_declaring_method_id->fq_class_name; |
||
| 275 | |||
| 276 | $appearing_class_storage = $classlike_storage_provider->get( |
||
| 277 | $appearing_fq_class_name |
||
| 278 | ); |
||
| 279 | |||
| 280 | $declaring_class_storage = $classlike_storage_provider->get( |
||
| 281 | $declaring_fq_class_name |
||
| 282 | ); |
||
| 283 | |||
| 284 | if (isset($appearing_class_storage->trait_visibility_map[$appearing_method_name])) { |
||
| 285 | $implementer_visibility |
||
| 286 | = $appearing_class_storage->trait_visibility_map[$appearing_method_name]; |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | // we've already checked this in the class checker |
||
| 291 | if (!isset($appearing_class_storage->class_implements[strtolower($overridden_fq_class_name)])) { |
||
| 292 | MethodComparator::compare( |
||
| 293 | $codebase, |
||
| 294 | $declaring_class_storage, |
||
| 295 | $parent_storage, |
||
| 296 | $storage, |
||
| 297 | $parent_method_storage, |
||
| 298 | $fq_class_name, |
||
| 299 | $implementer_visibility, |
||
| 300 | $codeLocation, |
||
| 301 | $storage->suppressed_issues |
||
| 302 | ); |
||
| 303 | } |
||
| 304 | |||
| 305 | foreach ($parent_method_storage->params as $i => $guide_param) { |
||
| 306 | if ($guide_param->type |
||
| 307 | && (!$guide_param->signature_type |
||
| 308 | || ($guide_param->signature_type !== $guide_param->type |
||
| 309 | && $storage->inheritdoc) |
||
| 310 | || !$parent_storage->user_defined |
||
| 311 | ) |
||
| 312 | ) { |
||
| 313 | if (!isset($implemented_docblock_param_types[$i])) { |
||
| 314 | $implemented_docblock_param_types[$i] = $guide_param->type; |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | MethodAnalyzer::checkMethodSignatureMustOmitReturnType($storage, $codeLocation); |
||
| 322 | |||
| 323 | if (!$context->calling_method_id || !$context->collect_initializations) { |
||
| 324 | $context->calling_method_id = strtolower((string) $method_id); |
||
| 325 | } |
||
| 326 | } elseif ($this->function instanceof Function_) { |
||
| 327 | $function_name = $this->function->name->name; |
||
| 328 | $namespace_prefix = $this->getNamespace(); |
||
| 329 | $cased_method_id = ($namespace_prefix !== null ? $namespace_prefix . '\\' : '') . $function_name; |
||
| 330 | $context->calling_function_id = strtolower($cased_method_id); |
||
| 331 | } else { // Closure |
||
| 332 | if ($storage->return_type) { |
||
| 333 | $closure_return_type = \Psalm\Internal\Type\TypeExpander::expandUnion( |
||
| 334 | $codebase, |
||
| 335 | $storage->return_type, |
||
| 336 | $context->self, |
||
| 337 | $context->self, |
||
| 338 | $this->getParentFQCLN() |
||
| 339 | ); |
||
| 340 | } else { |
||
| 341 | $closure_return_type = Type::getMixed(); |
||
| 342 | } |
||
| 343 | |||
| 344 | $closure_type = new Type\Atomic\TFn( |
||
| 345 | 'Closure', |
||
| 346 | $storage->params, |
||
| 347 | $closure_return_type |
||
| 348 | ); |
||
| 349 | |||
| 350 | if ($storage instanceof FunctionStorage) { |
||
| 351 | $closure_type->byref_uses = $storage->byref_uses; |
||
| 352 | } |
||
| 353 | |||
| 354 | $type_provider->setType( |
||
| 355 | $this->function, |
||
| 356 | new Type\Union([ |
||
| 357 | $closure_type, |
||
| 358 | ]) |
||
| 359 | ); |
||
| 360 | } |
||
| 361 | |||
| 362 | $this->suppressed_issues = $this->getSource()->getSuppressedIssues() + $storage->suppressed_issues; |
||
| 363 | |||
| 364 | if ($storage instanceof MethodStorage && $storage->is_static) { |
||
| 365 | $this->is_static = true; |
||
| 366 | } |
||
| 367 | |||
| 368 | $statements_analyzer = new StatementsAnalyzer($this, $type_provider); |
||
| 369 | |||
| 370 | if ($byref_uses) { |
||
| 371 | $statements_analyzer->setByRefUses($byref_uses); |
||
| 372 | } |
||
| 373 | |||
| 374 | if ($storage->template_types) { |
||
| 375 | foreach ($storage->template_types as $param_name => $_) { |
||
| 376 | $fq_classlike_name = Type::getFQCLNFromString( |
||
| 377 | $param_name, |
||
| 378 | $this->getAliases() |
||
| 379 | ); |
||
| 380 | |||
| 381 | if ($codebase->classOrInterfaceExists($fq_classlike_name)) { |
||
| 382 | if (IssueBuffer::accepts( |
||
| 383 | new ReservedWord( |
||
| 384 | 'Cannot use ' . $param_name . ' as template name since the class already exists', |
||
| 385 | new CodeLocation($this, $this->function), |
||
| 386 | 'resource' |
||
| 387 | ), |
||
| 388 | $this->getSuppressedIssues() |
||
| 389 | )) { |
||
| 390 | // fall through |
||
| 391 | } |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | $template_types = $storage->template_types; |
||
| 397 | |||
| 398 | if ($appearing_class_storage && $appearing_class_storage->template_types) { |
||
| 399 | $template_types = array_merge($template_types ?: [], $appearing_class_storage->template_types); |
||
| 400 | } |
||
| 401 | |||
| 402 | $params = $storage->params; |
||
| 403 | |||
| 404 | if ($storage instanceof MethodStorage) { |
||
| 405 | $non_null_param_types = array_filter( |
||
| 406 | $storage->params, |
||
| 407 | /** @return bool */ |
||
| 408 | function (FunctionLikeParameter $p) { |
||
| 409 | return $p->type !== null && $p->has_docblock_type; |
||
| 410 | } |
||
| 411 | ); |
||
| 412 | } else { |
||
| 413 | $non_null_param_types = array_filter( |
||
| 414 | $storage->params, |
||
| 415 | /** @return bool */ |
||
| 416 | function (FunctionLikeParameter $p) { |
||
| 417 | return $p->type !== null; |
||
| 418 | } |
||
| 419 | ); |
||
| 420 | } |
||
| 421 | |||
| 422 | if ($storage instanceof MethodStorage |
||
| 423 | && $method_id instanceof \Psalm\Internal\MethodIdentifier |
||
| 424 | && $overridden_method_ids |
||
| 425 | ) { |
||
| 426 | $types_without_docblocks = array_filter( |
||
| 427 | $storage->params, |
||
| 428 | /** @return bool */ |
||
| 429 | function (FunctionLikeParameter $p) { |
||
| 430 | return !$p->type || !$p->has_docblock_type; |
||
| 431 | } |
||
| 432 | ); |
||
| 433 | |||
| 434 | if ($types_without_docblocks) { |
||
| 435 | $params = $codebase->methods->getMethodParams( |
||
| 436 | $method_id, |
||
| 437 | $this |
||
| 438 | ); |
||
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 442 | if ($codebase->alter_code) { |
||
| 443 | $this->alterParams($codebase, $storage, $params, $context); |
||
| 444 | } |
||
| 445 | |||
| 446 | foreach ($codebase->methods_to_rename as $original_method_id => $new_method_name) { |
||
| 447 | if ($this->function instanceof ClassMethod |
||
| 448 | && $this instanceof MethodAnalyzer |
||
| 449 | && strtolower((string) $this->getMethodId()) === $original_method_id |
||
| 450 | ) { |
||
| 451 | $file_manipulations = [ |
||
| 452 | new \Psalm\FileManipulation( |
||
| 453 | (int) $this->function->name->getAttribute('startFilePos'), |
||
| 454 | (int) $this->function->name->getAttribute('endFilePos') + 1, |
||
| 455 | $new_method_name |
||
| 456 | ) |
||
| 457 | ]; |
||
| 458 | |||
| 459 | \Psalm\Internal\FileManipulation\FileManipulationBuffer::add( |
||
| 460 | $this->getFilePath(), |
||
| 461 | $file_manipulations |
||
| 462 | ); |
||
| 463 | } |
||
| 464 | } |
||
| 465 | |||
| 466 | $check_stmts = $this->processParams( |
||
| 467 | $statements_analyzer, |
||
| 468 | $storage, |
||
| 469 | $cased_method_id, |
||
| 470 | $params, |
||
| 471 | $context, |
||
| 472 | $implemented_docblock_param_types, |
||
| 473 | (bool) $non_null_param_types, |
||
| 474 | (bool) $template_types |
||
| 475 | ); |
||
| 476 | |||
| 477 | if ($storage->pure) { |
||
| 478 | $context->pure = true; |
||
| 479 | } |
||
| 480 | |||
| 481 | if ($storage->mutation_free |
||
| 482 | && $cased_method_id |
||
| 483 | && !strpos($cased_method_id, '__construct') |
||
| 484 | && !($storage instanceof MethodStorage && $storage->mutation_free_inferred) |
||
| 485 | ) { |
||
| 486 | $context->mutation_free = true; |
||
| 487 | } |
||
| 488 | |||
| 489 | if ($storage instanceof MethodStorage |
||
| 490 | && $storage->external_mutation_free |
||
| 491 | && !$storage->mutation_free_inferred |
||
| 492 | ) { |
||
| 493 | $context->external_mutation_free = true; |
||
| 494 | } |
||
| 495 | |||
| 496 | if ($storage->unused_docblock_params) { |
||
| 497 | foreach ($storage->unused_docblock_params as $param_name => $param_location) { |
||
| 498 | if (IssueBuffer::accepts( |
||
| 499 | new InvalidDocblockParamName( |
||
| 500 | 'Incorrect param name $' . $param_name . ' in docblock for ' . $cased_method_id, |
||
| 501 | $param_location |
||
| 502 | ) |
||
| 503 | )) { |
||
| 504 | } |
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | if ($storage->signature_return_type && $storage->signature_return_type_location) { |
||
| 509 | list($start, $end) = $storage->signature_return_type_location->getSelectionBounds(); |
||
| 510 | |||
| 511 | $codebase->analyzer->addOffsetReference( |
||
| 512 | $this->getFilePath(), |
||
| 513 | $start, |
||
| 514 | $end, |
||
| 515 | (string) $storage->signature_return_type |
||
| 516 | ); |
||
| 517 | } |
||
| 518 | |||
| 519 | if (ReturnTypeAnalyzer::checkReturnType( |
||
| 520 | $this->function, |
||
| 521 | $project_analyzer, |
||
| 522 | $this, |
||
| 523 | $storage, |
||
| 524 | $context |
||
| 525 | ) === false) { |
||
| 526 | $check_stmts = false; |
||
| 527 | } |
||
| 528 | |||
| 529 | if (!$check_stmts) { |
||
| 530 | return false; |
||
| 531 | } |
||
| 532 | |||
| 533 | if ($context->collect_initializations || $context->collect_mutations) { |
||
| 534 | $statements_analyzer->addSuppressedIssues([ |
||
| 535 | 'DocblockTypeContradiction', |
||
| 536 | 'InvalidReturnStatement', |
||
| 537 | 'RedundantCondition', |
||
| 538 | 'RedundantConditionGivenDocblockType', |
||
| 539 | 'TypeDoesNotContainNull', |
||
| 540 | 'TypeDoesNotContainType', |
||
| 541 | 'LoopInvalidation', |
||
| 542 | ]); |
||
| 543 | |||
| 544 | if ($context->collect_initializations) { |
||
| 545 | $statements_analyzer->addSuppressedIssues([ |
||
| 546 | 'UndefinedInterfaceMethod', |
||
| 547 | 'UndefinedMethod', |
||
| 548 | 'PossiblyUndefinedMethod', |
||
| 549 | ]); |
||
| 550 | } |
||
| 551 | } elseif ($cased_method_id && strpos($cased_method_id, '__destruct')) { |
||
| 552 | $statements_analyzer->addSuppressedIssues([ |
||
| 553 | 'InvalidPropertyAssignmentValue', |
||
| 554 | 'PossiblyNullPropertyAssignmentValue', |
||
| 555 | ]); |
||
| 556 | } |
||
| 557 | |||
| 558 | $statements_analyzer->analyze($function_stmts, $context, $global_context, true); |
||
| 559 | |||
| 560 | $this->examineParamTypes($statements_analyzer, $context, $codebase); |
||
| 561 | |||
| 562 | foreach ($storage->params as $offset => $function_param) { |
||
| 563 | // only complain if there's no type defined by a parent type |
||
| 564 | if (!$function_param->type |
||
| 565 | && $function_param->location |
||
| 566 | && !isset($implemented_docblock_param_types[$offset]) |
||
| 567 | ) { |
||
| 568 | if ($this->function instanceof Closure |
||
| 569 | || $this->function instanceof ArrowFunction |
||
| 570 | ) { |
||
| 571 | IssueBuffer::accepts( |
||
| 572 | new MissingClosureParamType( |
||
| 573 | 'Parameter $' . $function_param->name . ' has no provided type', |
||
| 574 | $function_param->location |
||
| 575 | ), |
||
| 576 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 577 | ); |
||
| 578 | } else { |
||
| 579 | IssueBuffer::accepts( |
||
| 580 | new MissingParamType( |
||
| 581 | 'Parameter $' . $function_param->name . ' has no provided type', |
||
| 582 | $function_param->location |
||
| 583 | ), |
||
| 584 | $storage->suppressed_issues + $this->getSuppressedIssues() |
||
| 585 | ); |
||
| 586 | } |
||
| 587 | } |
||
| 588 | } |
||
| 589 | |||
| 590 | if ($this->function instanceof Closure |
||
| 591 | || $this->function instanceof ArrowFunction |
||
| 592 | ) { |
||
| 593 | $this->verifyReturnType( |
||
| 594 | $function_stmts, |
||
| 595 | $statements_analyzer, |
||
| 596 | $storage->return_type, |
||
| 597 | $this->source->getFQCLN(), |
||
| 598 | $storage->return_type_location, |
||
| 599 | $context->has_returned, |
||
| 600 | $global_context && $global_context->inside_call |
||
| 601 | ); |
||
| 602 | |||
| 603 | $closure_yield_types = []; |
||
| 604 | |||
| 605 | $closure_return_types = ReturnTypeCollector::getReturnTypes( |
||
| 606 | $codebase, |
||
| 607 | $type_provider, |
||
| 608 | $function_stmts, |
||
| 609 | $closure_yield_types, |
||
| 610 | true |
||
| 611 | ); |
||
| 612 | |||
| 613 | $closure_return_type = $closure_return_types |
||
| 614 | ? \Psalm\Type::combineUnionTypeArray( |
||
| 615 | $closure_return_types, |
||
| 616 | $codebase |
||
| 617 | ) |
||
| 618 | : null; |
||
| 619 | |||
| 620 | $closure_yield_type = $closure_yield_types |
||
| 621 | ? \Psalm\Type::combineUnionTypeArray( |
||
| 622 | $closure_yield_types, |
||
| 623 | $codebase |
||
| 624 | ) |
||
| 625 | : null; |
||
| 626 | |||
| 627 | if ($closure_return_type || $closure_yield_type) { |
||
| 628 | if ($closure_yield_type) { |
||
| 629 | $closure_return_type = $closure_yield_type; |
||
| 630 | } |
||
| 631 | |||
| 632 | if (($storage->return_type === $storage->signature_return_type) |
||
| 633 | && (!$storage->return_type |
||
| 634 | || $storage->return_type->hasMixed() |
||
| 635 | || TypeAnalyzer::isContainedBy( |
||
| 636 | $codebase, |
||
| 637 | $closure_return_type, |
||
| 638 | $storage->return_type |
||
| 639 | )) |
||
| 640 | ) { |
||
| 641 | if ($function_type = $statements_analyzer->node_data->getType($this->function)) { |
||
| 642 | /** |
||
| 643 | * @var Type\Atomic\TFn |
||
| 644 | */ |
||
| 645 | $closure_atomic = \array_values($function_type->getAtomicTypes())[0]; |
||
| 646 | $closure_atomic->return_type = $closure_return_type; |
||
| 647 | } |
||
| 648 | } |
||
| 649 | } |
||
| 650 | } |
||
| 651 | |||
| 652 | if ($codebase->collect_references |
||
| 653 | && !$context->collect_initializations |
||
| 654 | && !$context->collect_mutations |
||
| 655 | && $codebase->find_unused_variables |
||
| 656 | && $context->check_variables |
||
| 657 | ) { |
||
| 658 | $this->checkParamReferences( |
||
| 659 | $statements_analyzer, |
||
| 660 | $storage, |
||
| 661 | $appearing_class_storage, |
||
| 662 | $context |
||
| 663 | ); |
||
| 664 | } |
||
| 665 | |||
| 666 | foreach ($storage->throws as $expected_exception => $_) { |
||
| 667 | if (($expected_exception === 'self' |
||
| 668 | || $expected_exception === 'static') |
||
| 669 | && $context->self |
||
| 670 | ) { |
||
| 671 | $expected_exception = $context->self; |
||
| 672 | } |
||
| 673 | |||
| 674 | if (isset($storage->throw_locations[$expected_exception])) { |
||
| 675 | if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName( |
||
| 676 | $statements_analyzer, |
||
| 677 | $expected_exception, |
||
| 678 | $storage->throw_locations[$expected_exception], |
||
| 679 | $context->self, |
||
| 680 | $context->calling_method_id, |
||
| 681 | $statements_analyzer->getSuppressedIssues(), |
||
| 682 | false, |
||
| 683 | false, |
||
| 684 | true, |
||
| 685 | true |
||
| 686 | )) { |
||
| 687 | $input_type = new Type\Union([new TNamedObject($expected_exception)]); |
||
| 688 | $container_type = new Type\Union([new TNamedObject('Exception'), new TNamedObject('Throwable')]); |
||
| 689 | |||
| 690 | if (!TypeAnalyzer::isContainedBy($codebase, $input_type, $container_type)) { |
||
| 691 | if (IssueBuffer::accepts( |
||
| 692 | new \Psalm\Issue\InvalidThrow( |
||
| 693 | 'Class supplied for @throws ' . $expected_exception |
||
| 694 | . ' does not implement Throwable', |
||
| 695 | $storage->throw_locations[$expected_exception], |
||
| 696 | $expected_exception |
||
| 697 | ), |
||
| 698 | $statements_analyzer->getSuppressedIssues() |
||
| 699 | )) { |
||
| 700 | // fall through |
||
| 701 | } |
||
| 702 | } |
||
| 703 | |||
| 704 | if ($codebase->alter_code) { |
||
| 705 | $codebase->classlikes->handleDocblockTypeInMigration( |
||
| 706 | $codebase, |
||
| 707 | $this, |
||
| 708 | $input_type, |
||
| 709 | $storage->throw_locations[$expected_exception], |
||
| 710 | $context->calling_method_id |
||
| 711 | ); |
||
| 712 | } |
||
| 713 | } |
||
| 714 | } |
||
| 715 | } |
||
| 716 | |||
| 717 | foreach ($statements_analyzer->getUncaughtThrows($context) as $possibly_thrown_exception => $codelocations) { |
||
| 718 | $is_expected = false; |
||
| 719 | |||
| 720 | foreach ($storage->throws as $expected_exception => $_) { |
||
| 721 | if ($expected_exception === $possibly_thrown_exception |
||
| 722 | || $codebase->classExtendsOrImplements($possibly_thrown_exception, $expected_exception) |
||
| 723 | ) { |
||
| 724 | $is_expected = true; |
||
| 725 | break; |
||
| 726 | } |
||
| 727 | } |
||
| 728 | |||
| 729 | if (!$is_expected) { |
||
| 730 | foreach ($codelocations as $codelocation) { |
||
| 731 | // issues are suppressed in ThrowAnalyzer, CallAnalyzer, etc. |
||
| 732 | if (IssueBuffer::accepts( |
||
| 733 | new MissingThrowsDocblock( |
||
| 734 | $possibly_thrown_exception . ' is thrown but not caught - please either catch' |
||
| 735 | . ' or add a @throws annotation', |
||
| 736 | $codelocation |
||
| 737 | ) |
||
| 738 | )) { |
||
| 739 | // fall through |
||
| 740 | } |
||
| 741 | } |
||
| 742 | } |
||
| 743 | } |
||
| 744 | |||
| 745 | if ($codebase->taint |
||
| 746 | && $this->function instanceof ClassMethod |
||
| 747 | && $cased_method_id |
||
| 748 | && $storage->specialize_call |
||
| 749 | && isset($context->vars_in_scope['$this']) |
||
| 750 | && $context->vars_in_scope['$this']->parent_nodes |
||
| 751 | ) { |
||
| 752 | $method_source = TaintNode::getForMethodReturn( |
||
| 753 | (string) $method_id, |
||
| 754 | $cased_method_id, |
||
| 755 | $storage->location |
||
| 756 | ); |
||
| 757 | |||
| 758 | $codebase->taint->addTaintNode($method_source); |
||
| 759 | |||
| 760 | foreach ($context->vars_in_scope['$this']->parent_nodes as $parent_node) { |
||
| 761 | $codebase->taint->addPath( |
||
| 762 | $parent_node, |
||
| 763 | $method_source, |
||
| 764 | '$this' |
||
| 765 | ); |
||
| 766 | } |
||
| 767 | } |
||
| 768 | |||
| 769 | if ($add_mutations) { |
||
| 770 | if ($this->return_vars_in_scope !== null) { |
||
| 771 | $context->vars_in_scope = TypeAnalyzer::combineKeyedTypes( |
||
| 772 | $context->vars_in_scope, |
||
| 773 | $this->return_vars_in_scope |
||
| 774 | ); |
||
| 775 | } |
||
| 776 | |||
| 777 | if ($this->return_vars_possibly_in_scope !== null) { |
||
| 778 | $context->vars_possibly_in_scope = array_merge( |
||
| 779 | $context->vars_possibly_in_scope, |
||
| 780 | $this->return_vars_possibly_in_scope |
||
| 781 | ); |
||
| 782 | } |
||
| 783 | |||
| 784 | foreach ($context->vars_in_scope as $var => $_) { |
||
| 785 | if (strpos($var, '$this->') !== 0 && $var !== '$this') { |
||
| 786 | unset($context->vars_in_scope[$var]); |
||
| 787 | } |
||
| 788 | } |
||
| 789 | |||
| 790 | foreach ($context->vars_possibly_in_scope as $var => $_) { |
||
| 791 | if (strpos($var, '$this->') !== 0 && $var !== '$this') { |
||
| 792 | unset($context->vars_possibly_in_scope[$var]); |
||
| 793 | } |
||
| 794 | } |
||
| 795 | |||
| 796 | if ($hash |
||
| 797 | && $real_method_id |
||
| 798 | && $this instanceof MethodAnalyzer |
||
| 799 | && !$context->collect_initializations |
||
| 800 | ) { |
||
| 801 | $new_hash = md5($real_method_id . '::' . $context->getScopeSummary()); |
||
| 802 | |||
| 803 | if ($new_hash === $hash) { |
||
| 804 | self::$no_effects_hashes[$hash] = true; |
||
| 805 | } |
||
| 806 | } |
||
| 807 | } |
||
| 808 | |||
| 809 | $plugin_classes = $codebase->config->after_functionlike_checks; |
||
| 810 | |||
| 811 | if ($plugin_classes) { |
||
| 812 | $file_manipulations = []; |
||
| 813 | |||
| 814 | foreach ($plugin_classes as $plugin_fq_class_name) { |
||
| 815 | if ($plugin_fq_class_name::afterStatementAnalysis( |
||
| 816 | $this->function, |
||
| 817 | $storage, |
||
| 818 | $this, |
||
| 819 | $codebase, |
||
| 820 | $file_manipulations |
||
| 821 | ) === false) { |
||
| 822 | return false; |
||
| 823 | } |
||
| 824 | } |
||
| 825 | |||
| 826 | if ($file_manipulations) { |
||
| 827 | \Psalm\Internal\FileManipulation\FileManipulationBuffer::add( |
||
| 828 | $this->getFilePath(), |
||
| 829 | $file_manipulations |
||
| 830 | ); |
||
| 831 | } |
||
| 832 | } |
||
| 833 | |||
| 834 | return null; |
||
| 835 | } |
||
| 836 | |||
| 1809 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: