Skip to content
Open
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
17 changes: 0 additions & 17 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,6 @@
"patches": {
"illuminate/container": [
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/illuminate-container-container-php.patch"
],
"nikic/php-parser": [
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-expr-closure-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-finally-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-function-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-do-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-catch-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-trycatch-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-for-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-classmethod-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-else-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-while-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-foreach-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-if-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-case-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-elseif-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-namespace-php.patch"
]
},
"composer-exit-on-patch-failure": true,
Expand Down
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,12 @@ parameters:
-
message: '#Property Rector\\PhpParser\\NodeTraverser\\AbstractImmutableNodeTraverser\:\:\$visitors \(list<PhpParser\\NodeVisitor>\) does not accept array<int\|string, PhpParser\\NodeVisitor>#'
path: src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php

# special check for stmts aware virtual interface
-
path: src/PhpParser/NodeTraverser/RectorNodeTraverser.php
identifier: symplify.forbiddenFuncCall
- '#Parameter \d should use "Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface" type as the only type passed to this method#'
- '#Parameter \#1 \$stmtsAware of method (.*?) expects Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface#'

- '#Method (.*?) should return Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface\|null but returns (.*?)\|null#'
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector\SimplifyUselessVariableRectorTest
Expand Down Expand Up @@ -151,12 +152,18 @@ public function refactor(Node $node): ?Node
return null;
}

