Skip to content

Commit bcd485b

Browse files
committed
make DeclareStrictTypesRector skip files without namespace, as very risky in files without PSR-4 autodsicovery
1 parent 0554c83 commit bcd485b

8 files changed

Lines changed: 34 additions & 112 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessAssignFromPropertyPromotionRector\Fixture;
4+
5+
class SkipNormalParam
6+
{
7+
private \stdClass $std;
8+
9+
public function __construct(\stdClass $std)
10+
{
11+
$this->std = $std;
12+
}
13+
}
14+

rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/AutoImportDeclareStrictTypesRectorTest.php

Lines changed: 0 additions & 28 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
function someFunction()
4+
{
5+
}

rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/FixtureAutoImport/no_namespace.php.inc

Lines changed: 0 additions & 15 deletions
This file was deleted.

rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/FixtureAutoImport/with_namespace.php.inc

Lines changed: 0 additions & 17 deletions
This file was deleted.

rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/config/auto_import_configured_rule.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,30 @@ public function __construct(
3434

3535
public function getRuleDefinition(): RuleDefinition
3636
{
37-
return new RuleDefinition('Add `declare(strict_types=1)` if missing', [
37+
return new RuleDefinition('Add `declare(strict_types=1)` if missing in a namespaced file', [
3838
new CodeSample(
3939
<<<'CODE_SAMPLE'
40-
function someFunction()
40+
namespace App;
41+
42+
class SomeClass
4143
{
44+
function someFunction(int $number)
45+
{
46+
}
4247
}
4348
CODE_SAMPLE
4449

4550
,
4651
<<<'CODE_SAMPLE'
4752
declare(strict_types=1);
4853
49-
function someFunction()
54+
namespace App;
55+
56+
class SomeClass
5057
{
58+
function someFunction(int $number)
59+
{
60+
}
5161
}
5262
CODE_SAMPLE
5363
),
@@ -79,14 +89,7 @@ public function beforeTraverse(array $nodes): ?array
7989
$stmt = $rootStmt;
8090

8191
if ($rootStmt instanceof FileWithoutNamespace) {
82-
$currentStmt = current($rootStmt->stmts);
83-
84-
if (! $currentStmt instanceof Stmt) {
85-
return null;
86-
}
87-
88-
$nodes = $rootStmt->stmts;
89-
$stmt = $currentStmt;
92+
return null;
9093
}
9194

9295
// when first stmt is Declare_, verify if there is strict_types definition already,
@@ -101,12 +104,6 @@ public function beforeTraverse(array $nodes): ?array
101104
$rectorWithLineChange = new RectorWithLineChange(self::class, $stmt->getStartLine());
102105
$this->file->addRectorClassWithLine($rectorWithLineChange);
103106

104-
if ($rootStmt instanceof FileWithoutNamespace) {
105-
/** @var Stmt[] $nodes */
106-
$rootStmt->stmts = [$strictTypesDeclare, new Nop(), ...$nodes];
107-
return [$rootStmt];
108-
}
109-
110107
return [$strictTypesDeclare, new Nop(), ...$nodes];
111108
}
112109

@@ -121,7 +118,7 @@ public function getNodeTypes(): array
121118
/**
122119
* @param StmtsAwareInterface $node
123120
*/
124-
public function refactor(Node $node): ?int
121+
public function refactor(Node $node): int
125122
{
126123
// workaround, as Rector now only hooks to specific nodes, not arrays
127124
// avoid traversing, as we already handled in beforeTraverse()

tests/Issues/InfiniteLoop/FixtureDeclareStrictTypesParseStr/no_namespace_same_function.php.inc

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)