Skip to content

Commit 578c8a4

Browse files
committed
cleanup phpstan errors
1 parent 2c016e4 commit 578c8a4

File tree

10 files changed

+47
-108
lines changed

10 files changed

+47
-108
lines changed

UPGRADING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Upgrading from Rector 2.2.11 to 2.2.12
2+
3+
4+
@todo
5+
6+
17
# Upgrading from Rector 1.x to 2.0
28

39
## PHP version requirements

phpstan.neon

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ parameters:
279279
- src/Configuration/RectorConfigBuilder.php
280280
- src/Reporting/DeprecatedRulesReporter.php
281281
identifier: classConstant.deprecatedInterface
282-
- '#Class Rector\\PhpParser\\Node\\CustomNode\\FileWithoutNamespace implements deprecated interface Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface#'
283282

284283
# allowed internally only
285284
-
@@ -386,12 +385,6 @@ parameters:
386385
paths:
387386
- rules/Php70/Rector/If_/IfToSpaceshipRector.php
388387

389-
# handles full file
390-
-
391-
paths:
392-
- rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php
393-
identifier: rector.noOnlyNullReturnInRefactor
394-
395388
-
396389
identifier: rector.noIntegerRefactorReturn
397390
paths:
@@ -403,13 +396,3 @@ parameters:
403396

404397
# condition check, just to be sure
405398
- '#Method Rector\\Rector\\AbstractRector\:\:enterNode\(\) never returns 3 so it can be removed from the return type#'
406-
407-
# special case, working on a file-level
408-
-
409-
identifier: rector.noOnlyNullReturnInRefactor
410-
path: rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php
411-
412-
# handle next with FileNode
413-
-
414-
identifier: method.parentMethodFinalByPhpDoc
415-
path: rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php

rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,6 @@ public function getNodeTypes(): array
9898
{
9999
return [FileNode::class];
100100
}
101-
//
102-
// /**
103-
// * @param StmtsAware $node
104-
// */
105-
// public function refactor(Node $node): null
106-
// {
107-
// // workaround, as Rector now only hooks to specific nodes, not arrays
108-
// // avoid traversing, as we already handled in beforeTraverse()
109-
// return null;
110-
// }
111101

