Skip to content

Commit eeda58e

Browse files
[stmts-aware] Use NodeGroup::STMTS_AWARE over StmtsAwareInterface (#470)
* [stmts-aware] Use NodeGroup::STMTS_AWARE over StmtsAwareInterface * [rector] Rector fixes * fix test --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent f636b9a commit eeda58e

4 files changed

Lines changed: 21 additions & 11 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"phpstan/phpstan-deprecation-rules": "^2.0",
1717
"phpstan/phpstan-webmozart-assert": "^2.0",
1818
"phpunit/phpunit": "^11.5",
19-
"rector/rector-src": "dev-main",
19+
"rector/rector-src": "dev-tv-stmts-interface",
2020
"rector/type-perfect": "^2.1",
2121
"symplify/phpstan-extensions": "^12.0",
2222
"symplify/phpstan-rules": "^14.6.9",

phpstan.neon

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ parameters:
66
level: 8
77
errorFormat: symplify
88

9+
# see https://phpstan.org/writing-php-code/phpdoc-types#global-type-aliases
10+
typeAliases:
11+
StmtsAware: \PhpParser\Node\Expr\Closure | \PhpParser\Node\Stmt\Case_ | \PhpParser\Node\Stmt\Catch_ | \PhpParser\Node\Stmt\ClassMethod | \PhpParser\Node\Stmt\Do_ | \PhpParser\Node\Stmt\Else_ | \PhpParser\Node\Stmt\ElseIf_ | \PhpParser\Node\Stmt\Finally_ | \PhpParser\Node\Stmt\For_ | \PhpParser\Node\Stmt\Foreach_ | \PhpParser\Node\Stmt\Function_ | \PhpParser\Node\Stmt\If_ | \PhpParser\Node\Stmt\Namespace_ | \PhpParser\Node\Stmt\TryCatch | \PhpParser\Node\Stmt\While_
12+
913
treatPhpDocTypesAsCertain: false
1014

1115
paths:
@@ -71,6 +75,6 @@ parameters:
7175
path: rules/TypedCollections/DocBlockProcessor/UnionCollectionTagValueNodeNarrower.php
7276
identifier: property.notFound
7377

74-
75-
# stmts aware interface
76-
- '#Access to an undefined property Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface::\$stmts#'
78+
-
79+
message: '#Function "property_exists\(\)" cannot be used/left in the code#'
80+
path: rules/Orm30/Rector/MethodCall/SetParametersArrayToCollectionRector.php

rules/Dbal40/Rector/StmtsAwareInterface/ExecuteQueryParamsToBindValueRector.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
use PhpParser\Node\Stmt\Foreach_;
1616
use PhpParser\NodeFinder;
1717
use PHPStan\Type\ObjectType;
18-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
18+
use Rector\PhpParser\Enum\NodeGroup;
1919
use Rector\Rector\AbstractRector;
2020
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2121
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2222

23+
/**
24+
* @see \Rector\Doctrine\Tests\Dbal40\Rector\StmtsAwareInterface\ExecuteQueryParamsToBindValueRector\ExecuteQueryParamsToBindValueRectorTest
25+
*/
2326
final class ExecuteQueryParamsToBindValueRector extends AbstractRector
2427
{
2528
public function getRuleDefinition(): RuleDefinition
@@ -62,19 +65,23 @@ public function run(Statement $statement, array $params): void
6265
*/
6366
public function getNodeTypes(): array
6467
{
65-
return [StmtsAwareInterface::class];
68+
return NodeGroup::STMTS_AWARE;
6669
}
6770

6871
/**
69-
* @param StmtsAwareInterface $node
72+
* @param StmtsAware $node
7073
*/
71-
public function refactor(Node $node): ?StmtsAwareInterface
74+
public function refactor(Node $node): ?Node
7275
{
76+
if ($node->stmts === null) {
77+
return null;
78+
}
79+
7380
$nodeFinder = new NodeFinder();
7481

7582
$hasChanged = false;
7683
$objectType = new ObjectType('Doctrine\DBAL\Statement');
77-
foreach ((array) $node->stmts as $key => $stmt) {
84+
foreach ($node->stmts as $key => $stmt) {
7885
if (! $stmt instanceof Expression) {
7986
continue;
8087
}

rules/Orm30/Rector/MethodCall/SetParametersArrayToCollectionRector.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use PhpParser\Node\Stmt\ClassMethod;
2121
use PhpParser\Node\Stmt\Expression;
2222
use PHPStan\Type\ObjectType;
23-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
2423
use Rector\Rector\AbstractRector;
2524
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2625
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -92,7 +91,7 @@ public function changeArrayToCollection(array $stmts, Variable $variable): bool
9291
{
9392
$hasChanges = false;
9493
foreach ($stmts as $stmt) {
95-
if ($stmt instanceof StmtsAwareInterface) {
94+
if (property_exists($stmt, 'stmts')) {
9695
if ($this->changeArrayToCollection($stmt->stmts ?? [], $variable)) {
9796
$hasChanges = true;
9897
}

0 commit comments

Comments
 (0)