Skip to content

Commit 5033cd6

Browse files
staabmclxmstaab
andauthored
improve compatibility with phpstan@bleedingEdge (#404)
Co-authored-by: Markus Staab <[email protected]>
1 parent a681d90 commit 5033cd6

18 files changed

+546
-225
lines changed

.phpstan-dba-mysqli.cache

Lines changed: 9 additions & 11 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: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"php": "^7.4 || ^8.0",
77
"composer-runtime-api": "^2.0",
88
"composer/semver": "^3.2",
9-
"phpstan/phpstan": "^1.2"
9+
"phpstan/phpstan": "^1.5.6"
1010
},
1111
"require-dev": {
1212
"ext-mysqli": "*",

config/extensions.neon

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ services:
5454
tags:
5555
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
5656

57-
-
57+
-
5858
class: staabm\PHPStanDba\Extensions\PdoStatementFetchObjectDynamicReturnTypeExtension
5959
tags:
6060
- phpstan.broker.dynamicMethodReturnTypeExtension
@@ -85,3 +85,8 @@ services:
8585
class: staabm\PHPStanDba\Extensions\PdoQuoteDynamicReturnTypeExtension
8686
tags:
8787
- phpstan.broker.dynamicMethodReturnTypeExtension
88+
89+
-
90+
class: staabm\PHPStanDba\Ast\PreviousConnectingVisitor
91+
tags:
92+
- phpstan.parser.richParserNodeVisitor

src/QueryReflection/ExpressionFinder.php renamed to src/Ast/ExpressionFinder.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace staabm\PHPStanDba\QueryReflection;
5+
namespace staabm\PHPStanDba\Ast;
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
@@ -15,20 +15,6 @@
1515

1616
final class ExpressionFinder
1717
{
18-
/**
19-
* Do not change, part of internal PHPStan naming.
20-
*
21-
* @var string
22-
*/
23-
private const PREVIOUS = 'previous';
24-
25-
/**
26-
* Convention key name in php-parser and PHPStan for parent node.
27-
*
28-
* @var string
29-
*/
30-
private const PARENT = 'parent';
31-
3218
private NodeFinder $nodeFinder;
3319

3420
public function __construct()
@@ -119,12 +105,14 @@ private function resolveName($node)
119105
}
120106

121107
/**
108+
* XXX At best, this method would be implemented in NodeConnectingVisitor, and the 'firstPreviousAssign' would be directly available.
109+
*
122110
* @param callable(Node $node):bool $filter
123111
*/
124112
private function findFirstPreviousOfNode(Node $node, callable $filter): ?Node
125113
{
126114
// move to previous expression
127-
$previousStatement = $node->getAttribute(self::PREVIOUS);
115+
$previousStatement = $node->getAttribute(PreviousConnectingVisitor::ATTRIBUTE_PREVIOUS);
128116
if (null !== $previousStatement) {
129117
if (!$previousStatement instanceof Node) {
130118
throw new ShouldNotHappenException();
@@ -138,7 +126,7 @@ private function findFirstPreviousOfNode(Node $node, callable $filter): ?Node
138126
return $this->findFirstPreviousOfNode($previousStatement, $filter);
139127
}
140128

141-
$parent = $node->getAttribute(self::PARENT);
129+
$parent = $node->getAttribute(PreviousConnectingVisitor::ATTRIBUTE_PARENT);
142130
if ($parent instanceof FunctionLike) {
143131
return null;
144132
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace staabm\PHPStanDba\Ast;
6+
7+
use function array_pop;
8+
use PhpParser\Node;
9+
use PhpParser\NodeVisitorAbstract;
10+
11+
final class PreviousConnectingVisitor extends NodeVisitorAbstract
12+
{
13+
public const ATTRIBUTE_PARENT = 'dba-parent';
14+
public const ATTRIBUTE_PREVIOUS = 'dba-previous';
15+
16+
/**
17+
* @var Node[]
18+
*/
19+
private $stack = [];
20+
21+
/**
22+
* @var ?Node
23+
*/
24+
private $previous;
25+
26+
public function beforeTraverse(array $nodes)
27+
{
28+
$this->stack = [];
29+
$this->previous = null;
30+
31+
return null;
32+
}
33+
34+
public function enterNode(Node $node)
35+
{
36+
if (!empty($this->stack)) {
37+
$node->setAttribute(self::ATTRIBUTE_PARENT, $this->stack[\count($this->stack) - 1]);
38+
}
39+
40+
if (null !== $this->previous && $this->previous->getAttribute(self::ATTRIBUTE_PARENT) === $node->getAttribute(self::ATTRIBUTE_PARENT)) {
41+
$node->setAttribute(self::ATTRIBUTE_PREVIOUS, $this->previous);
42+
}
43+
44+
$this->stack[] = $node;
45+
46+
return null;
47+
}
48+
49+
public function leaveNode(Node $node)
50+
{
51+
$this->previous = $node;
52+
53+
array_pop($this->stack);
54+
55+
return null;
56+
}
57+
}

src/PdoReflection/PdoStatementReflection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use PHPStan\Type\Type;
1414
use PHPStan\Type\TypeCombinator;
1515
use PHPStan\Type\UnionType;
16-
use staabm\PHPStanDba\QueryReflection\ExpressionFinder;
16+
use staabm\PHPStanDba\Ast\ExpressionFinder;
1717
use staabm\PHPStanDba\QueryReflection\QueryReflection;
1818
use staabm\PHPStanDba\QueryReflection\QueryReflector;
1919

src/QueryReflection/QueryReflection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use staabm\PHPStanDba\Analyzer\QueryPlanAnalyzerMysql;
1818
use staabm\PHPStanDba\Analyzer\QueryPlanQueryResolver;
1919
use staabm\PHPStanDba\Analyzer\QueryPlanResult;
20+
use staabm\PHPStanDba\Ast\ExpressionFinder;
2021
use staabm\PHPStanDba\DbaException;
2122
use staabm\PHPStanDba\Error;
2223
use staabm\PHPStanDba\UnresolvableQueryException;

0 commit comments

Comments
 (0)