Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions config/set/rector-preset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down Expand Up @@ -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<class-string<Node>>
*/
Expand All @@ -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
));
}
}
5 changes: 0 additions & 5 deletions src/ValueObject/Application/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,4 @@ public function getFileNode(): ?FileNode

return null;
}

public function hasShebang(): bool
{
return str_starts_with($this->fileContent, '#!');
}
}
4 changes: 2 additions & 2 deletions tests/Caching/Config/FileHashComputer/Fixture/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
};
28 changes: 0 additions & 28 deletions tests/Issues/InfiniteLoop/DeclareStrictTypesParseStrTest.php

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions tests/Issues/InfiniteLoop/config/declare_strict_types_parsestr.php

This file was deleted.

Loading