Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 28, 2024
1 parent 2ddb7c3 commit 06d6629
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 76 deletions.
11 changes: 3 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,6 @@ parameters:
# dev rule
- '#Class "Rector\\Utils\\Rector\\MoveAbstractRectorToChildrenRector" is missing @see annotation with test case class reference#'

# to be extended
- '#Interface "Rector\\Set\\Contract\\SetInterface" has only single implementer\. Consider using the class directly as there is no point in using the interface#'

# optional as changes behavior, should be used explicitly outside PHP upgrade
- '#Register "Rector\\Php73\\Rector\\FuncCall\\JsonThrowOnErrorRector" service to "php73\.php" config set#'

Expand All @@ -252,11 +249,6 @@ parameters:
- src/PhpDocParser/PhpDocParser/PhpDocNodeTraverser.php
- src/Testing/PHPUnit/AbstractTestCase.php

# way to invoke repeated quesitons in symfony
-
message: '#While loop condition is always true#'
path: src/Console/Command/DetectNodeCommand.php

# known node variables
-
message: '#Access to an undefined property PhpParser\\Node\\Scalar\:\:\$value#'
Expand Down Expand Up @@ -294,6 +286,9 @@ parameters:
message: '#Parameters should have "PhpParser\\Node\\Stmt\\ClassMethod" types as the only types passed to this method#'
path: src/Reflection/ClassModifierChecker.php

# node / stmts
- '#Parameter \#3 \$stmts of method Rector\\PostRector\\Application\\PostFileProcessor\:\:shouldSkipPostRector\(\) expects array<PhpParser\\Node\\Stmt>, array<PhpParser\\Node> given#'

# false positive - should be fixed
-
message: '#Parameters should have "PhpParser\\Node\\Expr\\Closure" types as the only types passed to this method#'
Expand Down
6 changes: 3 additions & 3 deletions src/PhpParser/NodeTraverser/RectorNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Rector\PhpParser\NodeTraverser;

use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
use Rector\Contract\Rector\RectorInterface;
use Rector\VersionBonding\PhpVersionedFilter;
Expand All @@ -24,8 +24,8 @@ public function __construct(
}

/**
* @param Node[] $nodes
* @return Node[]
* @param Stmt[] $nodes
* @return Stmt[]
*/
public function traverse(array $nodes): array
{
Expand Down
10 changes: 0 additions & 10 deletions src/PostRector/Rector/DocblockNameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,28 @@
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\InlineHTML;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockNameImporter;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\ValueObject\Application\File;

final class DocblockNameImportingPostRector extends AbstractPostRector
{
public function __construct(
private readonly DocBlockNameImporter $docBlockNameImporter,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly CurrentFileProvider $currentFileProvider,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly BetterNodeFinder $betterNodeFinder,
) {
}

// @todo use refactorWithFile() with use of File directly
public function enterNode(Node $node): Node|int|null
{
if (! $node instanceof Stmt && ! $node instanceof Param) {
return null;
}

$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
throw new ShouldNotHappenException();
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (! $phpDocInfo instanceof PhpDocInfo) {
return null;
Expand Down
73 changes: 18 additions & 55 deletions src/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\NodeTraverser;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper;
use Rector\CodingStyle\Node\NameImporter;
use Rector\Exception\ShouldNotHappenException;
use Rector\Naming\Naming\AliasNameResolver;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\ValueObject\Application\File;

final class NameImportingPostRector extends AbstractPostRector
Expand All @@ -36,71 +33,45 @@ public function __construct(
) {
}

// @todo use refactorWithFile() with use of File directly
public function enterNode(Node $node): Node|int|null
{
if (! $node instanceof FullyQualified) {
return null;
}

$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
throw new ShouldNotHappenException();
if ($node->isSpecialClassName()) {
return null;
}

$currentUses = $this->useImportsResolver->resolve();
if ($this->classNameImportSkipper->shouldSkipName($node, $currentUses)) {
return null;
}

if ($this->shouldSkipFileWithoutNamespace($file)) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
// make use of existing use import
$nameInUse = $this->resolveNameInUse($node, $currentUses);
if ($nameInUse instanceof Name) {
return $nameInUse;
}

return $this->processNodeName($node, $file);
/** @var File $file */
$file = $this->currentFileProvider->getFile();
return $this->nameImporter->importName($node, $file);
}

/**
* @param Stmt[] $stmts
*/
public function shouldTraverse(array $stmts): bool
{
return ! $this->betterNodeFinder->hasInstancesOf($stmts, [InlineHTML::class]);
}

private function shouldSkipFileWithoutNamespace(File $file): bool
{
$firstStmt = current($file->getNewStmts());
if (! $firstStmt instanceof FileWithoutNamespace) {
return false;
}

$currentStmt = current($firstStmt->stmts);
return $currentStmt instanceof InlineHTML || $currentStmt === false;
}

private function processNodeName(FullyQualified $fullyQualified, File $file): ?Node
{
if ($fullyQualified->isSpecialClassName()) {
return null;
}

$namespaces = array_filter(
$file->getNewStmts(),
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_
);
$namespaces = $this->betterNodeFinder->findInstanceOf($stmts, Namespace_::class);

// skip if 2 namespaces are present
if (count($namespaces) > 1) {
return null;
}

/** @var Use_[]|GroupUse[] $currentUses */
$currentUses = $this->useImportsResolver->resolve();
if ($this->classNameImportSkipper->shouldSkipName($fullyQualified, $currentUses)) {
return null;
}

$nameInUse = $this->resolveNameInUse($fullyQualified, $currentUses);
if ($nameInUse instanceof Name) {
return $nameInUse;
return false;
}

return $this->nameImporter->importName($fullyQualified, $file);
return ! $this->betterNodeFinder->hasInstancesOf($stmts, [InlineHTML::class]);
}

/**
Expand All @@ -113,14 +84,6 @@ private function resolveNameInUse(FullyQualified $fullyQualified, array $current
return new Name($aliasName);
}

return $this->resolveLongNameInUseName($fullyQualified, $currentUses);
}

/**
* @param Use_[]|GroupUse[] $currentUses
*/
private function resolveLongNameInUseName(FullyQualified $fullyQualified, array $currentUses): ?Name
{
if (substr_count($fullyQualified->toCodeString(), '\\') === 1) {
return null;
}
Expand Down

0 comments on commit 06d6629

Please sign in to comment.