Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect when a class extends a class that has been flagged as @internal #248

Merged
merged 1 commit into from
Dec 1, 2021
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
5 changes: 5 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ services:
-
class: mglaman\PHPStanDrupal\Reflection\EntityFieldsViaMagicReflectionExtension
tags: [phpstan.broker.propertiesClassReflectionExtension]
-
class: mglaman\PHPStanDrupal\Rules\Classes\ClassExtendsInternalClassRule
tags: [phpstan.rules.rule]
arguments:
reflectionProvider: @reflectionProvider
-
class: mglaman\PHPStanDrupal\Rules\Drupal\LoadIncludes
tags: [phpstan.rules.rule]
Expand Down
39 changes: 39 additions & 0 deletions src/Internal/NamespaceCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);

namespace mglaman\PHPStanDrupal\Internal;

use PhpParser\Node\Stmt\Class_;

/**
* @internal
*/
final class NamespaceCheck
{
public static function isDrupalNamespace(Class_ $class): bool
{
// @phpstan-ignore-next-line
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;
}

// @phpstan-ignore-next-line
if (!isset($class->namespacedName)) {
return false;
}

if (!self::isDrupalNamespace($class)) {
return false;
}

return (string) $class->namespacedName->slice(0, 2) === (string) $class->extends->slice(0, 2);
}
}
75 changes: 75 additions & 0 deletions src/Rules/Classes/ClassExtendsInternalClassRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php declare(strict_types=1);

namespace mglaman\PHPStanDrupal\Rules\Classes;

use mglaman\PHPStanDrupal\Internal\NamespaceCheck;
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 [];
brambaud marked this conversation as resolved.
Show resolved Hide resolved
}

$extendedClassReflection = $this->reflectionProvider->getClass($extendedClassName);
if (!$extendedClassReflection->isInternal()) {
return [];
}

// @phpstan-ignore-next-line
if (!isset($node->namespacedName)) {
return $this->buildError(null, $extendedClassName);
}
brambaud marked this conversation as resolved.
Show resolved Hide resolved

$currentClassName = $node->namespacedName->toString();

if (!NamespaceCheck::isDrupalNamespace($node)) {
return $this->buildError($currentClassName, $extendedClassName);
}

if (NamespaceCheck::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()
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: module_with_internal_classes
type: module
core: 8.x
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 {

}
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 {

}
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 {

}
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 {

}
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 {

}
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 {

}
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 {

}
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 {

}
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 {

}
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 {

}
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 {

}
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 {};
}
}
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 {

}
Loading