Skip to content

Commit

Permalink
[DX] Test BetterNodeFinder with Laravel container (#4106)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 8, 2023
1 parent b0873f7 commit 2f3e9be
Show file tree
Hide file tree
Showing 26 changed files with 90 additions and 191 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"composer/xdebug-handler": "^3.0.3",
"doctrine/inflector": "^2.0.6",
"fidry/cpu-core-counter": "^0.5.1",
"illuminate/container": "^10.13",
"nette/neon": "^3.4",
"nette/utils": "^3.2",
"nikic/php-parser": "^4.15.4",
Expand Down
9 changes: 4 additions & 5 deletions packages-tests/Comments/CommentRemover/CommentRemoverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Comments\CommentRemover;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\FileSystemRector\Parser\FileInfoParser;
use Rector\Testing\Fixture\FixtureFileFinder;
Expand All @@ -21,15 +20,15 @@ final class CommentRemoverTest extends AbstractTestCase

private FileInfoParser $fileInfoParser;

private NodePrinterInterface $nodePrinter;
private BetterStandardPrinter $betterStandardPrinter;

protected function setUp(): void
{
$this->boot();

$this->commentRemover = $this->getService(CommentRemover::class);
$this->fileInfoParser = $this->getService(FileInfoParser::class);
$this->nodePrinter = $this->getService(BetterStandardPrinter::class);
$this->betterStandardPrinter = $this->getService(BetterStandardPrinter::class);
}

#[DataProvider('provideData')]
Expand All @@ -41,14 +40,14 @@ public function test(string $filePath): void
$nodes = $this->fileInfoParser->parseFileInfoToNodesAndDecorate($inputFilePath);
$nodesWithoutComments = $this->commentRemover->removeFromNode($nodes);

$fileContent = $this->nodePrinter->print($nodesWithoutComments);
$fileContent = $this->betterStandardPrinter->print($nodesWithoutComments);
$fileContent = trim($fileContent);

$expectedContent = trim((string) $expectedOutputContents);
$this->assertSame($fileContent, $expectedContent);

// original nodes are not touched
$originalContent = $this->nodePrinter->print($nodes);
$originalContent = $this->betterStandardPrinter->print($nodes);
$this->assertNotSame($expectedContent, $originalContent);
}

Expand Down
90 changes: 0 additions & 90 deletions packages/NodeNameResolver/Error/InvalidNameNodeReporter.php

This file was deleted.

10 changes: 2 additions & 8 deletions packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Rector\Core\NodeAnalyzer\CallAnalyzer;
use Rector\Core\Util\StringUtils;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\NodeNameResolver\Error\InvalidNameNodeReporter;
use Rector\NodeNameResolver\Regex\RegexPatternDetector;
use Rector\NodeTypeResolver\Node\AttributeKey;

Expand All @@ -42,7 +41,6 @@ final class NodeNameResolver
public function __construct(
private readonly RegexPatternDetector $regexPatternDetector,
private readonly ClassNaming $classNaming,
private readonly InvalidNameNodeReporter $invalidNameNodeReporter,
private readonly CallAnalyzer $callAnalyzer,
private readonly array $nodeNameResolvers = []
) {
Expand Down Expand Up @@ -123,12 +121,8 @@ public function getName(Node | string $node): ?string
return $namespacedName;
}

