Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add phpstan native cond types for getName() for nodes that always return a string #5413

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"phpstan/phpstan-strict-rules": "^1.5",
"phpstan/phpstan-webmozart-assert": "^1.2.2",
"phpunit/phpunit": "^10.1",
"rector/phpstan-rules": "^0.7.1",
"rector/phpstan-rules": "dev-main",
"rector/rector-generator": "^0.7",
"spatie/enum": "^3.13",
"symplify/easy-ci": "^11.3",
Expand Down
22 changes: 22 additions & 0 deletions packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Const_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\Analyser\Scope;
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\Core\Exception\ShouldNotHappenException;
Expand Down Expand Up @@ -109,6 +116,21 @@ public function isCaseSensitiveName(Node $node, string $name): bool
return $name === $resolvedName;
}

/**
* Some nodes have always-known string name. This makes PHPStan smarter.
* @see https://phpstan.org/writing-php-code/phpdoc-types#conditional-return-types
*
* @return ($node is Param ? string :
* ($node is ClassMethod ? string :
* ($node is Property ? string :
* ($node is PropertyProperty ? string :
* ($node is Trait_ ? string :
* ($node is Interface_ ? string :
* ($node is Const_ ? string :
* ($node is Node\Const_ ? string :
* ($node is Name ? string :
* string|null )))))))))
*/
public function getName(Node | string $node): ?string
{
if (is_string($node)) {
Expand Down
3 changes: 1 addition & 2 deletions rules/Strict/NodeAnalyzer/UnitializedPropertyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\ThisType;
Expand All @@ -28,7 +27,7 @@ public function __construct(

public function isUnitialized(Expr $expr): bool
{
if (! $expr instanceof PropertyFetch && ! $expr instanceof StaticPropertyFetch) {
if (! $expr instanceof PropertyFetch) {
return false;
}

Expand Down
7 changes: 7 additions & 0 deletions rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ private function refactorBooleanNot(BooleanNot $booleanNot, Scope $scope): Expr|
$this->treatAsNonEmpty
);

if (! $result instanceof Expr) {
return null;
}

if ($this->unitializedPropertyAnalyzer->isUnitialized($empty->expr)) {
return new BooleanAnd(new Isset_([$empty->expr]), $result);
}
Expand All @@ -130,6 +134,9 @@ private function refactorEmpty(Empty_ $empty, Scope $scope, bool $treatAsNonEmpt

$exprType = $scope->getNativeType($empty->expr);
$result = $this->exactCompareFactory->createIdenticalFalsyCompare($exprType, $empty->expr, $treatAsNonEmpty);
if (! $result instanceof Expr) {
return null;
}

if ($this->unitializedPropertyAnalyzer->isUnitialized($empty->expr)) {
return new BooleanOr(new BooleanNot(new Isset_([$empty->expr])), $result);
Expand Down
19 changes: 19 additions & 0 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
namespace Rector\Core\Rector;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Const_;
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\MutatingScope;
Expand Down Expand Up @@ -204,6 +208,21 @@ protected function isNames(Node $node, array $names): bool
return $this->nodeNameResolver->isNames($node, $names);
}

/**
* Some nodes have always-known string name. This makes PHPStan smarter.
* @see https://phpstan.org/writing-php-code/phpdoc-types#conditional-return-types
*
* @return ($node is Node\Param ? string :
* ($node is Node\Stmt\ClassMethod ? string :
* ($node is Node\Stmt\Property ? string :
* ($node is Node\Stmt\PropertyProperty ? string :
* ($node is Trait_ ? string :
* ($node is Interface_ ? string :
* ($node is Const_ ? string :
* ($node is Node\Const_ ? string :
* ($node is Name ? string :
* string|null )))))))))
*/
protected function getName(Node $node): ?string
{
return $this->nodeNameResolver->getName($node);
Expand Down
Loading