-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] Add ExternalFullyQualifiedAnalyzer to check external FullyQual…
…ified extends/interface/traits (#164) Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
- Loading branch information
1 parent
1d2b324
commit ca8f75f
Showing
13 changed files
with
221 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...gradeParameterTypeWideningRector/Fixture/skip_nullable_string_from_external_trait.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture; | ||
|
||
use Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Source\NullableStringTrait; | ||
|
||
final class SkipNullableStringFromExternalTrait | ||
{ | ||
use NullableStringTrait; | ||
|
||
public function load(string $value = null) | ||
{ | ||
} | ||
} | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...dePhp72/Rector/Class_/DowngradeParameterTypeWideningRector/Source/NullableStringTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Source; | ||
|
||
trait NullableStringTrait | ||
{ | ||
public function load(string $value = null) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...unctionLike/ReturnTypeDeclarationRector/Fixture/skip_from_external_void_interface.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture; | ||
|
||
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\ExternalForVoidInterface; | ||
|
||
final class SkipFromExternalVoidInterface implements ExternalForVoidInterface | ||
{ | ||
public function run() | ||
{ | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...or/FunctionLike/ReturnTypeDeclarationRector/Fixture/skip_from_external_void_trait.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture; | ||
|
||
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\ExternalForVoidTrait; | ||
|
||
final class SkipFromExternalVoidTrait | ||
{ | ||
use ExternalForVoidTrait; | ||
|
||
public function run() | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ypeDeclaration/Rector/FunctionLike/ReturnTypeDeclarationRector/Source/ExternalForVoid.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source; | ||
|
||
class ExternalForVoid | ||
{ | ||
public function run() | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ation/Rector/FunctionLike/ReturnTypeDeclarationRector/Source/ExternalForVoidInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source; | ||
|
||
interface ExternalForVoidInterface | ||
{ | ||
public function run(); | ||
} |
12 changes: 12 additions & 0 deletions
12
...claration/Rector/FunctionLike/ReturnTypeDeclarationRector/Source/ExternalForVoidTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source; | ||
|
||
trait ExternalForVoidTrait | ||
{ | ||
public function run() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\NodeAnalyzer; | ||
|
||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use PhpParser\Node\Stmt\Interface_; | ||
use PhpParser\Node\Stmt\TraitUse; | ||
use Rector\NodeCollector\NodeCollector\NodeRepository; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
|
||
final class ExternalFullyQualifiedAnalyzer | ||
{ | ||
public function __construct( | ||
private NodeNameResolver $nodeNameResolver, | ||
private NodeRepository $nodeRepository | ||
) { | ||
} | ||
|
||
public function hasExternalFullyQualifieds(ClassLike $classLike): bool | ||
{ | ||
if ($classLike instanceof Class_ || $classLike instanceof Interface_) { | ||
$extends = $classLike->extends ?? []; | ||
} else { | ||
$extends = []; | ||
} | ||
|
||
/** @var FullyQualified[] $extends */ | ||
$extends = $extends instanceof FullyQualified | ||
? [$extends] | ||
: $extends; | ||
|
||
/** @var FullyQualified[] $implements */ | ||
$implements = $classLike instanceof Class_ ? $classLike->implements : []; | ||
|
||
$parentClassesAndInterfaces = array_merge($extends, $implements); | ||
$hasExternalClassOrInterface = $this->hasExternalClassOrInterface($parentClassesAndInterfaces); | ||
if ($hasExternalClassOrInterface) { | ||
return true; | ||
} | ||
|
||
/** @var TraitUse[] $traitUses */ | ||
$traitUses = $classLike->getTraitUses(); | ||
return $this->hasExternalTrait($traitUses); | ||
} | ||
|
||
/** | ||
* @param FullyQualified[] $fullyQualifiedClassLikes | ||
*/ | ||
private function hasExternalClassOrInterface(array $fullyQualifiedClassLikes): bool | ||
{ | ||
if ($fullyQualifiedClassLikes === []) { | ||
return false; | ||
} | ||
|
||
foreach ($fullyQualifiedClassLikes as $fullyQualifiedClassLike) { | ||
/** @var string $className */ | ||
$className = $this->nodeNameResolver->getName($fullyQualifiedClassLike); | ||
$isClassFound = (bool) $this->nodeRepository->findClass($className); | ||
$isInterfaceFound = (bool) $this->nodeRepository->findInterface($className); | ||
if ($isClassFound) { | ||
continue; | ||
} | ||
if ($isInterfaceFound) { | ||
continue; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param TraitUse[] $traitUses | ||
*/ | ||
private function hasExternalTrait(array $traitUses): bool | ||
{ | ||
if ($traitUses === []) { | ||
return false; | ||
} | ||
|
||
foreach ($traitUses as $traitUse) { | ||
$traits = $traitUse->traits; | ||
|
||
foreach ($traits as $trait) { | ||
if (! $trait instanceof FullyQualified) { | ||
return false; | ||
} | ||
|
||
/** @var string $traitName */ | ||
$traitName = $this->nodeNameResolver->getName($trait); | ||
$isTraitFound = (bool) $this->nodeRepository->findTrait($traitName); | ||
|
||
if (! $isTraitFound) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |