Skip to content

Commit

Permalink
[DeadCode][TypeDeclaration] Handle Nullable Param on RemoveUnusedPriv…
Browse files Browse the repository at this point in the history
…ateMethodParameterRector+RemoveUnreachableStatementRector+AddArrayParamDocTypeRector (#1216)

* [DeadCode][TypeDeclaration] Handle RemoveUnusedPrivateMethodParameterRector+RemoveUnreachableStatementRector+AddArrayParamDocTypeRector

* Fixed 🎉

* rename fixture

* check with namespacedName attribute

* [ci-review] Rector Rectify

* move under NullableType

* Trigger notification

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Nov 12, 2021
1 parent 19d01b9 commit f775b6e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar;
Expand Down Expand Up @@ -141,6 +142,10 @@ public function resolve(Node $node): Type
public function getType(Node $node): Type
{
if ($node instanceof NullableType) {
if ($node->type instanceof Name && $node->type->hasAttribute(AttributeKey::NAMESPACED_NAME)) {
$node->type = new FullyQualified($node->type->getAttribute(AttributeKey::NAMESPACED_NAME));
}

$type = $this->getType($node->type);
if (! $type instanceof MixedType) {
return new UnionType([$type, new NullType()]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rector\Core\Tests\Issues\NullableParamNamespaced\Fixture;

use PhpParser\Node\Stmt\ClassLike;

class DoNotAddShortFQCNClassNameDocblockNullable
{
public function run(?ClassLike $classLike): bool
{
if (! $classLike instanceof ClassLike) {
return false;
}

return $this->callPrivateMethod($classLike, true);
}

private function callPrivateMethod(ClassLike $classLike, bool $value)
{
return $classLike->getMethods() !== [];
}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\NullableParamNamespaced\Fixture;

use PhpParser\Node\Stmt\ClassLike;

class DoNotAddShortFQCNClassNameDocblockNullable
{
public function run(?ClassLike $classLike): bool
{
if (! $classLike instanceof ClassLike) {
return false;
}

return $this->callPrivateMethod($classLike, true);
}

private function callPrivateMethod(ClassLike $classLike)
{
return $classLike->getMethods() !== [];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\NullableParamNamespaced;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class NullableParamNamespacedTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
19 changes: 19 additions & 0 deletions tests/Issues/NullableParamNamespaced/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodParameterRector;
use Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(AddArrayParamDocTypeRector::class);
$services->set(RemoveUnusedPrivateMethodParameterRector::class);
$services->set(RemoveUnreachableStatementRector::class);

$parameters = $containerConfigurator->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
};

0 comments on commit f775b6e

Please sign in to comment.