Skip to content

Commit

Permalink
fix phpdoc parsing with whitespaces/contents after values (#438)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Staab <[email protected]>
  • Loading branch information
staabm and clxmstaab authored Oct 10, 2022
1 parent 8e6836a commit 7ce54d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PhpDoc/PhpDocUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function matchTaintEscape($callLike, Scope $scope): ?string
// atm no resolved phpdoc for methods
// see https://github.com/phpstan/phpstan/discussions/7657
$phpDocString = $methodReflection->getDocComment();
if (null !== $phpDocString && preg_match('/@psalm-taint-escape\s+(\S+)$/m', $phpDocString, $matches)) {
if (null !== $phpDocString && preg_match('/@psalm-taint-escape\s+(\S+).*$/m', $phpDocString, $matches)) {
return $matches[1];
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ private static function matchStringAnnotation(string $annotation, $callLike, Sco
// 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)) {
if (null !== $phpDocString && preg_match('/'.$annotation.'\s+(\S+).*$/m', $phpDocString, $matches)) {
$placeholder = $matches[1];

if (\in_array($placeholder[0], ['"', "'"], true)) {
Expand Down
17 changes: 17 additions & 0 deletions tests/default/data/inference-placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public function fetchOneWithDynamicQueryPartViaVariable(Connection $conn, string
assertType('string|false', $fetchResult);
}

public function bug439(Connection $conn, string $email)
{
$query = 'SELECT email, adaid FROM ada WHERE email = :email AND '.$this->stuffAfterPlaceholder(rand(0, 100));
$fetchResult = $conn->fetchOne($query, ['email' => $email]);
assertType('string|false', $fetchResult);
}

/**
* simulating a dynamic where part, not relevant for the query overall result.
*
Expand Down Expand Up @@ -61,4 +68,14 @@ private static function staticDynamicWhere(int $i)

return implode(' AND ', $where);
}

/**
* @phpstandba-inference-placeholder '1=1' <-- anything behind placeholder value
*
* @return string
*/
private function stuffAfterPlaceholder(int $i)
{
return '';
}
}

0 comments on commit 7ce54d8

Please sign in to comment.