diff --git a/config/set/rector-preset.php b/config/set/rector-preset.php
index 89ffc20df27..f350c1689c7 100644
--- a/config/set/rector-preset.php
+++ b/config/set/rector-preset.php
@@ -7,12 +7,10 @@
use Rector\DeadCode\Rector\Class_\RemoveRefactorDuplicatedNodeInstanceCheckRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddSeeTestAnnotationRector;
use Rector\Privatization\Rector\Class_\FinalizeTestCaseClassRector;
-use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;
use Rector\TypeDeclarationDocblocks\Rector\Class_\AddParamTypeToRefactorMethodRector;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
- DeclareStrictTypesRector::class,
PostIncDecToPreIncDecRector::class,
FinalizeTestCaseClassRector::class,
AddParamTypeToRefactorMethodRector::class,
diff --git a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/DeclareStrictTypesRectorTest.php b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/DeclareStrictTypesRectorTest.php
deleted file mode 100644
index 2cbfb54a7da..00000000000
--- a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/DeclareStrictTypesRectorTest.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/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/multiple_declare.php.inc b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/multiple_declare.php.inc
deleted file mode 100644
index b89891feee0..00000000000
--- a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/multiple_declare.php.inc
+++ /dev/null
@@ -1,43 +0,0 @@
-";
-}
-
-register_tick_function('example');
-
-declare(ticks=1) {
- $cars = ["Ford", "Volvo", "BMW"];
- foreach($cars as $car) {
- echo "$car
";
- }
-}
-
-?>
------
-";
-}
-
-register_tick_function('example');
-
-declare(ticks=1) {
- $cars = ["Ford", "Volvo", "BMW"];
- foreach($cars as $car) {
- echo "$car
";
- }
-}
diff --git a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/multiple_declare2.php.inc b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/multiple_declare2.php.inc
deleted file mode 100644
index be70181af63..00000000000
--- a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/multiple_declare2.php.inc
+++ /dev/null
@@ -1,17 +0,0 @@
-
------
-
diff --git a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/no_namespace_with_plus_operation.php.inc b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/no_namespace_with_plus_operation.php.inc
deleted file mode 100644
index 5c048cc3cef..00000000000
--- a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/no_namespace_with_plus_operation.php.inc
+++ /dev/null
@@ -1,31 +0,0 @@
-prop2 + $second;
- }
-}
-
-?>
------
-prop2 + $second;
- }
-}
diff --git a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/skip_already_with_strict_types.php.inc b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/skip_already_with_strict_types.php.inc
deleted file mode 100644
index 7383a11e7d3..00000000000
--- a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/Fixture/skip_already_with_strict_types.php.inc
+++ /dev/null
@@ -1,9 +0,0 @@
-
------
-withSkip([
- DeclareStrictTypesRector::class => [
- // .php.inc changed .php during running test
- realpath(__DIR__ . '/../Fixture') . '/skipped_by_path.php',
- ],
- ])
- ->withRules([DeclareStrictTypesRector::class]);
diff --git a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php
index db8e89ffd3a..a24066b2830 100644
--- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php
+++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php
@@ -5,26 +5,18 @@
namespace Rector\TypeDeclaration\Rector\StmtsAwareInterface;
use PhpParser\Node;
-use PhpParser\Node\Stmt\Nop;
-use Rector\Contract\Rector\HTMLAverseRectorInterface;
+use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
+use Rector\Exception\ShouldNotHappenException;
use Rector\PhpParser\Node\FileNode;
use Rector\Rector\AbstractRector;
-use Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder;
-use Rector\ValueObject\PhpVersion;
-use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
- * @see \Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\DeclareStrictTypesRectorTest
+ * @deprecated This rule is deprecated, as too risky to add `declare(strict_types=1)` without context about type safety. Use SafeDeclareStrictTypesRector instead, which only adds it to type-safe files.
*/
-final class DeclareStrictTypesRector extends AbstractRector implements HTMLAverseRectorInterface, MinPhpVersionInterface
+final class DeclareStrictTypesRector extends AbstractRector implements DeprecatedInterface
{
- public function __construct(
- private readonly DeclareStrictTypeFinder $declareStrictTypeFinder,
- ) {
- }
-
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add `declare(strict_types=1)` if missing in a namespaced file', [
@@ -57,33 +49,6 @@ function someFunction(int $number)
]);
}
- /**
- * @param FileNode $node
- */
- public function refactor(Node $node): ?FileNode
- {
- // shebang files cannot have declare strict types
- if ($this->getFile()->hasShebang()) {
- return null;
- }
-
- // only add to namespaced files, as global namespace files are often included in other files
- if (! $node->isNamespaced()) {
- return null;
- }
-
- // when first stmt is Declare_, verify if there is strict_types definition already,
- // as multiple declare is allowed, with declare(strict_types=1) only allowed on very first stmt
- if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($node)) {
- return null;
- }
-
- $declaresStrictType = $this->nodeFactory->createDeclaresStrictType();
- $node->stmts = array_merge([$declaresStrictType, new Nop()], $node->stmts);
-
- return $node;
- }
-
/**
* @return array>
*/
@@ -92,8 +57,14 @@ public function getNodeTypes(): array
return [FileNode::class];
}
- public function provideMinPhpVersion(): int
+ /**
+ * @param FileNode $node
+ */
+ public function refactor(Node $node): ?FileNode
{
- return PhpVersion::PHP_70;
+ throw new ShouldNotHappenException(sprintf(
+ '"%s" rule is deprecated, as too risky without type-safety context. Use SafeDeclareStrictTypesRector instead',
+ self::class
+ ));
}
}
diff --git a/src/ValueObject/Application/File.php b/src/ValueObject/Application/File.php
index ca87c687614..5190e72963b 100644
--- a/src/ValueObject/Application/File.php
+++ b/src/ValueObject/Application/File.php
@@ -229,9 +229,4 @@ public function getFileNode(): ?FileNode
return null;
}
-
- public function hasShebang(): bool
- {
- return str_starts_with($this->fileContent, '#!');
- }
}
diff --git a/tests/Caching/Config/FileHashComputer/Fixture/rector.php b/tests/Caching/Config/FileHashComputer/Fixture/rector.php
index 76975c2f816..0eef32a01cf 100644
--- a/tests/Caching/Config/FileHashComputer/Fixture/rector.php
+++ b/tests/Caching/Config/FileHashComputer/Fixture/rector.php
@@ -3,8 +3,8 @@
declare(strict_types=1);
use Rector\Config\RectorConfig;
-use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;
+use Rector\TypeDeclaration\Rector\StmtsAwareInterface\SafeDeclareStrictTypesRector;
return static function (RectorConfig $rectorConfig): void {
- $rectorConfig->rules([DeclareStrictTypesRector::class]);
+ $rectorConfig->rules([SafeDeclareStrictTypesRector::class]);
};
diff --git a/tests/Issues/InfiniteLoop/DeclareStrictTypesParseStrTest.php b/tests/Issues/InfiniteLoop/DeclareStrictTypesParseStrTest.php
deleted file mode 100644
index 9a2d398d192..00000000000
--- a/tests/Issues/InfiniteLoop/DeclareStrictTypesParseStrTest.php
+++ /dev/null
@@ -1,28 +0,0 @@
-doTestFile($filePath);
- }
-
- public static function provideData(): Iterator
- {
- return self::yieldFilesFromDirectory(__DIR__ . '/FixtureDeclareStrictTypesParseStr');
- }
-
- public function provideConfigFilePath(): string
- {
- return __DIR__ . '/config/declare_strict_types_parsestr.php';
- }
-}
diff --git a/tests/Issues/InfiniteLoop/FixtureDeclareStrictTypesParseStr/namespaced_same_function.php.inc b/tests/Issues/InfiniteLoop/FixtureDeclareStrictTypesParseStr/namespaced_same_function.php.inc
deleted file mode 100644
index 8ac6174c63d..00000000000
--- a/tests/Issues/InfiniteLoop/FixtureDeclareStrictTypesParseStr/namespaced_same_function.php.inc
+++ /dev/null
@@ -1,25 +0,0 @@
-
------
-withRules([DeclareStrictTypesRector::class, ParseStrWithResultArgumentRector::class]);