Skip to content

Commit

Permalink
[AutoImport] Handle inner with sub namespace on auto import (#6679)
Browse files Browse the repository at this point in the history
* [AutoImport] Handle inner with sub namespace on auto import

* more fixtures

* add @see for future reference for sub sub

* Fix

* verify short name

* [ci-review] Rector Rectify

* verify short name

* verify short name

* [ci-review] Rector Rectify

* future note

* future note

* more fixture

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user authored Jan 18, 2025
1 parent 71534e3 commit 0c36822
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
11 changes: 5 additions & 6 deletions rules/CodingStyle/Application/UseImportsAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace Rector\CodingStyle\Application;

use Nette\Utils\Strings;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Use_;
use PHPStan\Type\ObjectType;
use Rector\CodingStyle\ClassNameImport\UsedImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
Expand Down Expand Up @@ -244,13 +242,14 @@ private function getNamespaceName(Namespace_ $namespace): ?string
return $namespace->name->toString();
}

private function isCurrentNamespace(string $namespaceName, ObjectType $objectType): bool
private function isCurrentNamespace(string $namespaceName, AliasedObjectType|FullyQualifiedObjectType $objectType): bool
{
$afterCurrentNamespace = Strings::after($objectType->getClassName(), $namespaceName . '\\');
if ($afterCurrentNamespace === null) {
$className = $objectType->getClassName();

if (! str_starts_with($className, $namespaceName . '\\')) {
return false;
}

return $namespaceName . '\\' . $afterCurrentNamespace === $objectType->getClassName();
return $namespaceName . '\\' . $objectType->getShortName() === $className;
}
}
29 changes: 29 additions & 0 deletions tests/Issues/AutoImport/Fixture/inner_with_subnamespace.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace helpers;

/**
* @see https://3v4l.org/WMUjV0
* it read with "helpers" as prefix: helpers\Sub\VarDumper
*/
class InnerWithSubnamespace extends Sub\VarDumper
{
}

?>
-----
<?php

namespace helpers;

use helpers\Sub\VarDumper;

/**
* @see https://3v4l.org/WMUjV0
* it read with "helpers" as prefix: helpers\Sub\VarDumper
*/
class InnerWithSubnamespace extends VarDumper
{
}

?>
29 changes: 29 additions & 0 deletions tests/Issues/AutoImport/Fixture/inner_with_subnamespace2.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace helpers;

/**
* @see https://3v4l.org/PsIsB
* it read as double "helpers" as sub sub: helpers\helpers\Sub\VarDumper
*/
class InnerWithSubnamespace extends helpers\Sub\VarDumper
{
}

?>
-----
<?php

namespace helpers;

use helpers\helpers\Sub\VarDumper;

/**
* @see https://3v4l.org/PsIsB
* it read as double "helpers" as sub sub: helpers\helpers\Sub\VarDumper
*/
class InnerWithSubnamespace extends VarDumper
{
}

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

namespace helpers;

/**
* @see https://3v4l.org/3ccde
* it read with "helpers" as prefix with direct classname: helpers\VarDumper
*
* without needs of import, as directly in the same namespace
*/
class SkipDirectInnerWithSubnamespace extends VarDumper
{
}

0 comments on commit 0c36822

Please sign in to comment.