Skip to content

Commit 950726e

Browse files
staabmclxmstaab
andauthored
implement @phpstandba-inference-placeholder annotation (#413)
Co-authored-by: Markus Staab <[email protected]>
1 parent 7abaa7f commit 950726e

27 files changed

+2747
-340
lines changed

.phpstan-dba-mysqli.cache

Lines changed: 87 additions & 333 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.phpstan-dba-pdo-mysql.cache

Lines changed: 40 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/QueryReflection/QueryReflection.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node\Expr;
88
use PhpParser\Node\Expr\BinaryOp\Concat;
9+
use PhpParser\Node\Identifier;
910
use PHPStan\Analyser\Scope;
1011
use PHPStan\ShouldNotHappenException;
1112
use PHPStan\Type\Constant\ConstantArrayType;
@@ -186,6 +187,27 @@ private function resolveQueryExpr(Expr $queryExpr, Scope $scope): ?string
186187
*/
187188
private function resolveQueryStringExpr(Expr $queryExpr, Scope $scope): ?string
188189
{
190+
if ($queryExpr instanceof Expr\MethodCall && $queryExpr->name instanceof Identifier) {
191+
$classReflection = $scope->getClassReflection();
192+
193+
// XXX atm we only support inference-placeholder for method calls within the same class
194+
if (null !== $classReflection && $classReflection->hasMethod($queryExpr->name->name)) {
195+
$methodReflection = $classReflection->getMethod($queryExpr->name->name, $scope);
196+
197+
// atm no resolved phpdoc for methods
198+
// see https://github.com/phpstan/phpstan/discussions/7657
199+
$phpDocString = $methodReflection->getDocComment();
200+
if (null !== $phpDocString && preg_match('/@phpstandba-inference-placeholder\s+(.+)$/m', $phpDocString, $matches)) {
201+
$placeholder = $matches[1];
202+
203+
if (\in_array($placeholder[0], ['"', "'"], true)) {
204+
$placeholder = trim($placeholder, $placeholder[0]);
205+
}
206+
207+
return $placeholder;
208+
}
209+
}
210+
}
189211
if ($queryExpr instanceof Concat) {
190212
$left = $queryExpr->left;
191213
$right = $queryExpr->right;

tests/default/DbaInferenceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function dataFileAsserts(): iterable
6464
yield from $this->gatherAssertTypes(__DIR__.'/data/mysqli-union-result.php');
6565
yield from $this->gatherAssertTypes(__DIR__.'/data/pdo-default-fetch-types.php');
6666
yield from $this->gatherAssertTypes(__DIR__.'/data/bug372.php');
67+
yield from $this->gatherAssertTypes(__DIR__.'/data/inference-placeholder.php');
6768
}
6869

6970
/**

0 commit comments

Comments
 (0)