Skip to content

Commit ce1e7a1

Browse files
Boegiemglaman
andauthored
Update phpstan/phpstan related dependencies to ^2.0" (#805)
* Update phpstan/phpstan related dependencies to ^2.0" * Other versions now being also 2.0 until the next minor bump * PHPStan errors, low hanging fruit edition - Part I * PHPStan errors, low hanging fruit edition - Part II * Big one... * PHPStan errors, low hanging fruit edition - Part III * PHPStan errors, low hanging fruit edition - Part IV * PHPStan errors, low hanging fruit edition - Part V * PHPUnit fix * PHPCS fixes * PHPUnit fix * More brain = less errors * PHPUnit D9 fix * Temporary here until drupal core is also on phpstan/*:^2. * Temporary here until drupal core is also on phpstan/*:^2. * Temporary here until drupal core is also on phpstan/*:^2. * Yawn... * Yawn... * FFS WTFBBQ * Cleanup * More brain = less errors * Update src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php Co-authored-by: Matt Glaman <[email protected]> --------- Co-authored-by: Matt Glaman <[email protected]>
1 parent 2ff2cc8 commit ce1e7a1

File tree

52 files changed

+268
-104
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+268
-104
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ jobs:
121121
- name: "require phpstan-drupal"
122122
run: |
123123
cd ~/drupal
124-
COMPOSER_MEMORY_LIMIT=-1 composer require mglaman/phpstan-drupal "${{ steps.branch_alias.outputs.VERSION_ALIAS }} as 1.2.99" phpstan/extension-installer --with-all-dependencies
124+
COMPOSER_MEMORY_LIMIT=-1 composer require drupal/core-dev "phpstan/phpstan:^2.0" "phpstan/phpstan-phpunit:^2.0" mglaman/phpstan-drupal "${{ steps.branch_alias.outputs.VERSION_ALIAS }} as 2.99.99" phpstan/extension-installer --with-all-dependencies
125125
cp $GITHUB_WORKSPACE/tests/fixtures/config/drupal-phpstan.neon phpstan.neon
126126
- name: "Test core/install.php"
127127
run: |
@@ -253,7 +253,7 @@ jobs:
253253
- name: "require phpstan-drupal"
254254
run: |
255255
cd ${{ runner.temp }}/drupal
256-
composer require --dev mglaman/phpstan-drupal "${{ steps.branch_alias.outputs.VERSION_ALIAS }} as 1.1.99" --with-all-dependencies
256+
composer require drupal/core-dev "phpstan/phpstan:^2.0" "phpstan/phpstan-phpunit:^2.0" mglaman/phpstan-drupal "${{ steps.branch_alias.outputs.VERSION_ALIAS }} as 2.99.99" phpstan/extension-installer --with-all-dependencies
257257
- name: "Check baseline"
258258
run: |
259259
cd ${{ runner.temp }}/drupal

.github/workflows/phpstan-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
matrix:
1515
phpstan:
16-
- '1.11.x-dev'
16+
- '2.0.x-dev'
1717
steps:
1818
- name: "Checkout"
1919
uses: "actions/checkout@v4"

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
],
1212
"require": {
1313
"php": "^8.1",
14-
"phpstan/phpstan": "^1.10.56",
15-
"phpstan/phpstan-deprecation-rules": "^1.1.4",
14+
"phpstan/phpstan": "^2.0",
15+
"phpstan/phpstan-deprecation-rules": "^2.0",
1616
"symfony/finder": "^4.2 || ^5.0 || ^6.0 || ^7.0",
1717
"symfony/yaml": "^4.2|| ^5.0 || ^6.0 || ^7.0",
1818
"webflo/drupal-finder": "^1.3.1"
@@ -22,8 +22,8 @@
2222
"composer/installers": "^1.9",
2323
"drupal/core-recommended": "^10",
2424
"drush/drush": "^10.0 || ^11 || ^12 || ^13@beta",
25-
"phpstan/extension-installer": "^1.1",
26-
"phpstan/phpstan-strict-rules": "^1.0",
25+
"phpstan/extension-installer": "1.4.3",
26+
"phpstan/phpstan-strict-rules": "^2.0",
2727
"phpunit/phpunit": "^8.5 || ^9 || ^10 || ^11",
2828
"slevomat/coding-standard": "^7.1",
2929
"squizlabs/php_codesniffer": "^3.3",

src/Drupal/DrupalAutoloader.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ public function register(Container $container): void
193193
$levels = 3;
194194
}
195195
$drushDir = dirname($reflect->getFileName(), $levels);
196-
/** @var \SplFileInfo $file */
197196
foreach (Finder::create()->files()->name('*.inc')->in($drushDir . '/includes') as $file) {
198197
require_once $file->getPathname();
199198
}
@@ -254,7 +253,6 @@ class: Drupal\jsonapi\Routing\JsonApiParamEnhancer
254253

