Skip to content

Commit

Permalink
extract PhpDocUtil (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Oct 3, 2022
1 parent ac2596a commit c280be4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 27 deletions.
77 changes: 77 additions & 0 deletions src/PhpDoc/PhpDocUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace staabm\PHPStanDba\PhpDoc;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;

final class PhpDocUtil
{
/**
* @api
*/
public static function commentContains(string $text, CallLike $callike, Scope $scope): bool
{
$methodReflection = self::getMethodReflection($callike, $scope);

if (null !== $methodReflection) {
// atm no resolved phpdoc for methods
// see https://github.com/phpstan/phpstan/discussions/7657
$phpDocString = $methodReflection->getDocComment();
if (null !== $phpDocString && false !== strpos($phpDocString, $text)) {
return true;
}
}

return false;
}

/**
* Returns a unquoted plain string following a annotation.
*
* @param string $annotation e.g. '@phpstandba-inference-placeholder'
*/
public static function matchStringAnnotation(string $annotation, CallLike $callike, Scope $scope): ?string
{
$methodReflection = self::getMethodReflection($callike, $scope);

if (null !== $methodReflection) {
// atm no resolved phpdoc for methods
// see https://github.com/phpstan/phpstan/discussions/7657
$phpDocString = $methodReflection->getDocComment();
if (null !== $phpDocString && preg_match('/'.$annotation.'\s+(.+)$/m', $phpDocString, $matches)) {
$placeholder = $matches[1];

if (\in_array($placeholder[0], ['"', "'"], true)) {
$placeholder = trim($placeholder, $placeholder[0]);
}

return $placeholder;
}
}

return null;
}

private static function getMethodReflection(CallLike $callike, Scope $scope): ?MethodReflection
{
$methodReflection = null;
if ($callike instanceof Expr\StaticCall) {
if ($callike->class instanceof Name && $callike->name instanceof Identifier) {
$classType = $scope->resolveTypeByName($callike->class);
$methodReflection = $scope->getMethodReflection($classType, $callike->name->name);
}
} elseif ($callike instanceof Expr\MethodCall && $callike->name instanceof Identifier) {
$classReflection = $scope->getClassReflection();
if (null !== $classReflection && $classReflection->hasMethod($callike->name->name)) {
$methodReflection = $classReflection->getMethod($callike->name->name, $scope);
}
}

return $methodReflection;
}
}
32 changes: 5 additions & 27 deletions src/QueryReflection/QueryReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\EncapsedStringPart;
use PHPStan\Analyser\Scope;
Expand All @@ -24,6 +22,7 @@
use staabm\PHPStanDba\Ast\ExpressionFinder;
use staabm\PHPStanDba\DbaException;
use staabm\PHPStanDba\Error;
use staabm\PHPStanDba\PhpDoc\PhpDocUtil;
use staabm\PHPStanDba\UnresolvableQueryException;

final class QueryReflection
Expand Down Expand Up @@ -202,34 +201,13 @@ private function resolveQueryStringExpr(Expr $queryExpr, Scope $scope, bool $res
}

if ($queryExpr instanceof Expr\CallLike) {
$methodReflection = null;
if ($queryExpr instanceof Expr\StaticCall) {
if ($queryExpr->class instanceof Name && $queryExpr->name instanceof Identifier) {
$classType = $scope->resolveTypeByName($queryExpr->class);
$methodReflection = $scope->getMethodReflection($classType, $queryExpr->name->name);
}
} elseif ($queryExpr instanceof Expr\MethodCall && $queryExpr->name instanceof Identifier) {
$classReflection = $scope->getClassReflection();
if (null !== $classReflection && $classReflection->hasMethod($queryExpr->name->name)) {
$methodReflection = $classReflection->getMethod($queryExpr->name->name, $scope);
}
}

if (null !== $methodReflection) {
// atm no resolved phpdoc for methods
// see https://github.com/phpstan/phpstan/discussions/7657
$phpDocString = $methodReflection->getDocComment();
if (null !== $phpDocString && preg_match('/@phpstandba-inference-placeholder\s+(.+)$/m', $phpDocString, $matches)) {
$placeholder = $matches[1];
$placeholder = PhpDocUtil::matchStringAnnotation('@phpstandba-inference-placeholder', $queryExpr, $scope);

if (\in_array($placeholder[0], ['"', "'"], true)) {
$placeholder = trim($placeholder, $placeholder[0]);
}

return $placeholder;
}
if (null !== $placeholder) {
return $placeholder;
}
}

if ($queryExpr instanceof Concat) {
$left = $queryExpr->left;
$right = $queryExpr->right;
Expand Down

0 comments on commit c280be4

Please sign in to comment.