Skip to content
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
7 changes: 6 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,9 @@ parameters:
message: '#Unable to resolve the template type T in call to static method Webmozart\\Assert\\Assert\:\:isInstanceOfAny\(\)#'
path: rules/DeadCode/Rector/FunctionLike/NarrowWideUnionReturnTypeRector.php

- '#Provide more specific return type "Iterator|PhpParser\\Node" over abstract one#'
- '#Provide more specific return type "Iterator|PhpParser\\Node" over abstract one#'

# BC layer
-
message: '#Access to deprecated property \$file of class Rector\\Rector\\AbstractRector#'
path: src/Rector/AbstractRector.php
6 changes: 3 additions & 3 deletions rules/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

namespace Rector\CodeQuality\Rector\Coalesce;

use PHPStan\Type\UnionType;
use PHPStan\Type\NullType;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\Ternary;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\UnionType;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Rector\PHPStan\ScopeFetcher;

/**
* @see \Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\CoalesceToTernaryRectorTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private function isAllowedExpr(Expr $expr, Scope $scope): bool
return false;
}

$type = $this->allAssignNodePropertyTypeInferer->inferProperty($property, $classReflection, $this->file);
$type = $this->allAssignNodePropertyTypeInferer->inferProperty($property, $classReflection, $this->getFile());
if (! $type instanceof Type) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public function refactor(Node $node): ?Node
return null;
}

$oldTokens = $this->file->getOldTokens();
$oldTokens = $this->getFile()
->getOldTokens();
if ($this->isIfConditionFollowedByOpeningCurlyBracket($node, $oldTokens)) {
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function refactor(Node $node): ?NotEqual
$tokenEndPos = $node->getEndTokenPos();

for ($i = $tokenStartPos; $i < $tokenEndPos; ++$i) {
$token = $this->file->getOldTokens()[$i];
$token = $this->getFile()
->getOldTokens()[$i];

if ((string) $token === '<>') {
$token->text = '!=';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
$hasVariableBeenWrapped = false;
$oldTokens = $this->file->getOldTokens();
$oldTokens = $this->getFile()
->getOldTokens();

foreach ($node->parts as $index => $nodePart) {
if ($nodePart instanceof Variable && $nodePart->getStartTokenPos() >= 0) {
Expand Down
3 changes: 2 additions & 1 deletion rules/DeadCode/Rector/Concat/RemoveConcatAutocastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ private function removeStringCast(Expr $expr): Expr
}

$targetExpr = $expr->expr;
$tokens = $this->file->getOldTokens();
$tokens = $this->getFile()
->getOldTokens();

if ($expr->expr instanceof BinaryOp) {
$castStartTokenPos = $expr->getStartTokenPos();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private function processBinaryMulAndDiv(Mul | Div $binaryOp): ?Expr
if ($binaryOp instanceof Mul && $this->isLiteralOne($binaryOp->left) && $this->nodeTypeResolver->isNumberType(
$binaryOp->right
)) {
if ($this->isMulParenthesized($this->file, $binaryOp)) {
if ($this->isMulParenthesized($this->getFile(), $binaryOp)) {
$binaryOp->right->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
}

Expand Down
3 changes: 2 additions & 1 deletion rules/Php53/Rector/Ternary/TernaryToElvisRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public function provideMinPhpVersion(): int

private function isParenthesized(Expr $ifExpr, Expr $elseExpr): bool
{
$tokens = $this->file->getOldTokens();
$tokens = $this->getFile()
->getOldTokens();

$ifExprTokenEnd = $ifExpr->getEndTokenPos();
$elseExprTokenStart = $elseExpr->getStartTokenPos();
Expand Down
3 changes: 2 additions & 1 deletion rules/Php54/Rector/Array_/LongArrayToShortArrayRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public function refactor(Node $node): ?Node

$node->setAttribute(AttributeKey::KIND, Array_::KIND_SHORT);

$tokens = $this->file->getOldTokens();
$tokens = $this->getFile()
->getOldTokens();

$startTokenPos = $node->getStartTokenPos();
$endTokenPos = $node->getEndTokenPos();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private function processTernaryWithIsset(Ternary $ternary, Isset_ $isset): ?Coal
}

if (($ternary->else instanceof Ternary || $ternary->else instanceof BinaryOp) && $this->isTernaryParenthesized(
$this->file,
$this->getFile(),
$ternary->cond,
$ternary
)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isFollowedByCurlyBracket($this->file, $node)) {
if (! $this->isFollowedByCurlyBracket($this->getFile(), $node)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->parenthesizedNestedTernaryAnalyzer->isParenthesized($this->file, $node)) {
if ($this->parenthesizedNestedTernaryAnalyzer->isParenthesized($this->getFile(), $node)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$oldTokens = $this->file->getOldTokens();
$oldTokens = $this->getFile()
->getOldTokens();
$hasChanged = false;

foreach ($node->parts as $part) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function refactor(Node $node): ?Node
return null;
}

$oldTokens = $this->file->getOldTokens();
$oldTokens = $this->getFile()
->getOldTokens();

$loop = 1;
while (isset($oldTokens[$node->var->getStartTokenPos() + $loop])) {
Expand Down
3 changes: 2 additions & 1 deletion rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
$hasChanged = false;
$oldTokens = $this->file->getOldTokens();
$oldTokens = $this->getFile()
->getOldTokens();

foreach ($node->cases as $key => $case) {
$cond = $case->cond;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function refactor(Node $node): ?Node
$inferredType = $this->allAssignNodePropertyTypeInferer->inferProperty(
$property,
$classReflection,
$this->file
$this->getFile()
);
if (! $inferredType instanceof Type) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function someFunction(int $number)
public function refactor(Node $node): ?FileNode
{
// shebang files cannot have declare strict types
if ($this->file->hasShebang()) {
if ($this->getFile()->hasShebang()) {
return null;
}

Expand Down
11 changes: 9 additions & 2 deletions src/Console/Command/WorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ private function runWorker(
$encoder->on(ReactEvent::ERROR, $handleErrorCallback);

// 2. collect diffs + errors from file processor
$decoder->on(ReactEvent::DATA, function (array $json) use ($preFileCallback, $encoder, $configuration, $input): void {
$decoder->on(ReactEvent::DATA, function (array $json) use (
$preFileCallback,
$encoder,
$configuration,
$input
): void {
$action = $json[ReactCommand::ACTION];
if ($action !== Action::MAIN) {
return;
Expand All @@ -154,7 +159,9 @@ private function runWorker(
$encoder->write([
ReactCommand::ACTION => Action::RESULT,
self::RESULT => [
Bridge::FILE_DIFFS => $processResult->getFileDiffs($input->getOption(Option::OUTPUT_FORMAT) !== 'json'),
Bridge::FILE_DIFFS => $processResult->getFileDiffs(
$input->getOption(Option::OUTPUT_FORMAT) !== 'json'
),
Bridge::FILES_COUNT => count($filePaths),
Bridge::SYSTEM_ERRORS => $processResult->getSystemErrors(),
Bridge::SYSTEM_ERRORS_COUNT => count($processResult->getSystemErrors()),
Expand Down
26 changes: 22 additions & 4 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter

protected NodeComparator $nodeComparator;

/**
* @deprecated Use getFile() instead.
*/
protected File $file;

