mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Improve detection of fluent methods
This commit is contained in:
parent
23b7ff86b7
commit
b678461f5c
@ -1884,9 +1884,7 @@ class ClassLikes
|
||||
&& !$method_storage->return_type->isVoid()
|
||||
&& !$method_storage->return_type->isNever()
|
||||
&& $method_id->method_name !== '__tostring'
|
||||
&& ($method_storage->is_static
|
||||
|| !($method_storage->return_type->getId() === 'static'
|
||||
|| $method_storage->return_type->isFormerStaticObject()))
|
||||
&& ($method_storage->is_static || !$method_storage->probably_fluent)
|
||||
) {
|
||||
$method_return_referenced = $this->file_reference_provider->isMethodReturnReferenced(
|
||||
strtolower((string) $method_id)
|
||||
|
@ -341,6 +341,20 @@ class FunctionLikeNodeScanner
|
||||
|
||||
$storage->assertions = $var_assertions;
|
||||
}
|
||||
|
||||
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod
|
||||
&& $stmt->stmts
|
||||
&& $storage instanceof MethodStorage
|
||||
) {
|
||||
$last_stmt = end($stmt->stmts);
|
||||
|
||||
if ($last_stmt instanceof PhpParser\Node\Stmt\Return_
|
||||
&& $last_stmt->expr instanceof PhpParser\Node\Expr\Variable
|
||||
&& $last_stmt->expr->name === 'this'
|
||||
) {
|
||||
$storage->probably_fluent = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->file_scanner->will_analyze
|
||||
@ -602,16 +616,14 @@ class FunctionLikeNodeScanner
|
||||
$classlike_storage->appearing_property_ids[$param_storage->name] = $property_id;
|
||||
$classlike_storage->initialized_properties[$param_storage->name] = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod
|
||||
&& $stmt->name->name === '__construct'
|
||||
&& $classlike_storage
|
||||
&& $storage instanceof MethodStorage
|
||||
&& $storage->params
|
||||
&& $this->config->infer_property_types_from_constructor
|
||||
) {
|
||||
$this->inferPropertyTypeFromConstructor($stmt, $storage, $classlike_storage);
|
||||
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod
|
||||
&& $storage instanceof MethodStorage
|
||||
&& $storage->params
|
||||
&& $this->config->infer_property_types_from_constructor
|
||||
) {
|
||||
$this->inferPropertyTypeFromConstructor($stmt, $storage, $classlike_storage);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($stmt->getAttrGroups() as $attr_group) {
|
||||
|
@ -94,4 +94,9 @@ class MethodStorage extends FunctionLikeStorage
|
||||
* @var bool
|
||||
*/
|
||||
public $stubbed = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public $probably_fluent = false;
|
||||
}
|
||||
|
@ -1416,21 +1416,6 @@ class UnusedCodeTest extends TestCase
|
||||
f(new Worker());',
|
||||
'error_message' => 'PossiblyUnusedReturnValue',
|
||||
],
|
||||
'fluentSelfMethodsProhibited' => [
|
||||
'<?php
|
||||
class A {
|
||||
public function foo(): self {
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function bar(): self {
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
(new A())->foo()->bar();',
|
||||
'error_message' => 'PossiblyUnusedReturnValue',
|
||||
],
|
||||
'interfaceWithImplementingClassMethodUnused' => [
|
||||
'<?php
|
||||
interface IWorker {
|
||||
|
Loading…
Reference in New Issue
Block a user