255254
protected function loadLegacyIncludes(): void
256255
{
257-
/** @var \SplFileInfo $file */
258256
foreach (Finder::create()->files()->name('*.inc')->in($this->drupalRoot . '/core/includes') as $file) {
259257
require_once $file->getPathname();
260258
}

src/Reflection/EntityFieldsViaMagicReflectionExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PHPStan\Reflection\ClassReflection;
77
use PHPStan\Reflection\PropertiesClassReflectionExtension;
88
use PHPStan\Reflection\PropertyReflection;
9-
use PHPStan\TrinaryLogic;
9+
use PHPStan\Type\IsSuperTypeOfResult;
1010
use PHPStan\Type\ObjectType;
1111
use function array_key_exists;
1212

@@ -63,7 +63,7 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa
6363
throw new LogicException($classReflection->getName() . "::$propertyName should be handled earlier.");
6464
}
6565

66-
public static function classObjectIsSuperOfInterface(string $name, ObjectType $interfaceObject) : TrinaryLogic
66+
public static function classObjectIsSuperOfInterface(string $name, ObjectType $interfaceObject) : IsSuperTypeOfResult
6767
{
6868
return $interfaceObject->isSuperTypeOf(new ObjectType($name));
6969
}

src/Reflection/FieldItemListMethodReflection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getPrototype(): ClassMemberReflection
6161
}
6262

6363
/**
64-
* @return \PHPStan\Reflection\ParametersAcceptor[]
64+
* @return list<\PHPStan\Reflection\ParametersAcceptor>
6565
*/
6666
public function getVariants(): array
6767
{

src/Rules/Classes/ClassExtendsInternalClassRule.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,47 @@ public function processNode(Node $node, Scope $scope): array
4848
}
4949

5050
if (!isset($node->namespacedName)) {
51-
return [$this->buildError(null, $extendedClassName)->build()];
51+
return $this->buildError(null, $extendedClassName, null);
5252
}
5353

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

5656
if (!NamespaceCheck::isDrupalNamespace($node)) {
57-
return [$this->buildError($currentClassName, $extendedClassName)->build()];
57+
return $this->buildError($currentClassName, $extendedClassName, null);
5858
}
5959

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

64-
$errorBuilder = $this->buildError($currentClassName, $extendedClassName);
64+
$tip = null;
6565
if ($extendedClassName === 'Drupal\Core\Entity\ContentEntityDeleteForm') {
66-
$errorBuilder->tip('Extend \Drupal\Core\Entity\ContentEntityConfirmFormBase. See https://www.drupal.org/node/2491057');
66+
$tip = 'Extend \Drupal\Core\Entity\ContentEntityConfirmFormBase. See https://www.drupal.org/node/2491057';
6767
} elseif ((string) $node->extends->slice(0, 2) === 'Drupal\Core') {
68-
$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');
68+
$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';
6969
}
70-
return [$errorBuilder->build()];
70+
71+
return $this->buildError(
72+
$currentClassName,
73+
$extendedClassName,
74+
$tip
75+
);
7176
}
7277

73-
private function buildError(?string $currentClassName, string $extendedClassName): RuleErrorBuilder
78+
/**
79+
* @return list<\PHPStan\Rules\IdentifierRuleError>
80+
*/
81+
private function buildError(?string $currentClassName, string $extendedClassName, ?string $tip): array
7482
{
75-
return RuleErrorBuilder::message(sprintf(
83+
$ruleErrorBuilder = RuleErrorBuilder::message(sprintf(
7684
'%s extends @internal class %s.',
7785
$currentClassName !== null ? sprintf('Class %s', $currentClassName) : 'Anonymous class',
7886
$extendedClassName
79-
));
87+
))->identifier('classExtendsInternalClass.classExtendsInternalClass');
88+
if ($tip !== null) {
89+
$ruleErrorBuilder->tip($tip);
90+
}
91+
92+
return [$ruleErrorBuilder->build()];
8093
}
8194
}

