Skip to content

Commit

Permalink
Reproduce Rector issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 4, 2022
1 parent f96fc80 commit 423a005
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/other-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ jobs:
cd e2e/swift-mailer
composer install
../../phpstan
- |
cd e2e/rector-autoload
composer install
../../phpstan
include:
- php-version: 8.0
ini-values: memory_limit=256M
Expand Down
1 change: 1 addition & 0 deletions e2e/rector-autoload/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
6 changes: 6 additions & 0 deletions e2e/rector-autoload/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require-dev": {
"phpstan/phpstan": "^1.6",
"rector/rector": "^0.12.23"
}
}
138 changes: 138 additions & 0 deletions e2e/rector-autoload/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions e2e/rector-autoload/phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon

parameters:
level: 8
paths:
- src
64 changes: 64 additions & 0 deletions e2e/rector-autoload/src/ImportPhpUnitFunctionsRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Luxemate\PhpstanRectorError\Rector\Rule;

use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class ImportPhpUnitFunctionsRector extends AbstractRector
{
private const PHPUNIT_FUNC_NAMESPACE = 'PHPUnit\\Framework\\';

public function refactor(Node $node): ?Node
{
if (!$node instanceof Node\Name) {
return null;
}

$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);

if (!$parentNode instanceof Node\Expr\FuncCall) {
return null;
}

$resolvedName = $node->getAttribute(AttributeKey::RESOLVED_NAME);

// If node has a resolved name, then it's probably an internal function
if ($resolvedName instanceof Node\Name\FullyQualified) {
return null;
}

/** @var string $functionName */
$functionName = $this->getName($node);

// If function name is FQCN it's already imported or is a member of this file's namespace
if (str_contains($functionName, '\\')) {
return null;
}

$functionFullyQualifiedName = self::PHPUNIT_FUNC_NAMESPACE.$functionName;

if (!function_exists($functionFullyQualifiedName)) {
return null;
}

return new Node\Name\FullyQualified($functionFullyQualifiedName);
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Adds import for non-imported PHPUnit functions.',
[]
);
}

public function getNodeTypes(): array
{
return [Node\Name::class];
}
}

0 comments on commit 423a005

Please sign in to comment.