diff --git a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector/Fixture/skip_already_with_strict_types.php.inc b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector/Fixture/skip_already_with_strict_types.php.inc deleted file mode 100644 index 7383a11e7d3..00000000000 --- a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector/Fixture/skip_already_with_strict_types.php.inc +++ /dev/null @@ -1,9 +0,0 @@ - ------ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector/config/configured_rule.php b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector/config/configured_rule.php deleted file mode 100644 index 177749b095b..00000000000 --- a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([IncreaseDeclareStrictTypesRector::class]); diff --git a/rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php b/rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php index f4c73e70146..35838340981 100644 --- a/rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php +++ b/rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php @@ -87,7 +87,7 @@ public function getNodeTypes(): array public function refactor(Node $node): ?Node { throw new ShouldNotHappenException(sprintf( - '%s is deprecated as opinonated and group size depends on context. Cannot be automated. Use manually where needed instead', + '"%s" is deprecated as opinionated and group size depends on context. Cannot be automated. Use manually where needed instead', self::class )); } diff --git a/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php b/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php index 12d0623d5e6..b6584cf6fa2 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php @@ -5,38 +5,21 @@ namespace Rector\TypeDeclaration\Rector\StmtsAwareInterface; use PhpParser\Node; -use PhpParser\Node\DeclareItem; -use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\Int_; -use PhpParser\Node\Stmt; -use PhpParser\Node\Stmt\Declare_; -use PhpParser\Node\Stmt\Nop; -use Rector\ChangesReporting\ValueObject\RectorWithLineChange; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; use Rector\Contract\Rector\ConfigurableRectorInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\PhpParser\Enum\NodeGroup; -use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; use Rector\Rector\AbstractRector; -use Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; -use Webmozart\Assert\Assert; /** - * @see \Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\IncreaseDeclareStrictTypesRector\IncreaseDeclareStrictTypesRectorTest + * @deprecated As keeps changing files randomly on every run. Not deterministic. Use more reliable @see \Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector instead on specific paths. */ -final class IncreaseDeclareStrictTypesRector extends AbstractRector implements ConfigurableRectorInterface +final class IncreaseDeclareStrictTypesRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface { public const LIMIT = 'limit'; - private int $limit = 10; - - private int $changedItemCount = 0; - - public function __construct( - private readonly DeclareStrictTypeFinder $declareStrictTypeFinder - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -65,45 +48,6 @@ function someFunction() ); } - /** - * @param Stmt[] $nodes - * @return Stmt[]|null - */ - public function beforeTraverse(array $nodes): ?array - { - parent::beforeTraverse($nodes); - - if ($nodes === []) { - return null; - } - - $rootStmt = \current($nodes); - $stmt = $rootStmt; - - // skip classes without namespace for safety reasons - if ($rootStmt instanceof FileWithoutNamespace) { - return null; - } - - if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($stmt)) { - return null; - } - - // keep change within a limit - if ($this->changedItemCount >= $this->limit) { - return null; - } - - ++$this->changedItemCount; - - $strictTypesDeclare = $this->creteStrictTypesDeclare(); - - $rectorWithLineChange = new RectorWithLineChange(self::class, $stmt->getStartLine()); - $this->file->addRectorClassWithLine($rectorWithLineChange); - - return \array_merge([$strictTypesDeclare, new Nop()], $nodes); - } - /** * @return array> */ @@ -117,8 +61,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - // workaround, as Rector now only hooks to specific nodes, not arrays - return null; + throw new ShouldNotHappenException(sprintf( + '"%s" is deprecated as changes strict types randomly on each run.. Use "%s" Rector on specific paths instead.', + self::class, + DeclareStrictTypesRector::class + )); } /** @@ -126,13 +73,5 @@ public function refactor(Node $node): ?Node */ public function configure(array $configuration): void { - Assert::keyExists($configuration, self::LIMIT); - $this->limit = (int) $configuration[self::LIMIT]; - } - - private function creteStrictTypesDeclare(): Declare_ - { - $declareItem = new DeclareItem(new Identifier('strict_types'), new Int_(1)); - return new Declare_([$declareItem]); } } diff --git a/tests/Issues/DoubleDeclare/DoubleDeclareTest.php b/tests/Issues/DoubleDeclare/DoubleDeclareTest.php deleted file mode 100644 index 31b9f9efbf0..00000000000 --- a/tests/Issues/DoubleDeclare/DoubleDeclareTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/tests/Issues/DoubleDeclare/Fixture/fixture.php.inc b/tests/Issues/DoubleDeclare/Fixture/fixture.php.inc deleted file mode 100644 index c9542366731..00000000000 --- a/tests/Issues/DoubleDeclare/Fixture/fixture.php.inc +++ /dev/null @@ -1,19 +0,0 @@ - ------ -rules([DeclareStrictTypesRector::class, IncreaseDeclareStrictTypesRector::class]); -};