Skip to content

Commit

Permalink
improve compatibility with phpstan@bleedingEdge (#404)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Staab <m.staab@complex-it.de>
  • Loading branch information
staabm and clxmstaab authored Jun 9, 2022
1 parent a681d90 commit 5033cd6
Show file tree
Hide file tree
Showing 18 changed files with 546 additions and 225 deletions.
20 changes: 9 additions & 11 deletions .phpstan-dba-mysqli.cache

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions .phpstan-dba-pdo-mysql.cache

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"php": "^7.4 || ^8.0",
"composer-runtime-api": "^2.0",
"composer/semver": "^3.2",
"phpstan/phpstan": "^1.2"
"phpstan/phpstan": "^1.5.6"
},
"require-dev": {
"ext-mysqli": "*",
Expand Down
7 changes: 6 additions & 1 deletion config/extensions.neon
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ services:
tags:
- phpstan.typeSpecifier.methodTypeSpecifyingExtension

-
-
class: staabm\PHPStanDba\Extensions\PdoStatementFetchObjectDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
Expand Down Expand Up @@ -85,3 +85,8 @@ services:
class: staabm\PHPStanDba\Extensions\PdoQuoteDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: staabm\PHPStanDba\Ast\PreviousConnectingVisitor
tags:
- phpstan.parser.richParserNodeVisitor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace staabm\PHPStanDba\QueryReflection;
namespace staabm\PHPStanDba\Ast;

use PhpParser\Node;
use PhpParser\Node\Expr;
Expand All @@ -15,20 +15,6 @@

final class ExpressionFinder
{
/**
* Do not change, part of internal PHPStan naming.
*
* @var string
*/
private const PREVIOUS = 'previous';

/**
* Convention key name in php-parser and PHPStan for parent node.
*
* @var string
*/
private const PARENT = 'parent';

private NodeFinder $nodeFinder;

public function __construct()
Expand Down Expand Up @@ -119,12 +105,14 @@ private function resolveName($node)
}

/**
* XXX At best, this method would be implemented in NodeConnectingVisitor, and the 'firstPreviousAssign' would be directly available.
*
* @param callable(Node $node):bool $filter
*/
private function findFirstPreviousOfNode(Node $node, callable $filter): ?Node
{
// move to previous expression
$previousStatement = $node->getAttribute(self::PREVIOUS);
$previousStatement = $node->getAttribute(PreviousConnectingVisitor::ATTRIBUTE_PREVIOUS);
if (null !== $previousStatement) {
if (!$previousStatement instanceof Node) {
throw new ShouldNotHappenException();
Expand All @@ -138,7 +126,7 @@ private function findFirstPreviousOfNode(Node $node, callable $filter): ?Node
return $this->findFirstPreviousOfNode($previousStatement, $filter);
}

$parent = $node->getAttribute(self::PARENT);
$parent = $node->getAttribute(PreviousConnectingVisitor::ATTRIBUTE_PARENT);
if ($parent instanceof FunctionLike) {
return null;
}
Expand Down
57 changes: 57 additions & 0 deletions src/Ast/PreviousConnectingVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba\Ast;

use function array_pop;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;

final class PreviousConnectingVisitor extends NodeVisitorAbstract
{
public const ATTRIBUTE_PARENT = 'dba-parent';
public const ATTRIBUTE_PREVIOUS = 'dba-previous';

/**
* @var Node[]
*/
private $stack = [];

/**
* @var ?Node
*/
private $previous;

public function beforeTraverse(array $nodes)
{
$this->stack = [];
$this->previous = null;

return null;
}

public function enterNode(Node $node)
{
if (!empty($this->stack)) {
$node->setAttribute(self::ATTRIBUTE_PARENT, $this->stack[\count($this->stack) - 1]);
}

if (null !== $this->previous && $this->previous->getAttribute(self::ATTRIBUTE_PARENT) === $node->getAttribute(self::ATTRIBUTE_PARENT)) {
$node->setAttribute(self::ATTRIBUTE_PREVIOUS, $this->previous);
}

$this->stack[] = $node;

return null;
}

public function leaveNode(Node $node)
{
$this->previous = $node;

array_pop($this->stack);

return null;
}
}
2 changes: 1 addition & 1 deletion src/PdoReflection/PdoStatementReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use staabm\PHPStanDba\QueryReflection\ExpressionFinder;
use staabm\PHPStanDba\Ast\ExpressionFinder;
use staabm\PHPStanDba\QueryReflection\QueryReflection;
use staabm\PHPStanDba\QueryReflection\QueryReflector;

Expand Down
1 change: 1 addition & 0 deletions src/QueryReflection/QueryReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use staabm\PHPStanDba\Analyzer\QueryPlanAnalyzerMysql;
use staabm\PHPStanDba\Analyzer\QueryPlanQueryResolver;
use staabm\PHPStanDba\Analyzer\QueryPlanResult;
use staabm\PHPStanDba\Ast\ExpressionFinder;
use staabm\PHPStanDba\DbaException;
use staabm\PHPStanDba\Error;
use staabm\PHPStanDba\UnresolvableQueryException;
Expand Down
Loading

0 comments on commit 5033cd6

Please sign in to comment.