Skip to content

Commit

Permalink
Use regex for better annotation key detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mglaman committed Jul 16, 2021
1 parent 569a1ea commit 00e255f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Rules/Deprecations/ConfigEntityConfigExportRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;

final class ConfigEntityConfigExportRule extends DeprecatedAnnotationsRuleBase
{
Expand All @@ -23,8 +24,11 @@ protected function doProcessNode(ClassReflection $reflection, Node\Stmt\Class_ $
if ($annotation === null) {
return [];
}
$hasMatch = strpos($annotation->getPhpDocString(), 'config_export = {') === false;
if ($hasMatch) {
$hasMatch = preg_match('/config_export\s?=\s?{/', $annotation->getPhpDocString());
if ($hasMatch === false) {
throw new ShouldNotHappenException('Unexpected error when trying to run match on phpDoc string.');
}
if ($hasMatch === 0) {
return [
'Configuration entity must define a `config_export` key. See https://www.drupal.org/node/2481909',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;

final class PluginAnnotationContextDefinitionsRule extends DeprecatedAnnotationsRuleBase
{
Expand All @@ -23,8 +24,11 @@ protected function doProcessNode(ClassReflection $reflection, Node\Stmt\Class_ $
if ($annotation === null) {
return [];
}
$hasMatch = strpos($annotation->getPhpDocString(), 'context = {') !== false;
if ($hasMatch) {
$hasMatch = preg_match('/context\s?=\s?{/', $annotation->getPhpDocString());
if ($hasMatch === false) {
throw new ShouldNotHappenException('Unexpected error when trying to run match on phpDoc string.');
}
if ($hasMatch === 1) {
return [
'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.',
];
Expand Down

0 comments on commit 00e255f

Please sign in to comment.