|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace staabm\PHPStanDba\PhpDoc; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr; |
| 6 | +use PhpParser\Node\Expr\CallLike; |
| 7 | +use PhpParser\Node\Identifier; |
| 8 | +use PhpParser\Node\Name; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Reflection\MethodReflection; |
| 11 | + |
| 12 | +final class PhpDocUtil |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @api |
| 16 | + */ |
| 17 | + public static function commentContains(string $text, CallLike $callike, Scope $scope): bool |
| 18 | + { |
| 19 | + $methodReflection = self::getMethodReflection($callike, $scope); |
| 20 | + |
| 21 | + if (null !== $methodReflection) { |
| 22 | + // atm no resolved phpdoc for methods |
| 23 | + // see https://github.com/phpstan/phpstan/discussions/7657 |
| 24 | + $phpDocString = $methodReflection->getDocComment(); |
| 25 | + if (null !== $phpDocString && false !== strpos($phpDocString, $text)) { |
| 26 | + return true; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Returns a unquoted plain string following a annotation. |
| 35 | + * |
| 36 | + * @param string $annotation e.g. '@phpstandba-inference-placeholder' |
| 37 | + */ |
| 38 | + public static function matchStringAnnotation(string $annotation, CallLike $callike, Scope $scope): ?string |
| 39 | + { |
| 40 | + $methodReflection = self::getMethodReflection($callike, $scope); |
| 41 | + |
| 42 | + if (null !== $methodReflection) { |
| 43 | + // atm no resolved phpdoc for methods |
| 44 | + // see https://github.com/phpstan/phpstan/discussions/7657 |
| 45 | + $phpDocString = $methodReflection->getDocComment(); |
| 46 | + if (null !== $phpDocString && preg_match('/'.$annotation.'\s+(.+)$/m', $phpDocString, $matches)) { |
| 47 | + $placeholder = $matches[1]; |
| 48 | + |
| 49 | + if (\in_array($placeholder[0], ['"', "'"], true)) { |
| 50 | + $placeholder = trim($placeholder, $placeholder[0]); |
| 51 | + } |
| 52 | + |
| 53 | + return $placeholder; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + private static function getMethodReflection(CallLike $callike, Scope $scope): ?MethodReflection |
| 61 | + { |
| 62 | + $methodReflection = null; |
| 63 | + if ($callike instanceof Expr\StaticCall) { |
| 64 | + if ($callike->class instanceof Name && $callike->name instanceof Identifier) { |
| 65 | + $classType = $scope->resolveTypeByName($callike->class); |
| 66 | + $methodReflection = $scope->getMethodReflection($classType, $callike->name->name); |
| 67 | + } |
| 68 | + } elseif ($callike instanceof Expr\MethodCall && $callike->name instanceof Identifier) { |
| 69 | + $classReflection = $scope->getClassReflection(); |
| 70 | + if (null !== $classReflection && $classReflection->hasMethod($callike->name->name)) { |
| 71 | + $methodReflection = $classReflection->getMethod($callike->name->name, $scope); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return $methodReflection; |
| 76 | + } |
| 77 | +} |
0 commit comments