Skip to content

Commit 82f7133

Browse files
committed
Add tips to the extends internal class rule errors
Fixes #284
1 parent 7a8b9ca commit 82f7133

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

src/Rules/Classes/ClassExtendsInternalClassRule.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,34 @@ public function processNode(Node $node, Scope $scope): array
4646

4747
// @phpstan-ignore-next-line
4848
if (!isset($node->namespacedName)) {
49-
return $this->buildError(null, $extendedClassName);
49+
return [$this->buildError(null, $extendedClassName)->build()];
5050
}
5151

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

5454
if (!NamespaceCheck::isDrupalNamespace($node)) {
55-
return $this->buildError($currentClassName, $extendedClassName);
55+
[$this->buildError($currentClassName, $extendedClassName)->build()];
5656
}
5757

5858
if (NamespaceCheck::isSharedNamespace($node)) {
5959
return [];
6060
}
6161

62-
return $this->buildError($currentClassName, $extendedClassName);
62+
$errorBuilder = $this->buildError($currentClassName, $extendedClassName);
63+
if ($extendedClassName === 'Drupal\Core\Entity\ContentEntityDeleteForm') {
64+
$errorBuilder->tip('Extend \Drupal\Core\Entity\ContentEntityConfirmFormBase. See https://www.drupal.org/node/2491057');
65+
} elseif ((string) $node->extends->slice(0, 2) === 'Drupal\Core') {
66+
$errorBuilder->tip('Read the Drupal core backwards compatibility and internal API policy: https://www.drupal.org/about/core/policies/core-change-policies/drupal-8-and-9-backwards-compatibility-and-internal-api#internal');
67+
}
68+
return [$errorBuilder->build()];
6369
}
6470

65-
private function buildError(?string $currentClassName, string $extendedClassName): array
71+
private function buildError(?string $currentClassName, string $extendedClassName): RuleErrorBuilder
6672
{
67-
return [
68-
RuleErrorBuilder::message(\sprintf(
69-
'%s extends @internal class %s.',
70-
$currentClassName !== null ? \sprintf('Class %s', $currentClassName) : 'Anonymous class',
71-
$extendedClassName
72-
))->build()
73-
];
73+
return RuleErrorBuilder::message(\sprintf(
74+
'%s extends @internal class %s.',
75+
$currentClassName !== null ? \sprintf('Class %s', $currentClassName) : 'Anonymous class',
76+
$extendedClassName
77+
));
7478
}
7579
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Drupal\phpstan_fixtures\Form;
4+
5+
use Drupal\Core\Entity\ContentEntityDeleteForm;
6+
7+
final class ExtendsContentEntityDeleteForm extends ContentEntityDeleteForm {
8+
9+
}

tests/src/Rules/ClassExtendsInternalClassRuleTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public function pluginData(): \Generator
5151
[
5252
[
5353
'Class Drupal\phpstan_fixtures\Internal\ExtendsDrupalCoreInternalClass extends @internal class Drupal\Core\InternalClass.',
54-
7
54+
7,
55+
'Read the Drupal core backwards compatibility and internal API policy: https://www.drupal.org/about/core/policies/core-change-policies/drupal-8-and-9-backwards-compatibility-and-internal-api#internal',
5556
],
5657
],
5758
];
@@ -60,7 +61,8 @@ public function pluginData(): \Generator
6061
[
6162
[
6263
'Class Drupal\phpstan_fixtures\Internal\ExtendsDrupalCorePHPStanDrupalTestsInternalClass extends @internal class Drupal\Core\PHPStanDrupalTests\InternalClass.',
63-
7
64+
7,
65+
'Read the Drupal core backwards compatibility and internal API policy: https://www.drupal.org/about/core/policies/core-change-policies/drupal-8-and-9-backwards-compatibility-and-internal-api#internal',
6466
],
6567
],
6668
];
@@ -119,6 +121,16 @@ public function pluginData(): \Generator
119121
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/Internal/ExtendsPHPStanDrupalModuleWithInternalClassesExternalClass.php',
120122
[],
121123
];
124+
yield 'tip for ContentEntityDeleteForm' => [
125+
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/Form/ExtendsContentEntityDeleteForm.php',
126+
[
127+
[
128+
'Class Drupal\phpstan_fixtures\Form\ExtendsContentEntityDeleteForm extends @internal class Drupal\Core\Entity\ContentEntityDeleteForm.',
129+
7,
130+
'Extend \Drupal\Core\Entity\ContentEntityConfirmFormBase. See https://www.drupal.org/node/2491057'
131+
]
132+
],
133+
];
122134
}
123135

124136
private static function cleanDrupalCoreStubs(): void {

0 commit comments

Comments
 (0)