protected Skipper $skipper;
Expand Down Expand Up @@ -122,11 +125,12 @@ public function beforeTraverse(array $nodes): ?array
*/
final public function enterNode(Node $node): int|Node|null|array
{
if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->file->containsHTML()) {
if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->getFile()->containsHTML()) {
return null;
}

$filePath = $this->file->getFilePath();
$filePath = $this->getFile()
->getFilePath();
if ($this->skipper->shouldSkipCurrentNode($this, $filePath, static::class, $node)) {
return null;
}
Expand Down Expand Up @@ -159,7 +163,8 @@ final public function enterNode(Node $node): int|Node|null|array

// notify this rule changed code
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
$this->file->addRectorClassWithLine($rectorWithLineChange);
$this->getFile()
->addRectorClassWithLine($rectorWithLineChange);

return $refactoredNodeOrState;
}
Expand All @@ -175,6 +180,18 @@ final public function leaveNode(Node $node): array|int|Node|null
return null;
}

protected function getFile(): File
{
$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
throw new ShouldNotHappenException(
'File object is missing. Make sure you call $this->currentFileProvider->setFile(...) before traversing.'
);
}

return $file;
}

protected function isName(Node $node, string $name): bool
{
return $this->nodeNameResolver->isName($node, $name);
Expand Down Expand Up @@ -257,7 +274,8 @@ private function postRefactorProcess(
$this->createdByRuleDecorator->decorate($refactoredNode, $originalNode, static::class);

$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
$this->file->addRectorClassWithLine($rectorWithLineChange);
$this->getFile()
->addRectorClassWithLine($rectorWithLineChange);

/** @var MutatingScope|null $currentScope */
$currentScope = $node->getAttribute(AttributeKey::SCOPE);
Expand Down
Loading