Skip to content

Commit

Permalink
make use of AbstractRector and scope fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 12, 2024
1 parent bd6a6af commit b7b157e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-webmozart-assert": "^1.2",
"phpunit/phpunit": "^10.5",
"rector/rector-src": "dev-main",
"rector/rector-src": "dev-main#e30c7af",
"rector/type-perfect": "^1.0",
"symplify/easy-coding-standard": "^12.3",
"symplify/phpstan-extensions": "^11.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
use Rector\NodeAnalyzer\ExprInTopStmtMatcher;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Parser\InlineCodeParser;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -29,7 +30,7 @@
*
* @see \Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector\DowngradeStreamIsattyRectorTest
*/
final class DowngradeStreamIsattyRector extends AbstractScopeAwareRector
final class DowngradeStreamIsattyRector extends AbstractRector
{
private ?Closure $cachedClosure = null;

Expand Down Expand Up @@ -99,7 +100,7 @@ public function getNodeTypes(): array
* @param StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $node
* @return Node[]|null
*/
public function refactorWithScope(Node $node, Scope $scope): ?array
public function refactor(Node $node): ?array
{
$expr = $this->exprInTopStmtMatcher->match(
$node,
Expand Down Expand Up @@ -128,6 +129,8 @@ function (Node $subNode): bool {

$function = $this->createClosure();

$scope = ScopeFetcher::fetch($node);

$variable = new Variable($this->variableNaming->createCountedValueName('streamIsatty', $scope));
$assign = new Assign($variable, $function);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassLike;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use PHPStan\Type\Type;
use Rector\DowngradePhp81\NodeAnalyzer\ArraySpreadAnalyzer;
use Rector\DowngradePhp81\NodeFactory\ArrayMergeFromArraySpreadFactory;
use Rector\PhpParser\AstResolver;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -29,7 +29,7 @@
*
* @see \Rector\Tests\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector\DowngradeArraySpreadRectorTest
*/
final class DowngradeArraySpreadRector extends AbstractScopeAwareRector
final class DowngradeArraySpreadRector extends AbstractRector
{
public function __construct(
private readonly ArrayMergeFromArraySpreadFactory $arrayMergeFromArraySpreadFactory,
Expand Down Expand Up @@ -98,7 +98,7 @@ public function getNodeTypes(): array
/**
* @param Array_|ClassConst $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
if ($node instanceof ClassConst) {
return $this->refactorUnderClassConst($node);
Expand All @@ -109,6 +109,8 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
}

/** @var MutatingScope $scope */
$scope = ScopeFetcher::fetch($node);

return $this->arrayMergeFromArraySpreadFactory->createFromArray($node, $scope);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Catch_;
use PHPStan\Analyser\Scope;
use Rector\Naming\Naming\VariableNaming;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -18,7 +18,7 @@
*
* @see \Rector\Tests\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector\DowngradeNonCapturingCatchesRectorTest
*/
final class DowngradeNonCapturingCatchesRector extends AbstractScopeAwareRector
final class DowngradeNonCapturingCatchesRector extends AbstractRector
{
public function __construct(
private readonly VariableNaming $variableNaming
Expand Down Expand Up @@ -72,12 +72,14 @@ public function getNodeTypes(): array
/**
* @param Catch_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
if ($node->var instanceof Variable) {
return null;
}

$scope = ScopeFetcher::fetch($node);

$exceptionVarName = $this->variableNaming->createCountedValueName('exception', $scope);
$node->var = new Variable($exceptionVarName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
use PHPStan\Analyser\Scope;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php72\NodeFactory\AnonymousFunctionFactory;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -38,7 +39,7 @@
*
* @see \Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\DowngradeMatchToSwitchRectorTest
*/
final class DowngradeMatchToSwitchRector extends AbstractScopeAwareRector
final class DowngradeMatchToSwitchRector extends AbstractRector
{
public function __construct(
private readonly AnonymousFunctionFactory $anonymousFunctionFactory
Expand Down Expand Up @@ -99,12 +100,14 @@ public function getNodeTypes(): array
/**
* @param Echo_|Expression|Return_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
/** @var Match_|null $match */
$match = null;
$hasChanged = false;

$scope = ScopeFetcher::fetch($node);

$this->traverseNodesWithCallable(
$node,
function (Node $subNode) use ($node, &$match, &$hasChanged, $scope) {
Expand Down Expand Up @@ -180,6 +183,7 @@ function (Node $subNode) use ($node, &$match, &$hasChanged, $scope) {
return null;
}

$scope = ScopeFetcher::fetch($node);
if (! $this->isEqualScope($match, $scope)) {
return null;
}
Expand All @@ -191,6 +195,7 @@ function (Node $subNode) use ($node, &$match, &$hasChanged, $scope) {

private function isEqualScope(Match_ $match, ?Scope $containerScope): bool
{

$matchScope = $match->getAttribute(AttributeKey::SCOPE);
if (! $matchScope instanceof Scope) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IntegerType;
use Rector\DowngradePhp81\NodeAnalyzer\ArraySpreadAnalyzer;
use Rector\DowngradePhp81\NodeFactory\ArrayMergeFromArraySpreadFactory;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -22,7 +22,7 @@
*
* @see \Rector\Tests\DowngradePhp81\Rector\Array_\DowngradeArraySpreadStringKeyRector\DowngradeArraySpreadStringKeyRectorTest
*/
final class DowngradeArraySpreadStringKeyRector extends AbstractScopeAwareRector
final class DowngradeArraySpreadStringKeyRector extends AbstractRector
{
public function __construct(
private readonly ArrayMergeFromArraySpreadFactory $arrayMergeFromArraySpreadFactory,
Expand Down Expand Up @@ -65,7 +65,7 @@ public function getNodeTypes(): array
/**
* @param Array_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
if (! $this->arraySpreadAnalyzer->isArrayWithUnpack($node)) {
return null;
Expand All @@ -76,6 +76,8 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
}

/** @var MutatingScope $scope */
$scope = ScopeFetcher::fetch($node);

return $this->arrayMergeFromArraySpreadFactory->createFromArray($node, $scope);
}

Expand Down

0 comments on commit b7b157e

Please sign in to comment.