if ($node instanceof MethodCall || $node instanceof StaticCall) {
if ($this->isCallOrIdentifier($node->name)) {
return null;
}

$this->invalidNameNodeReporter->reportInvalidNodeForName($node);
if (($node instanceof MethodCall || $node instanceof StaticCall) && $this->isCallOrIdentifier($node->name)) {
return null;
}

$scope = $node->getAttribute(AttributeKey::SCOPE);
Expand Down
22 changes: 22 additions & 0 deletions packages/Testing/PHPUnit/AbstractLazyTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Rector\Testing\PHPUnit;

use Illuminate\Container\Container;
use PHPUnit\Framework\TestCase;

abstract class AbstractLazyTestCase extends TestCase
{
/**
* @template TType as object
* @param class-string<TType> $class
* @return TType
*/
protected function make(string $class): object
{
$container = new Container();
return $container->make($class);
}
}
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,8 @@ parameters:
- '#Cognitive complexity for "Rector\\(.*?)\:\:refactor\(\)" is (1[0-9]|20), keep it under 11#'
- '#Cognitive complexity for "Rector\\DeadCode\\Rector\\StaticCall\\RemoveParentCallWithoutParentRector\:\:refactor\(\)" is 26, keep it under 11#'

- '#Call to deprecated method findFirstPrevious\(\) of class Rector\\Core\\PhpParser\\Node\\BetterNodeFinder#'

- '#Return type (.*?) should be covariant with return type \(1\|2\|3\|4\|array<PhpParser\\Node>\|PhpParser\\Node\|null\) of method Rector\\Core\\Contract\\Rector(.*?)\:\:(.*?)#'

# false positive
Expand Down
6 changes: 3 additions & 3 deletions rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
use Rector\Arguments\NodeAnalyzer\ArgumentAddingScope;
use Rector\Arguments\NodeAnalyzer\ChangedArgumentsDetector;
use Rector\Arguments\ValueObject\ArgumentAdder;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\Rector\AbstractRector;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand All @@ -48,7 +48,7 @@ public function __construct(
private readonly ArgumentAddingScope $argumentAddingScope,
private readonly ChangedArgumentsDetector $changedArgumentsDetector,
private readonly AstResolver $astResolver,
private readonly NodePrinterInterface $nodePrinter
private readonly BetterStandardPrinter $betterStandardPrinter
) {
}

Expand Down Expand Up @@ -215,7 +215,7 @@ private function fillGapBetweenWithDefaultValue(MethodCall | StaticCall $node, i
throw new ShouldNotHappenException('Previous position does not have default value');
}

$default = $this->nodePrinter->print($param->default);
$default = $this->betterStandardPrinter->print($param->default);
$node->args[$index] = new Arg(new ConstFetch(new Name($default)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -29,7 +29,7 @@ final class SimplifyIfElseToTernaryRector extends AbstractRector
private const LINE_LENGTH_LIMIT = 120;

public function __construct(
private readonly NodePrinterInterface $nodePrinter
private readonly BetterStandardPrinter $betterStandardPrinter
) {
}

Expand Down Expand Up @@ -192,7 +192,7 @@ private function haveNestedTernary(array $nodes): bool

private function isNodeTooLong(Assign $assign): bool
{
$assignContent = $this->nodePrinter->print($assign);
$assignContent = $this->betterStandardPrinter->print($assign);
return Strings::length($assignContent) > self::LINE_LENGTH_LIMIT;
}
}
6 changes: 3 additions & 3 deletions rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
use Rector\CodeQuality\NodeManipulator\ExprBoolCaster;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -28,7 +28,7 @@ final class SimplifyIfReturnBoolRector extends AbstractRector
public function __construct(
private readonly CommentsMerger $commentsMerger,
private readonly ExprBoolCaster $exprBoolCaster,
private readonly NodePrinterInterface $nodePrinter
private readonly BetterStandardPrinter $betterStandardPrinter
) {
}

Expand Down Expand Up @@ -146,7 +146,7 @@ private function shouldSkipIfAndReturn(If_ $if, Return_ $return): bool
return ! $this->valueResolver->isTrueOrFalse($return->expr);
}

$condString = $this->nodePrinter->print($if->cond);
$condString = $this->betterStandardPrinter->print($if->cond);
if (! \str_contains($condString, '!=')) {
return ! $this->valueResolver->isTrueOrFalse($return->expr);
}
Expand Down
6 changes: 3 additions & 3 deletions rules/DeadCode/NodeAnalyzer/ExprUsedInNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Expr\Variable;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\NodeAnalyzer\CompactFuncCallAnalyzer;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;

final class ExprUsedInNodeAnalyzer
{
public function __construct(
private readonly UsedVariableNameAnalyzer $usedVariableNameAnalyzer,
private readonly CompactFuncCallAnalyzer $compactFuncCallAnalyzer,
private readonly NodePrinterInterface $nodePrinter
private readonly BetterStandardPrinter $betterStandardPrinter
) {
}

Expand All @@ -28,7 +28,7 @@ public function isUsed(Node $node, Variable $variable): bool

// variable as variable variable need mark as used
if ($node instanceof Variable) {
$print = $this->nodePrinter->print($node);
$print = $this->betterStandardPrinter->print($node);
if (\str_starts_with($print, '${$')) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\PreDec;
use PhpParser\Node\Expr\PreInc;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\MultiInstanceofChecker;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -28,7 +28,7 @@ final class RemoveDuplicatedArrayKeyRector extends AbstractRector
private const ALLOWED_KEY_DUPLICATES = [PreInc::class, PreDec::class];

public function __construct(
private readonly NodePrinterInterface $nodePrinter,
private readonly BetterStandardPrinter $betterStandardPrinter,
private readonly MultiInstanceofChecker $multiInstanceofChecker
) {
}
Expand Down Expand Up @@ -102,7 +102,7 @@ private function resolveDuplicateKeysArrayItems(Array_ $array): array
continue;
}

$keyValue = $this->nodePrinter->print($arrayItem->key);
$keyValue = $this->betterStandardPrinter->print($arrayItem->key);
$arrayItemsByKeys[$keyValue][] = $arrayItem;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\If_;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -20,7 +20,7 @@
final class SimplifyIfElseWithSameContentRector extends AbstractRector
{
public function __construct(
private readonly NodePrinterInterface $nodePrinter,
private readonly BetterStandardPrinter $betterStandardPrinter,
) {
}

Expand Down Expand Up @@ -83,18 +83,18 @@ public function refactor(Node $node): ?array
private function isIfWithConstantReturns(If_ $if): bool
{
$possibleContents = [];
$possibleContents[] = $this->nodePrinter->print($if->stmts);
$possibleContents[] = $this->betterStandardPrinter->print($if->stmts);

foreach ($if->elseifs as $elseif) {
$possibleContents[] = $this->nodePrinter->print($elseif->stmts);
$possibleContents[] = $this->betterStandardPrinter->print($elseif->stmts);
}

$else = $if->else;
if (! $else instanceof Else_) {
throw new ShouldNotHappenException();
}

$possibleContents[] = $this->nodePrinter->print($else->stmts);
$possibleContents[] = $this->betterStandardPrinter->print($else->stmts);

$uniqueContents = array_unique($possibleContents);

Expand Down
Loading

0 comments on commit 2f3e9be

Please sign in to comment.