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
12 changes: 1 addition & 11 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ parameters:

# see https://phpstan.org/writing-php-code/phpdoc-types#global-type-aliases
typeAliases:
StmtsAware: \PhpParser\Node\Stmt\Block | \PhpParser\Node\Expr\Closure | \PhpParser\Node\Stmt\Case_ | \PhpParser\Node\Stmt\Catch_ | \PhpParser\Node\Stmt\ClassMethod | \PhpParser\Node\Stmt\Do_ | \PhpParser\Node\Stmt\Else_ | \PhpParser\Node\Stmt\ElseIf_ | \PhpParser\Node\Stmt\Finally_ | \PhpParser\Node\Stmt\For_ | \PhpParser\Node\Stmt\Foreach_ | \PhpParser\Node\Stmt\Function_ | \PhpParser\Node\Stmt\If_ | \PhpParser\Node\Stmt\Namespace_ | \PhpParser\Node\Stmt\TryCatch | \PhpParser\Node\Stmt\While_ | \Rector\PhpParser\Node\CustomNode\FileWithoutNamespace
StmtsAware: \PhpParser\Node\Stmt\Block | \PhpParser\Node\Expr\Closure | \PhpParser\Node\Stmt\Case_ | \PhpParser\Node\Stmt\Catch_ | \PhpParser\Node\Stmt\ClassMethod | \PhpParser\Node\Stmt\Do_ | \PhpParser\Node\Stmt\Else_ | \PhpParser\Node\Stmt\ElseIf_ | \PhpParser\Node\Stmt\Finally_ | \PhpParser\Node\Stmt\For_ | \PhpParser\Node\Stmt\Foreach_ | \PhpParser\Node\Stmt\Function_ | \PhpParser\Node\Stmt\If_ | \PhpParser\Node\Stmt\Namespace_ | \PhpParser\Node\Stmt\TryCatch | \PhpParser\Node\Stmt\While_ | \Rector\PhpParser\Node\FileNode

scanDirectories:
- stubs
Expand Down Expand Up @@ -49,13 +49,3 @@ parameters:
message: '#Property PhpParser\\Node\\Identifier\:\:\$name \(non\-empty\-string\) does not accept string#'
path: rules/CodeQuality/Rector/ClassMethod/ReplaceTestFunctionPrefixWithAttributeRector.php


# special case for namespace file
-
identifier: rector.noOnlyNullReturnInRefactor
path: rules/CodeQuality/Rector/StmtsAwareInterface/DeclareStrictTypesTestsRector.php

-
identifier: method.parentMethodFinalByPhpDoc
path: rules/CodeQuality/Rector/StmtsAwareInterface/DeclareStrictTypesTestsRector.php

Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Nop;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Contract\Rector\HTMLAverseRectorInterface;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PhpParser\Node\FileNode;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder;
use Rector\ValueObject\Application\File;
use Rector\ValueObject\PhpVersion;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -67,97 +64,39 @@ public function test()
}

/**
* @param Stmt[] $nodes
* @return Stmt[]|null
* @return array<class-string<Node>>
*/
public function beforeTraverse(array $nodes): ?array
public function getNodeTypes(): array
{
parent::beforeTraverse($nodes);

if ($this->file->containsHTML()) {
return null;
}

if ($this->shouldSkipNodes($nodes, $this->file)) {
return null;
}

/** @var Node $rootStmt */
$rootStmt = current($nodes);
return [FileNode::class];
}

/**
* @param FileNode $node
*/
public function refactor(Node $node): ?FileNode
{
// 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 ($rootStmt instanceof FileWithoutNamespace) {
$currentNode = current($rootStmt->stmts);
if (! $currentNode instanceof Stmt) {
return null;
}

if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($currentNode)) {
return null;
}
} elseif ($this->declareStrictTypeFinder->hasDeclareStrictTypes($rootStmt)) {
if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($node)) {
return null;
}

if (! $this->hasPHPUnitTestClass($nodes)) {
if (! $this->hasPHPUnitTestClass($node->stmts)) {
return null;
}

$rectorWithLineChange = new RectorWithLineChange(self::class, $rootStmt->getStartLine());
$this->file->addRectorClassWithLine($rectorWithLineChange);

if ($rootStmt instanceof FileWithoutNamespace) {
$stmts = [$this->nodeFactory->createDeclaresStrictType()];
$stmts = array_merge($stmts, [new Nop()]);
$stmts = array_merge($stmts, $rootStmt->stmts);
$rootStmt->stmts = $stmts;
$declareStrictTypes = $this->nodeFactory->createDeclaresStrictType();
$node->stmts = array_merge([$declareStrictTypes, new Nop()], $node->stmts);

return [$rootStmt];
}

return [$this->nodeFactory->createDeclaresStrictType(), new Nop(), ...$nodes];
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return NodeGroup::STMTS_AWARE;
}

/**
* @param StmtsAware $node
*/
public function refactor(Node $node): null
{
// workaround, as Rector now only hooks to specific nodes, not arrays
// avoid traversing, as we already handled in beforeTraverse()
return null;
return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_70;
}

/**
* @param Stmt[] $nodes
*/
private function shouldSkipNodes(array $nodes, File $file): bool
{
if ($this->skipper->shouldSkipElementAndFilePath(self::class, $file->getFilePath())) {
return true;
}

if (str_starts_with($file->getFileContent(), '#!')) {
return true;
}

return $nodes === [];
}

/**
* @param Stmt[] $nodes
*/
Expand Down