Skip to content

Commit e53fa7b

Browse files
authored
Merge pull request #189 from mglaman/improve-annotation-inspection
Use regex for better annotation key detection
2 parents 569a1ea + 00e255f commit e53fa7b

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/Rules/Deprecations/ConfigEntityConfigExportRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\ClassReflection;
88
use PHPStan\Reflection\ReflectionProvider;
9+
use PHPStan\ShouldNotHappenException;
910

1011
final class ConfigEntityConfigExportRule extends DeprecatedAnnotationsRuleBase
1112
{
@@ -23,8 +24,11 @@ protected function doProcessNode(ClassReflection $reflection, Node\Stmt\Class_ $
2324
if ($annotation === null) {
2425
return [];
2526
}
26-
$hasMatch = strpos($annotation->getPhpDocString(), 'config_export = {') === false;
27-
if ($hasMatch) {
27+
$hasMatch = preg_match('/config_export\s?=\s?{/', $annotation->getPhpDocString());
28+
if ($hasMatch === false) {
29+
throw new ShouldNotHappenException('Unexpected error when trying to run match on phpDoc string.');
30+
}
31+
if ($hasMatch === 0) {
2832
return [
2933
'Configuration entity must define a `config_export` key. See https://www.drupal.org/node/2481909',
3034
];

src/Rules/Deprecations/PluginAnnotationContextDefinitionsRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\ClassReflection;
88
use PHPStan\Reflection\ReflectionProvider;
9+
use PHPStan\ShouldNotHappenException;
910

1011
final class PluginAnnotationContextDefinitionsRule extends DeprecatedAnnotationsRuleBase
1112
{
@@ -23,8 +24,11 @@ protected function doProcessNode(ClassReflection $reflection, Node\Stmt\Class_ $
2324
if ($annotation === null) {
2425
return [];
2526
}
26-
$hasMatch = strpos($annotation->getPhpDocString(), 'context = {') !== false;
27-
if ($hasMatch) {
27+
$hasMatch = preg_match('/context\s?=\s?{/', $annotation->getPhpDocString());
28+
if ($hasMatch === false) {
29+
throw new ShouldNotHappenException('Unexpected error when trying to run match on phpDoc string.');
30+
}
31+
if ($hasMatch === 1) {
2832
return [
2933
'Providing context definitions via the "context" key is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use the "context_definitions" key instead.',
3034
];

0 commit comments

Comments
 (0)