112102
public function provideMinPhpVersion(): int
113103
{

src/NodeAnalyzer/TerminatedNodeAnalyzer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use PhpParser\Node\Stmt\Return_;
2626
use PhpParser\Node\Stmt\Switch_;
2727
use PhpParser\Node\Stmt\TryCatch;
28-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
2928
use Rector\PhpParser\Node\FileNode;
3029

3130
final class TerminatedNodeAnalyzer

src/PhpParser/Enum/NodeGroup.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use PhpParser\Node\Stmt\Trait_;
2828
use PhpParser\Node\Stmt\TryCatch;
2929
use PhpParser\Node\Stmt\While_;
30-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
3130
use Rector\PhpParser\Node\FileNode;
3231

3332
final class NodeGroup

src/PhpParser/Printer/BetterStandardPrinter.php

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use PhpParser\Node\Expr\MethodCall;
2222
use PhpParser\Node\Expr\Ternary;
2323
use PhpParser\Node\Expr\Yield_;
24-
use PhpParser\Node\InterpolatedStringPart;
2524
use PhpParser\Node\Scalar\InterpolatedString;
2625
use PhpParser\Node\Scalar\String_;
2726
use PhpParser\Node\Stmt\Declare_;
@@ -34,7 +33,6 @@
3433
use Rector\Configuration\Parameter\SimpleParameterProvider;
3534
use Rector\NodeAnalyzer\ExprAnalyzer;
3635
use Rector\NodeTypeResolver\Node\AttributeKey;
37-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
3836
use Rector\PhpParser\Node\FileNode;
3937
use Rector\Util\NewLineSplitter;
4038
use Rector\Util\Reflection\PrivatesAccessor;
@@ -84,6 +82,7 @@ public function printFormatPreserving(array $stmts, array $origStmts, array $ori
8482
*/
8583
public function print(Node | array | null $node): string
8684
{
85+
8786
if ($node === null) {
8887
$node = [];
8988
}
@@ -92,34 +91,9 @@ public function print(Node | array | null $node): string
9291
$node = [$node];
9392
}
9493

95-
return $this->prettyPrint($node);
96-
}
97-
98-
/**
99-
* @param Node[] $stmts
100-
*/
101-
public function prettyPrintFile(array $stmts): string
102-
{
103-
// to keep indexes from 0
104-
$stmts = array_values($stmts);
94+
$node = $this->unwrapFileNode($node);
10595

106-
return parent::prettyPrintFile($stmts) . PHP_EOL;
107-
}
108-
109-
// /**
110-
// * @api magic method in parent
111-
// */
112-
// public function pFileWithoutNamespace(FileWithoutNamespace $fileWithoutNamespace): string
113-
// {
114-
// return $this->pStmts($fileWithoutNamespace->stmts);
115-
// }
116-
117-
/**
118-
* @api magic method in parent
119-
*/
120-
public function pInterpolatedStringPart(InterpolatedStringPart $interpolatedStringPart): string
121-
{
122-
return $interpolatedStringPart->value;
96+
return $this->prettyPrint($node);
12397
}
12498

12599
protected function p(

src/PostRector/Rector/ClassRenamingPostRector.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
namespace Rector\PostRector\Rector;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Stmt;
98
use PhpParser\Node\Stmt\Namespace_;
109
use PhpParser\NodeTraverser;
1110
use PhpParser\NodeVisitor;
1211
use Rector\CodingStyle\Application\UseImportsRemover;
13-
use Rector\Configuration\Option;
14-
use Rector\Configuration\Parameter\SimpleParameterProvider;
1512
use Rector\Configuration\RenamedClassesDataCollector;
1613
use Rector\PhpParser\Node\FileNode;
1714
use Rector\PostRector\Guard\AddUseStatementGuard;
@@ -32,30 +29,6 @@ public function __construct(
3229
) {
3330
}
3431

35-
// /**
36-
// * @param Stmt[] $node
37-
// * @return Stmt[]
38-
// */
39-
// public function beforeTraverse(array $nodes): array
40-
// {
41-
// if (! SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_NAMES)) {
42-
// return $nodes;
43-
// }
44-
//
45-
// if ($nodes[0] instanceof FileNode) {
46-
// $rootNode = $nodes[0];
47-
// }
48-
//
49-
// foreach ($nodes as $node) {
50-
// if ($node instanceof FileNode || $node instanceof Namespace_) {
51-
// break;
52-
// }
53-
// }
54-
//
55-
// $this->renamedNameCollector->reset();
56-
// return $nodes;
57-
// }
58-
5932
public function enterNode(Node $node): Namespace_|FileNode|int|null
6033
{
6134
if ($node instanceof FileNode) {

src/Testing/TestingParser/TestingParser.php

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,7 @@ public function __construct(
2828

2929
public function parseFilePathToFile(string $filePath): File
3030
{
31-
// needed for PHPStan reflection, as it caches the last processed file
32-
$this->dynamicSourceLocatorProvider->setFilePath($filePath);
33-
34-
$fileContent = FileSystem::read($filePath);
35-
$file = new File($filePath, $fileContent);
36-
$stmts = $this->rectorParser->parseString($fileContent);
37-
38-
// wrap in FileNode to enable file-level rules
39-
$stmts = [new FileNode($stmts)];
40-
41-
$stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($filePath, $stmts);
42-
43-
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
44-
$this->currentFileProvider->setFile($file);
31+
[$file, $stmts] = $this->parseToFileAndStmts($filePath);
4532

4633
return $file;
4734
}
@@ -50,22 +37,49 @@ public function parseFilePathToFile(string $filePath): File
5037
* @return Node[]
5138
*/
5239
public function parseFileToDecoratedNodes(string $filePath): array
40+
{
41+
[$file, $stmts] = $this->parseToFileAndStmts($filePath);
42+
43+
return $stmts;
44+
//
45+
// // needed for PHPStan reflection, as it caches the last processed file
46+
// $this->dynamicSourceLocatorProvider->setFilePath($filePath);
47+
//
48+
// $fileContent = FileSystem::read($filePath);
49+
// $stmts = $this->rectorParser->parseString($fileContent);
50+
// $file = new File($filePath, $fileContent);
51+
//
52+
// // wrap in FileNode to enable file-level rules
53+
// $stmts = [new FileNode($stmts)];
54+
//
55+
// $stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($filePath, $stmts);
56+
// $file->hydrateStmtsAndTokens($stmts, $stmts, []);
57+
//
58+
// $this->currentFileProvider->setFile($file);
59+
//
60+
// return $stmts;
61+
}
62+
63+
/**
64+
* @return array{0: File, 1: Node\Stmt[]}
65+
*/
66+
private function parseToFileAndStmts(string $filePath): array
5367
{
5468
// needed for PHPStan reflection, as it caches the last processed file
5569
$this->dynamicSourceLocatorProvider->setFilePath($filePath);
5670

5771
$fileContent = FileSystem::read($filePath);
58-
$stmts = $this->rectorParser->parseString($fileContent);
5972
$file = new File($filePath, $fileContent);
73+
$stmts = $this->rectorParser->parseString($fileContent);
6074

6175
// wrap in FileNode to enable file-level rules
6276
$stmts = [new FileNode($stmts)];
6377

6478
$stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($filePath, $stmts);
65-
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
6679

80+
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
6781
$this->currentFileProvider->setFile($file);
6882

69-
return $stmts;
83+
return [$file, $stmts];
7084
}
7185
}

tests/Comments/CommentRemover/CommentRemoverTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected function setUp(): void
2929

3030
$this->commentRemover = $this->make(CommentRemover::class);
3131
$this->testingParser = $this->make(TestingParser::class);
32+
3233
$this->betterStandardPrinter = $this->make(BetterStandardPrinter::class);
3334
}
3435

@@ -44,11 +45,11 @@ public function test(string $filePath): void
4445

4546
$nodesWithoutComments = $this->commentRemover->removeFromNode($nodes);
4647

47-
$fileContent = $this->betterStandardPrinter->print($nodesWithoutComments);
48-
$fileContent = trim($fileContent);
48+
$printedFileContent = $this->betterStandardPrinter->print($nodesWithoutComments);
49+
$printedFileContent = trim($printedFileContent);
4950

5051
$expectedContent = trim($expectedOutputContents);
51-
$this->assertSame($fileContent, $expectedContent);
52+
$this->assertSame($printedFileContent, $expectedContent);
5253

5354
// original nodes are not touched
5455
$originalContent = $this->betterStandardPrinter->print($nodes);

tests/Comments/CommentRemover/Fixture/another_comment.php.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Rector\Tests\Comments\CommentRemover\Fixture;
44

5-
$values = new class
5+
final class AnotherComment
66
{
77
public function run($value)
88
{
@@ -19,7 +19,7 @@ $values = new class
1919
-----
2020
namespace Rector\Tests\Comments\CommentRemover\Fixture;
2121

22-
$values = new class
22+
final class AnotherComment
2323
{
2424
public function run($value)
2525
{
@@ -28,4 +28,4 @@ $values = new class
2828
return 'https://some_very_long_link.cz';
2929
}
3030
}
31-
};
31+
}

0 commit comments

Comments
 (0)