src/Rules/Classes/PluginManagerInspectionRule.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ public function processNode(Node $node, Scope $scope): array
7878
$errors[] = RuleErrorBuilder::message(
7979
'Plugin managers should call alterInfo to allow plugin definitions to be altered.'
8080
)
81-
->identifier('plugin.manager.alterInfoMissing')
8281
->tip('For example, to invoke hook_mymodule_data_alter() call alterInfo with "mymodule_data".')
8382
->line($node->getStartLine())
83+
->identifier('pluginManagerInspection.alterInfoMissing')
8484
->build();
8585
}
8686

@@ -110,6 +110,9 @@ private function isYamlDiscovery(Node\Stmt\Class_ $class): bool
110110
return false;
111111
}
112112

113+
/**
114+
* @return list<\PHPStan\Rules\IdentifierRuleError>
115+
*/
113116
private function inspectYamlPluginManager(Node\Stmt\Class_ $class, Node\Stmt\ClassMethod $constructorMethodNode): array
114117
{
115118
$errors = [];
@@ -119,7 +122,11 @@ private function inspectYamlPluginManager(Node\Stmt\Class_ $class, Node\Stmt\Cla
119122
$constructor = $reflection->getConstructor();
120123

121124
if ($constructor->getDeclaringClass()->getName() !== $fqn) {
122-
$errors[] = sprintf('%s must override __construct if using YAML plugins.', $fqn);
125+
$errors[] = RuleErrorBuilder::message(
126+
sprintf('%s must override __construct if using YAML plugins.', $fqn)
127+
)
128+
->identifier('pluginManagerInspection.callAlterInfo')
129+
->build();
123130
} else {
124131
foreach ($constructorMethodNode->stmts ?? [] as $constructorStmt) {
125132
if ($constructorStmt instanceof Node\Stmt\Expression) {
@@ -130,7 +137,11 @@ private function inspectYamlPluginManager(Node\Stmt\Class_ $class, Node\Stmt\Cla
130137
&& ((string)$constructorStmt->class === 'parent')
131138
&& $constructorStmt->name instanceof Node\Identifier
132139
&& $constructorStmt->name->name === '__construct') {
133-
$errors[] = 'YAML plugin managers should not invoke its parent constructor.';
140+
$errors[] = RuleErrorBuilder::message(
141+
'YAML plugin managers should not invoke its parent constructor.'
142+
)
143+
->identifier('pluginManagerInspection.yamlPluginManagersInvokesParentConstructor')
144+
->build();
134145
}
135146
}
136147
}

src/Rules/Deprecations/AccessDeprecatedConstant.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Reflection\ReflectionProvider;
1010
use PHPStan\Rules\Rule;
11+
use PHPStan\Rules\RuleErrorBuilder;
1112
use function array_merge;
1213
use function explode;
1314
use function sprintf;
@@ -124,7 +125,11 @@ public function processNode(Node $node, Scope $scope): array
124125
$constantName = $this->reflectionProvider->resolveConstantName($node->name, $scope);
125126
if (isset($deprecatedConstants[$constantName])) {
126127
return [
127-
sprintf('Call to deprecated constant %s: %s', $constantName, $deprecatedConstants[$constantName])
128+
RuleErrorBuilder::message(
129+
sprintf('Call to deprecated constant %s: %s', $constantName, $deprecatedConstants[$constantName])
130+
)
131+
->identifier("accessDeprecatedConstant.deprecatedConstantCalled")
132+
->build()
128133
];
129134
}
130135
return [];

src/Rules/Deprecations/ConditionManagerCreateInstanceContextConfigurationRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function processNode(Node $node, Scope $scope): array
5151
return [
5252
RuleErrorBuilder::message('Passing context values to plugins via configuration is deprecated in drupal:9.1.0 and will be removed before drupal:10.0.0. Instead, call ::setContextValue() on the plugin itself. See https://www.drupal.org/node/3120980')
5353
->line($node->getStartLine())
54+
->identifier("conditionManagerCreateInstanceContextConfiguration.callSetContextValue")
5455
->build()
5556
];
5657
}

0 commit comments

Comments
 (0)