-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect when a class extends a class that has been flagged as @internal
- Loading branch information
Showing
18 changed files
with
480 additions
and
0 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
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,47 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Internal; | ||
|
||
use PhpParser\Node\Stmt\Class_; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ClassHelper | ||
{ | ||
public static function isInDrupalNamespace(Class_ $class): bool | ||
{ | ||
if (!isset($class->namespacedName)) { | ||
return false; | ||
} | ||
|
||
return 'Drupal' === (string) $class->namespacedName->slice(0, 1); | ||
} | ||
|
||
public static function isSharedNamespace(Class_ $class): bool | ||
{ | ||
if (!isset($class->extends)) { | ||
return false; | ||
} | ||
|
||
if (!isset($class->namespacedName)) { | ||
return false; | ||
} | ||
|
||
if (!self::isInDrupalNamespace($class)) { | ||
return false; | ||
} | ||
|
||
$classNamespaceBase = (string) $class->namespacedName->slice(0, 2); | ||
$extendedClassNamespaceBase = (string) $class->extends->slice(0, 2); | ||
|
||
if ('Drupal\Core' === $classNamespaceBase && 'Drupal\Component' === $extendedClassNamespaceBase) { | ||
return true; | ||
} | ||
if ('Drupal\Component' === $classNamespaceBase && 'Drupal\Core' === $extendedClassNamespaceBase) { | ||
return true; | ||
} | ||
|
||
return $classNamespaceBase === $extendedClassNamespaceBase; | ||
} | ||
} |
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,74 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Rules\Classes; | ||
|
||
use mglaman\PHPStanDrupal\Internal\ClassHelper; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
class ClassExtendsInternalClassRule implements Rule | ||
{ | ||
/** | ||
* @var ReflectionProvider | ||
*/ | ||
private $reflectionProvider; | ||
|
||
public function __construct(ReflectionProvider $reflectionProvider) | ||
{ | ||
$this->reflectionProvider = $reflectionProvider; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Class_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
/** @var Class_ $node */ | ||
if (!isset($node->extends)) { | ||
return []; | ||
} | ||
|
||
$extendedClassName = $node->extends->toString(); | ||
if (!$this->reflectionProvider->hasClass($extendedClassName)) { | ||
return []; | ||
} | ||
|
||
$extendedClassReflection = $this->reflectionProvider->getClass($extendedClassName); | ||
if (!$extendedClassReflection->isInternal()) { | ||
return []; | ||
} | ||
|
||
if (!isset($node->namespacedName)) { | ||
return $this->buildError(null, $extendedClassName); | ||
} | ||
|
||
$currentClassName = $node->namespacedName->toString(); | ||
|
||
if (!ClassHelper::isInDrupalNamespace($node)) { | ||
return $this->buildError($currentClassName, $extendedClassName); | ||
} | ||
|
||
if (ClassHelper::isSharedNamespace($node)) { | ||
return []; | ||
} | ||
|
||
return $this->buildError($currentClassName, $extendedClassName); | ||
} | ||
|
||
private function buildError(?string $currentClassName, string $extendedClassName): array | ||
{ | ||
return [ | ||
RuleErrorBuilder::message(\sprintf( | ||
'%s extends @internal class %s.', | ||
$currentClassName !== null ? \sprintf('Class %s', $currentClassName) : 'Anonymous class', | ||
$extendedClassName | ||
))->build() | ||
]; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...ixtures/drupal/modules/module_with_internal_classes/module_with_internal_classes.info.yml
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,3 @@ | ||
name: module_with_internal_classes | ||
type: module | ||
core: 8.x |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/drupal/modules/module_with_internal_classes/src/Foo/ExternalClass.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,7 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Drupal\module_with_internal_classes\Foo; | ||
|
||
class ExternalClass { | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
tests/fixtures/drupal/modules/module_with_internal_classes/src/Foo/InternalClass.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 Drupal\module_with_internal_classes\Foo; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class InternalClass { | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/drupal/modules/phpstan_fixtures/src/Internal/DoesNotExtendsAnyClass.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,7 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Drupal\phpstan_fixtures\Internal; | ||
|
||
final class DoesNotExtendsAnyClass { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
.../fixtures/drupal/modules/phpstan_fixtures/src/Internal/ExtendsDrupalCoreExternalClass.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,9 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Drupal\phpstan_fixtures\Internal; | ||
|
||
use Drupal\Core\PHPStanDrupalTests\ExternalClass; | ||
|
||
final class ExtendsDrupalCoreExternalClass extends ExternalClass { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
.../fixtures/drupal/modules/phpstan_fixtures/src/Internal/ExtendsDrupalCoreInternalClass.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,9 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Drupal\phpstan_fixtures\Internal; | ||
|
||
use Drupal\Core\InternalClass; | ||
|
||
final class ExtendsDrupalCoreInternalClass extends InternalClass { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...odules/phpstan_fixtures/src/Internal/ExtendsDrupalCorePHPStanDrupalTestsInternalClass.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,9 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Drupal\phpstan_fixtures\Internal; | ||
|
||
use Drupal\Core\PHPStanDrupalTests\InternalClass; | ||
|
||
final class ExtendsDrupalCorePHPStanDrupalTestsInternalClass extends InternalClass { | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/drupal/modules/phpstan_fixtures/src/Internal/ExtendsInternalClass.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,7 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Drupal\phpstan_fixtures\Internal; | ||
|
||
final class ExtendsInternalClass extends InternalClass { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...stan_fixtures/src/Internal/ExtendsPHPStanDrupalModuleWithInternalClassesExternalClass.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,9 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Drupal\phpstan_fixtures\Internal; | ||
|
||
use Drupal\module_with_internal_classes\Foo\ExternalClass; | ||
|
||
final class ExtendsPHPStanDrupalModuleWithInternalClassesExternalClass extends ExternalClass { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...stan_fixtures/src/Internal/ExtendsPHPStanDrupalModuleWithInternalClassesInternalClass.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,9 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Drupal\phpstan_fixtures\Internal; | ||
|
||
use Drupal\module_with_internal_classes\Foo\InternalClass; | ||
|
||
final class ExtendsPHPStanDrupalModuleWithInternalClassesInternalClass extends InternalClass { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
tests/fixtures/drupal/modules/phpstan_fixtures/src/Internal/ExtendsRootInternalClass.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,9 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Drupal\phpstan_fixtures\Internal; | ||
|
||
use Drupal\phpstan_fixtures\InternalClass; | ||
|
||
final class ExtendsRootInternalClass extends InternalClass { | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
tests/fixtures/drupal/modules/phpstan_fixtures/src/Internal/InternalClass.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 Drupal\phpstan_fixtures\Internal; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class InternalClass { | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...al/modules/phpstan_fixtures/src/Internal/WithAnAnonymousClassExtendingAnInternalClass.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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace fixtures\drupal\modules\phpstan_fixtures\src\Internal; | ||
|
||
use Drupal\module_with_internal_classes\Foo\InternalClass; | ||
|
||
final class WithAnAnonymousClassExtendingAnInternalClass | ||
{ | ||
public function foo(): void | ||
{ | ||
$foo = new class() extends InternalClass {}; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/fixtures/drupal/modules/phpstan_fixtures/src/InternalClass.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 Drupal\phpstan_fixtures; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class InternalClass { | ||
|
||
} |
Oops, something went wrong.