Skip to content

Commit

Permalink
Fix types on walkLiteral() and walkLikeExpression()
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Mar 6, 2022
1 parent bc6c6c9 commit 7b3e58d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 46 deletions.
14 changes: 8 additions & 6 deletions lib/Doctrine/ORM/Query/AST/LikeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Doctrine\ORM\Query\AST;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;

/**
* LikeExpression ::= StringExpression ["NOT"] "LIKE" string ["ESCAPE" char]
*
Expand All @@ -12,21 +14,21 @@
class LikeExpression extends Node
{
/** @var bool */
public $not;
public $not = false;

/** @var Node */
/** @var Node|string */
public $stringExpression;

/** @var InputParameter */
/** @var InputParameter|FunctionNode|PathExpression|Literal */
public $stringPattern;

/** @var Literal|null */
public $escapeChar;

/**
* @param Node $stringExpression
* @param InputParameter $stringPattern
* @param Literal|null $escapeChar
* @param Node|string $stringExpression
* @param InputParameter|FunctionNode|PathExpression|Literal $stringPattern
* @param Literal|null $escapeChar
*/
public function __construct($stringExpression, $stringPattern, $escapeChar = null)
{
Expand Down
6 changes: 5 additions & 1 deletion lib/Doctrine/ORM/Query/AST/Literal.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class Literal extends Node
public const BOOLEAN = 2;
public const NUMERIC = 3;

/** @var int */
/**
* @var int
* @psalm-var self::*
*/
public $type;

/** @var mixed */
Expand All @@ -19,6 +22,7 @@ class Literal extends Node
/**
* @param int $type
* @param mixed $value
* @psalm-param self::* $type
*/
public function __construct($type, $value)
{
Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/ORM/Query/QueryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\Query\AST\PathExpression;
use Exception;
use Stringable;

/**
* Description of QueryException.
Expand Down Expand Up @@ -139,7 +140,7 @@ public static function invalidPathExpression($pathExpr)
}

/**
* @param string $literal
* @param string|Stringable $literal
*
* @return QueryException
*/
Expand Down
9 changes: 6 additions & 3 deletions lib/Doctrine/ORM/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2156,9 +2156,12 @@ public function walkBetweenExpression($betweenExpr)
public function walkLikeExpression($likeExpr)
{
$stringExpr = $likeExpr->stringExpression;
$leftExpr = is_string($stringExpr) && isset($this->queryComponents[$stringExpr]['resultVariable'])
? $this->walkResultVariable($stringExpr)
: $stringExpr->dispatch($this);
if (is_string($stringExpr)) {
assert(isset($this->queryComponents[$stringExpr]['resultVariable']));
$leftExpr = $this->walkResultVariable($stringExpr);
} else {
$leftExpr = $stringExpr->dispatch($this);
}

$sql = $leftExpr . ($likeExpr->not ? ' NOT' : '') . ' LIKE ';

Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Query/TreeWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public function walkInstanceOfExpression($instanceOfExpr);
/**
* Walks down a literal that represents an AST node, thereby generating the appropriate SQL.
*
* @param mixed $literal
* @param AST\Literal $literal
*
* @return string The SQL.
*/
Expand Down
25 changes: 2 additions & 23 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ parameters:
path: lib/Doctrine/ORM/Query/Parser.php

-
message: "#^Parameter \\#2 \\$stringPattern of class Doctrine\\\\ORM\\\\Query\\\\AST\\\\LikeExpression constructor expects Doctrine\\\\ORM\\\\Query\\\\AST\\\\InputParameter, Doctrine\\\\ORM\\\\Query\\\\AST\\\\Node given\\.$#"
message: "#^Parameter \\#2 \\$stringPattern of class Doctrine\\\\ORM\\\\Query\\\\AST\\\\LikeExpression constructor expects Doctrine\\\\ORM\\\\Query\\\\AST\\\\Functions\\\\FunctionNode\\|Doctrine\\\\ORM\\\\Query\\\\AST\\\\InputParameter\\|Doctrine\\\\ORM\\\\Query\\\\AST\\\\Literal\\|Doctrine\\\\ORM\\\\Query\\\\AST\\\\PathExpression, Doctrine\\\\ORM\\\\Query\\\\AST\\\\Node given\\.$#"
count: 1
path: lib/Doctrine/ORM/Query/Parser.php

Expand All @@ -927,11 +927,6 @@ parameters:

-
message: "#^Call to function is_string\\(\\) with Doctrine\\\\ORM\\\\Query\\\\AST\\\\Node will always evaluate to false\\.$#"
count: 2
path: lib/Doctrine/ORM/Query/SqlWalker.php

-
message: "#^Else branch is unreachable because previous condition is always true\\.$#"
count: 1
path: lib/Doctrine/ORM/Query/SqlWalker.php

Expand All @@ -940,21 +935,6 @@ parameters:
count: 1
path: lib/Doctrine/ORM/Query/SqlWalker.php

-
message: "#^Elseif branch is unreachable because previous condition is always true\\.$#"
count: 2
path: lib/Doctrine/ORM/Query/SqlWalker.php

-
message: "#^Instanceof between \\*NEVER\\* and Doctrine\\\\ORM\\\\Query\\\\AST\\\\Functions\\\\FunctionNode will always evaluate to false\\.$#"
count: 1
path: lib/Doctrine/ORM/Query/SqlWalker.php

-
message: "#^Instanceof between \\*NEVER\\* and Doctrine\\\\ORM\\\\Query\\\\AST\\\\PathExpression will always evaluate to false\\.$#"
count: 1
path: lib/Doctrine/ORM/Query/SqlWalker.php

-
message: "#^Method Doctrine\\\\ORM\\\\Query\\\\SqlWalker\\:\\:walkConditionalPrimary\\(\\) should return string but return statement is missing\\.$#"
count: 1
Expand All @@ -972,7 +952,7 @@ parameters:

-
message: "#^Result of && is always false\\.$#"
count: 2
count: 1
path: lib/Doctrine/ORM/Query/SqlWalker.php

-
Expand Down Expand Up @@ -1774,4 +1754,3 @@ parameters:
message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:\\$subClasses\\.$#"
count: 1
path: lib/Doctrine/ORM/Utility/HierarchyDiscriminatorResolver.php

14 changes: 3 additions & 11 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1887,9 +1887,6 @@
<ParamNameMismatch occurrences="1">
<code>$sqlWalker</code>
</ParamNameMismatch>
<PropertyNotSetInConstructor occurrences="1">
<code>$not</code>
</PropertyNotSetInConstructor>
</file>
<file src="lib/Doctrine/ORM/Query/AST/NewObjectExpression.php">
<ParamNameMismatch occurrences="1">
Expand Down Expand Up @@ -2225,12 +2222,11 @@
<PossiblyFalseArgument occurrences="1">
<code>strrpos($fromClassName, '\\')</code>
</PossiblyFalseArgument>
<PossiblyInvalidArgument occurrences="14">
<PossiblyInvalidArgument occurrences="13">
<code>$AST</code>
<code>$conditionalExpression</code>
<code>$expr</code>
<code>$pathExp</code>
<code>$stringExpr</code>
<code>$this-&gt;ConditionalExpression()</code>
<code>$this-&gt;ConditionalExpression()</code>
<code>$this-&gt;lexer-&gt;lookahead['value']</code>
Expand Down Expand Up @@ -2383,12 +2379,9 @@
</PossiblyNullReference>
</file>
<file src="lib/Doctrine/ORM/Query/SqlWalker.php">
<DocblockTypeContradiction occurrences="5">
<code>$likeExpr-&gt;stringPattern instanceof AST\Functions\FunctionNode</code>
<code>$likeExpr-&gt;stringPattern instanceof AST\PathExpression</code>
<DocblockTypeContradiction occurrences="2">
<code>''</code>
<code>is_string($expression)</code>
<code>is_string($stringExpr)</code>
</DocblockTypeContradiction>
<ImplicitToStringCast occurrences="1">
<code>$expr</code>
Expand Down Expand Up @@ -2457,8 +2450,7 @@
<PossiblyNullReference occurrences="1">
<code>dispatch</code>
</PossiblyNullReference>
<RedundantConditionGivenDocblockType occurrences="3">
<code>$likeExpr-&gt;stringPattern instanceof AST\InputParameter</code>
<RedundantConditionGivenDocblockType occurrences="2">
<code>$whereClause !== null</code>
<code>($factor-&gt;not ? 'NOT ' : '') . $this-&gt;walkConditionalPrimary($factor-&gt;conditionalPrimary)</code>
</RedundantConditionGivenDocblockType>
Expand Down

0 comments on commit 7b3e58d

Please sign in to comment.