/**
* @param StmtsAwareInterface $stmtsAware
* @return StmtsAwareInterface
*/
private function processSimplifyUselessVariable(
StmtsAwareInterface $stmtsAware,
\PhpParser\Node $stmtsAware,
Return_ $return,
Assign|AssignOp $assign,
int $key
): ?StmtsAwareInterface {
): \PhpParser\Node|null {
Assert::propertyExists($stmtsAware, 'stmts');

if (! $assign instanceof Assign) {
$binaryClass = $this->assignAndBinaryMap->getAlternative($assign);
if ($binaryClass === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getNodeTypes(): array
/**
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node): ?StmtsAwareInterface
public function refactor(Node $node): ?\PhpParser\Node
{
foreach ((array) $node->stmts as $key => $stmt) {
if (! $stmt instanceof If_) {
Expand Down
4 changes: 4 additions & 0 deletions rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public function refactor(Node $node): ?Node
}

foreach ($node->stmts as $key => $stmt) {
if ($key === 0) {
continue;
}

if (! $stmt instanceof Return_) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
use Rector\NodeManipulator\StmtsManipulator;
use Rector\Php\ReservedKeywordAnalyzer;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\NodeGroups;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\RemoveUnusedVariableAssignRectorTest
Expand Down Expand Up @@ -164,10 +166,12 @@ private function shouldSkip(array $stmts): bool
*/
private function collectAssignRefVariableNames(Stmt $stmt, array &$refVariableNames): void
{
if (! $stmt instanceof StmtsAwareInterface) {
if (! NodeGroups::matchesStmtsAware($stmt)) {
return;
}

Assert::propertyExists($stmt, 'stmts');

$this->traverseNodesWithCallable(
$stmt,
function (Node $subNode) use (&$refVariableNames): Node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,25 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
<<<<<<< HEAD
if ($node->getMethods() === []) {
=======
foreach ($node->stmts as $stmt) {
if (!$stmt instanceof ClassMethod) {
continue;
}

if ($stmt->isPrivate()) {
return null;
}

dump('todo');
}

$classMethods = $node->getMethods();

if ($classMethods === []) {
>>>>>>> 77e904a598 (flip keys)
return null;
}

Expand Down
10 changes: 8 additions & 2 deletions rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ public function refactor(Node $node): Node|null
return null;
}

private function processIf(If_ $if, int $key, StmtsAwareInterface $stmtsAware): void
/**
* @param StmtsAwareInterface $stmtsAware
*/
private function processIf(If_ $if, int $key, \PhpParser\Node $stmtsAware): void
{
if ($if->elseifs !== []) {
return;
Expand All @@ -137,7 +140,10 @@ private function processIf(If_ $if, int $key, StmtsAwareInterface $stmtsAware):
$this->hasChanged = true;
}

private function processForForeach(For_|Foreach_ $for, int $key, StmtsAwareInterface $stmtsAware): void
/**
* @param StmtsAwareInterface $stmtsAware
*/
private function processForForeach(For_|Foreach_ $for, int $key, \PhpParser\Node $stmtsAware): void
{
if ($for instanceof For_) {
$variables = $this->betterNodeFinder->findInstanceOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\RemoveUnusedNonEmptyArrayBeforeForeachRectorTest
Expand Down Expand Up @@ -83,12 +84,12 @@ public function run()
*/
public function getNodeTypes(): array
{
return [If_::class, StmtsAwareInterface::class];
return [StmtsAwareInterface::class];
}

/**
* @param If_|StmtsAwareInterface $node
* @return Foreach_|StmtsAwareInterface|null
* @param StmtsAwareInterface $node
* @return StmtsAwareInterface|null
*/
public function refactor(Node $node): Node|null
{
Expand Down Expand Up @@ -151,8 +152,13 @@ private function isUselessBooleanAnd(BooleanAnd $booleanAnd, Expr $foreachExpr):
return $this->countManipulator->isCounterHigherThanOne($booleanAnd->right, $foreachExpr);
}

private function refactorStmtsAware(StmtsAwareInterface $stmtsAware): ?StmtsAwareInterface
/**
* @param StmtsAwareInterface $stmtsAware
*/
private function refactorStmtsAware(\PhpParser\Node $stmtsAware): ?StmtsAwareInterface
{
Assert::propertyExists($stmtsAware, 'stmts');

if ($stmtsAware->stmts === null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ public function refactor(Node $node): ?StmtsAwareInterface

/**
* @param If_[] $ifs
* @param StmtsAwareInterface $stmtsAware
*
* @return BareSingleAssignIf[]
*/
private function getMatchingBareSingleAssignIfs(array $ifs, StmtsAwareInterface $stmtsAware): array
private function getMatchingBareSingleAssignIfs(array $ifs, \PhpParser\Node $stmtsAware): array
{
$bareSingleAssignIfs = [];
foreach ($ifs as $key => $if) {
Expand Down Expand Up @@ -233,7 +235,10 @@ private function isVariableSharedInAssignIfsAndReturn(
return true;
}

private function matchBareSingleAssignIf(Stmt $stmt, int $key, StmtsAwareInterface $stmtsAware): ?BareSingleAssignIf
/**
* @param StmtsAwareInterface $stmtsAware
*/
private function matchBareSingleAssignIf(Stmt $stmt, int $key, \PhpParser\Node $stmtsAware): ?BareSingleAssignIf
{
if (! $stmt instanceof If_) {
return null;
Expand Down
15 changes: 13 additions & 2 deletions rules/Php84/Rector/Foreach_/ForeachToArrayAllRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAllRector\ForeachToArrayAllRectorTest
Expand Down Expand Up @@ -102,8 +103,13 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::ARRAY_ALL;
}

private function refactorBooleanAssignmentPattern(StmtsAwareInterface $stmtsAware): ?Node
/**
* @param StmtsAwareInterface $stmtsAware
*/
private function refactorBooleanAssignmentPattern(\PhpParser\Node $stmtsAware): ?Node
{
Assert::propertyExists($stmtsAware, 'stmts');

foreach ($stmtsAware->stmts as $key => $stmt) {
if (! $stmt instanceof Foreach_) {
continue;
Expand Down Expand Up @@ -185,8 +191,13 @@ private function refactorBooleanAssignmentPattern(StmtsAwareInterface $stmtsAwar
return null;
}

private function refactorEarlyReturnPattern(StmtsAwareInterface $stmtsAware): ?Node
/**
* @param StmtsAwareInterface $stmtsAware
*/
private function refactorEarlyReturnPattern(\PhpParser\Node $stmtsAware): ?Node
{
Assert::propertyExists($stmtsAware, 'stmts');

foreach ($stmtsAware->stmts as $key => $stmt) {
if (! $stmt instanceof Foreach_) {
continue;
Expand Down
15 changes: 13 additions & 2 deletions rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\ForeachToArrayAnyRectorTest
Expand Down Expand Up @@ -101,8 +102,13 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::ARRAY_ANY;
}

private function refactorBooleanAssignmentPattern(StmtsAwareInterface $stmtsAware): ?Node
/**
* @param StmtsAwareInterface $stmtsAware
*/
private function refactorBooleanAssignmentPattern(\PhpParser\Node $stmtsAware): ?Node
{
Assert::propertyExists($stmtsAware, 'stmts');

foreach ($stmtsAware->stmts as $key => $stmt) {
if (! $stmt instanceof Foreach_) {
continue;
Expand Down Expand Up @@ -182,8 +188,13 @@ private function refactorBooleanAssignmentPattern(StmtsAwareInterface $stmtsAwar
return null;
}

private function refactorEarlyReturnPattern(StmtsAwareInterface $stmtsAware): ?Node
/**
* @param StmtsAwareInterface $stmtsAware
*/
private function refactorEarlyReturnPattern(\PhpParser\Node $stmtsAware): ?Node
{
Assert::propertyExists($stmtsAware, 'stmts');

foreach ($stmtsAware->stmts as $key => $stmt) {
if (! $stmt instanceof Foreach_) {
continue;
Expand Down
13 changes: 8 additions & 5 deletions src/NodeManipulator/StmtsManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\Comparing\NodeComparator;
use Rector\PhpParser\Node\BetterNodeFinder;
use Webmozart\Assert\Assert;

final readonly class StmtsManipulator
{
Expand Down Expand Up @@ -73,11 +74,13 @@ function (Node $node) use (&$stmts): null {
return $stmts;
}

public function isVariableUsedInNextStmt(
StmtsAwareInterface $stmtsAware,
int $jumpToKey,
string $variableName
): bool {
/**
* @param StmtsAwareInterface $stmtsAware
*/
public function isVariableUsedInNextStmt(Node $stmtsAware, int $jumpToKey, string $variableName): bool
{
Assert::propertyExists($stmtsAware, 'stmts');

if ($stmtsAware->stmts === null) {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,12 @@ final class AttributeKey
public const IS_BYREF_RETURN = 'is_byref_return';

/**
<<<<<<< HEAD
* @deprecated This value can change, as based on default input keys. Use existing array keys instead.
*
=======
* @deprecated use keys directly from stmts iteration as convention
>>>>>>> c23994b233 (note)
* @var string
*/
public const STMT_KEY = 'stmt_key';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
use PhpParser\Node\Stmt\Global_;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\NodeGroups;
use Webmozart\Assert\Assert;

final class GlobalVariableNodeVisitor extends NodeVisitorAbstract implements ScopeResolverNodeVisitorInterface
{
Expand All @@ -26,10 +27,12 @@ public function __construct(

public function enterNode(Node $node): ?Node
{
if (! $node instanceof StmtsAwareInterface) {
if (! NodeGroups::matchesStmtsAware($node)) {
return null;
}

Assert::propertyExists($node, 'stmts');

if ($node->stmts === null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
use PhpParser\Node\Stmt\Static_;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\NodeGroups;
use Webmozart\Assert\Assert;

final class StaticVariableNodeVisitor extends NodeVisitorAbstract implements ScopeResolverNodeVisitorInterface
{
Expand All @@ -26,10 +27,12 @@ public function __construct(

public function enterNode(Node $node): ?Node
{
if (! $node instanceof StmtsAwareInterface) {
if (! NodeGroups::matchesStmtsAware($node)) {
return null;
}

Assert::propertyExists($node, 'stmts');

if ($node->stmts === null) {
return null;
}
Expand Down
Loading
Loading