Skip to content

Commit 7188ba6

Browse files
authored
[depre] Deprecate IncreaseDeclareStrictTypesRector as adds declares randomly on consequent run, use DeclareStrictTypesRector instead (#7726)
1 parent c793b84 commit 7188ba6

File tree

9 files changed

+10
-194
lines changed

9 files changed

+10
-194
lines changed

rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector/Fixture/skip_already_with_strict_types.php.inc

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

rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector/Fixture/some_class.php.inc

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

rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector/IncreaseDeclareStrictTypesRectorTest.php

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

rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector/config/configured_rule.php

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

rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function getNodeTypes(): array
8787
public function refactor(Node $node): ?Node
8888
{
8989
throw new ShouldNotHappenException(sprintf(
90-
'%s is deprecated as opinonated and group size depends on context. Cannot be automated. Use manually where needed instead',
90+
'"%s" is deprecated as opinionated and group size depends on context. Cannot be automated. Use manually where needed instead',
9191
self::class
9292
));
9393
}

rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php

Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,21 @@
55
namespace Rector\TypeDeclaration\Rector\StmtsAwareInterface;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\DeclareItem;
9-
use PhpParser\Node\Identifier;
10-
use PhpParser\Node\Scalar\Int_;
11-
use PhpParser\Node\Stmt;
12-
use PhpParser\Node\Stmt\Declare_;
13-
use PhpParser\Node\Stmt\Nop;
14-
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
8+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
159
use Rector\Contract\Rector\ConfigurableRectorInterface;
10+
use Rector\Exception\ShouldNotHappenException;
1611
use Rector\PhpParser\Enum\NodeGroup;
17-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
1812
use Rector\Rector\AbstractRector;
19-
use Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder;
2013
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
2114
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
22-
use Webmozart\Assert\Assert;
2315

2416
/**
25-
* @see \Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\IncreaseDeclareStrictTypesRector\IncreaseDeclareStrictTypesRectorTest
17+
* @deprecated As keeps changing files randomly on every run. Not deterministic. Use more reliable @see \Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector instead on specific paths.
2618
*/
27-
final class IncreaseDeclareStrictTypesRector extends AbstractRector implements ConfigurableRectorInterface
19+
final class IncreaseDeclareStrictTypesRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
2820
{
2921
public const LIMIT = 'limit';
3022

31-
private int $limit = 10;
32-
33-
private int $changedItemCount = 0;
34-
35-
public function __construct(
36-
private readonly DeclareStrictTypeFinder $declareStrictTypeFinder
37-
) {
38-
}
39-
4023
public function getRuleDefinition(): RuleDefinition
4124
{
4225
return new RuleDefinition(
@@ -65,45 +48,6 @@ function someFunction()
6548
);
6649
}
6750

68-
/**
69-
* @param Stmt[] $nodes
70-
* @return Stmt[]|null
71-
*/
72-
public function beforeTraverse(array $nodes): ?array
73-
{
74-
parent::beforeTraverse($nodes);
75-
76-
if ($nodes === []) {
77-
return null;
78-
}
79-
80-
$rootStmt = \current($nodes);
81-
$stmt = $rootStmt;
82-
83-
// skip classes without namespace for safety reasons
84-
if ($rootStmt instanceof FileWithoutNamespace) {
85-
return null;
86-
}
87-
88-
if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($stmt)) {
89-
return null;
90-
}
91-
92-
// keep change within a limit
93-
if ($this->changedItemCount >= $this->limit) {
94-
return null;
95-
}
96-
97-
++$this->changedItemCount;
98-
99-
$strictTypesDeclare = $this->creteStrictTypesDeclare();
100-
101-
$rectorWithLineChange = new RectorWithLineChange(self::class, $stmt->getStartLine());
102-
$this->file->addRectorClassWithLine($rectorWithLineChange);
103-
104-
return \array_merge([$strictTypesDeclare, new Nop()], $nodes);
105-
}
106-
10751
/**
10852
* @return array<class-string<Node>>
10953
*/
@@ -117,22 +61,17 @@ public function getNodeTypes(): array
11761
*/
11862
public function refactor(Node $node): ?Node
11963
{
120-
// workaround, as Rector now only hooks to specific nodes, not arrays
121-
return null;
64+
throw new ShouldNotHappenException(sprintf(
65+
'"%s" is deprecated as changes strict types randomly on each run.. Use "%s" Rector on specific paths instead.',
66+
self::class,
67+
DeclareStrictTypesRector::class
68+
));
12269
}
12370

12471
/**
12572
* @param array<string, mixed> $configuration
12673
*/
12774
public function configure(array $configuration): void
12875
{
129-
Assert::keyExists($configuration, self::LIMIT);
130-
$this->limit = (int) $configuration[self::LIMIT];
131-
}
132-
133-
private function creteStrictTypesDeclare(): Declare_
134-
{
135-
$declareItem = new DeclareItem(new Identifier('strict_types'), new Int_(1));
136-
return new Declare_([$declareItem]);
13776
}
13877
}

tests/Issues/DoubleDeclare/DoubleDeclareTest.php

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

tests/Issues/DoubleDeclare/Fixture/fixture.php.inc

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

tests/Issues/DoubleDeclare/config/configured_rule.php

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

0 commit comments

Comments
 (0)