Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Rector\CodingStyle\ClassNameImport\ClassNameImportSkipVoter;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\ValueObject\Application\File;

Expand All @@ -33,9 +35,31 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
return false;
}

/**
* Note: Don't use ScopeFetcher::fetch() on Name instance,
* Scope can be null on Name
* This is part of ScopeAnalyzer::NON_REFRESHABLE_NODES
* @see https://github.com/rectorphp/rector-src/blob/9929af7c0179929b4fde6915cb7a06c3141dde6c/src/NodeAnalyzer/ScopeAnalyzer.php#L17
*/
$scope = $node->getAttribute(AttributeKey::SCOPE);
$namespace = $scope instanceof Scope ? $scope->getNamespace() : null;
$namespace = strtolower((string) $namespace);

$shortNameLowered = $fullyQualifiedObjectType->getShortNameLowered();
$fullyQualifiedObjectTypeNamespace = strtolower(
substr($fullyQualifiedObjectType->getClassName(), 0, -strlen($fullyQualifiedObjectType->getShortName()) - 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samsonasik is the last -1 really correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will look into it

);

foreach ($classLikeNames as $classLikeName) {
if (strtolower($classLikeName) === $shortNameLowered) {
if (strtolower($classLikeName) !== $shortNameLowered) {
continue;
}

if ($namespace === '') {
return true;
}

if ($namespace !== $fullyQualifiedObjectTypeNamespace) {
return true;
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Issues/AutoImport/Fixture/fqcn_current_same_class.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Bar;

class FqcnCurrentSameClass
{
public \App\Bar\FqcnCurrentSameClass $prop;
}

?>
-----
<?php

namespace App\Bar;

class FqcnCurrentSameClass
{
public FqcnCurrentSameClass $prop;
}

?>
Loading