Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Analyser/ExprHandler/NullsafeMethodCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\NullsafeMethodCallExpressionNode;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -87,6 +88,8 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
{
$beforeScope = $scope;
$calledOnType = $scope->getScopeType($expr->var);
$calledOnNativeType = $scope->getScopeNativeType($expr->var);
$scopeBeforeNullsafe = $scope;
$varType = $scope->getType($expr->var);

Expand Down Expand Up @@ -118,6 +121,10 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$scope = $scope->mergeWith($scopeBeforeNullsafe);
}

// the nullsafe operation is processed; emit a virtual node carrying the
// receiver's entry-scope type so its rule does not re-ask the scope
$nodeScopeResolver->callNodeCallbackWithExpression($nodeCallback, new NullsafeMethodCallExpressionNode($expr, $calledOnType, $calledOnNativeType), $beforeScope, $storage, $context);

return $this->expressionResultFactory->create(
$scope,
beforeScope: $beforeScope,
Expand Down
7 changes: 7 additions & 0 deletions src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\NullsafePropertyFetchExpressionNode;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -87,6 +88,8 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
{
$beforeScope = $scope;
$calledOnType = $scope->getScopeType($expr->var);
$calledOnNativeType = $scope->getScopeNativeType($expr->var);
$nonNullabilityResult = $this->nonNullabilityHelper->ensureShallowNonNullability($scope, $scope, $expr->var);
$attributes = array_merge($expr->getAttributes(), ['virtualNullsafePropertyFetch' => true]);
unset($attributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
Expand All @@ -97,6 +100,10 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
), $nonNullabilityResult->getScope(), $storage, $nodeCallback, $context);
$scope = $this->nonNullabilityHelper->revertNonNullability($exprResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions());

// the nullsafe operation is processed; emit a virtual node carrying the
// receiver's entry-scope type so its rule does not re-ask the scope
$nodeScopeResolver->callNodeCallbackWithExpression($nodeCallback, new NullsafePropertyFetchExpressionNode($expr, $calledOnType, $calledOnNativeType), $beforeScope, $storage, $context);

return $this->expressionResultFactory->create(
$scope,
beforeScope: $beforeScope,
Expand Down
13 changes: 13 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
use PHPStan\Node\Expr\UnsetOffsetExpr;
use PHPStan\Node\FinallyExitPointsNode;
use PHPStan\Node\FunctionCallableNode;
use PHPStan\Node\FunctionCallExpressionNode;
use PHPStan\Node\FunctionReturnStatementsNode;
use PHPStan\Node\InArrowFunctionNode;
use PHPStan\Node\InClassMethodNode;
Expand All @@ -95,13 +96,15 @@
use PHPStan\Node\InTraitNode;
use PHPStan\Node\InvalidateExprNode;
use PHPStan\Node\MethodCallableNode;
use PHPStan\Node\MethodCallExpressionNode;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Node\NoopExpressionNode;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Node\PropertyHookReturnStatementsNode;
use PHPStan\Node\PropertyHookStatementNode;
use PHPStan\Node\ReturnStatement;
use PHPStan\Node\StaticMethodCallableNode;
use PHPStan\Node\StaticMethodCallExpressionNode;
use PHPStan\Node\SwitchConditionArm;
use PHPStan\Node\SwitchConditionNode;
use PHPStan\Node\UnreachableStatementNode;
Expand Down Expand Up @@ -2804,6 +2807,16 @@ public function processExprNode(
if ($exprHandler !== null) {
$expressionResult = $exprHandler->processExpr($this, $stmt, $expr, $scope, $storage, $nodeCallback, $context);
$this->storeExpressionResult($storage, $expr, $expressionResult);
// the call is now processed and stored; emit a virtual node so
// impossible-check rules run on the fully processed call instead of
// asking the scope before the call node itself is processed
if ($expr instanceof FuncCall) {
$this->callNodeCallbackWithExpression($nodeCallback, new FunctionCallExpressionNode($expr), $scope, $storage, $context);
} elseif ($expr instanceof MethodCall) {
$this->callNodeCallbackWithExpression($nodeCallback, new MethodCallExpressionNode($expr), $scope, $storage, $context);
} elseif ($expr instanceof StaticCall) {
$this->callNodeCallbackWithExpression($nodeCallback, new StaticMethodCallExpressionNode($expr), $scope, $storage, $context);
}
return $expressionResult;
}

Expand Down
45 changes: 45 additions & 0 deletions src/Node/FunctionCallExpressionNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace PHPStan\Node;

use Override;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\NodeAbstract;

/**
* Emitted by NodeScopeResolver once the call has been processed and stored, so
* rules listening on it (e.g. the impossible-check rules) run on the fully
* processed call instead of asking the scope to specify its types before the
* call node itself is processed.
*
* @internal
*/
final class FunctionCallExpressionNode extends NodeAbstract implements VirtualNode
{

public function __construct(private FuncCall $originalNode)
{
parent::__construct($originalNode->getAttributes());
}

public function getOriginalNode(): FuncCall
{
return $this->originalNode;
}

#[Override]
public function getType(): string
{
return 'PHPStan_Node_FunctionCallExpressionNode';
}

/**
* @return string[]
*/
#[Override]
public function getSubNodeNames(): array
{
return [];
}

}
45 changes: 45 additions & 0 deletions src/Node/MethodCallExpressionNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace PHPStan\Node;

use Override;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\NodeAbstract;

/**
* Emitted by NodeScopeResolver once the call has been processed and stored, so
* rules listening on it (e.g. the impossible-check rules) run on the fully
* processed call instead of asking the scope to specify its types before the
* call node itself is processed.
*
* @internal
*/
final class MethodCallExpressionNode extends NodeAbstract implements VirtualNode
{

public function __construct(private MethodCall $originalNode)
{
parent::__construct($originalNode->getAttributes());
}

public function getOriginalNode(): MethodCall
{
return $this->originalNode;
}

#[Override]
public function getType(): string
{
return 'PHPStan_Node_MethodCallExpressionNode';
}

/**
* @return string[]
*/
#[Override]
public function getSubNodeNames(): array
{
return [];
}

}
59 changes: 59 additions & 0 deletions src/Node/NullsafeMethodCallExpressionNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types = 1);

namespace PHPStan\Node;

use Override;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\NodeAbstract;
use PHPStan\Type\Type;

/**
* Emitted by NullsafeMethodCallHandler once the receiver has been processed, so
* NullsafeMethodCallRule reads the receiver's (possibly null) type from the carried
* type instead of asking the scope for the receiver type before it is processed.
*
* @internal
*/
final class NullsafeMethodCallExpressionNode extends NodeAbstract implements VirtualNode
{

public function __construct(
private NullsafeMethodCall $originalNode,
private Type $calledOnType,
private Type $calledOnNativeType,
)
{
parent::__construct($originalNode->getAttributes());
}

public function getOriginalNode(): NullsafeMethodCall
{
return $this->originalNode;
}

public function getCalledOnType(): Type
{
return $this->calledOnType;
}

public function getCalledOnNativeType(): Type
{
return $this->calledOnNativeType;
}

#[Override]
public function getType(): string
{
return 'PHPStan_Node_NullsafeMethodCallExpressionNode';
}

/**
* @return string[]
*/
#[Override]
public function getSubNodeNames(): array
{
return [];
}

}
60 changes: 60 additions & 0 deletions src/Node/NullsafePropertyFetchExpressionNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace PHPStan\Node;

use Override;
use PhpParser\Node\Expr\NullsafePropertyFetch;
use PhpParser\NodeAbstract;
use PHPStan\Type\Type;

/**
* Emitted by NullsafePropertyFetchHandler once the receiver has been processed, so
* NullsafePropertyFetchRule reads the receiver's (possibly null) type from the
* carried type instead of asking the scope for the receiver type before it is
* processed.
*
* @internal
*/
final class NullsafePropertyFetchExpressionNode extends NodeAbstract implements VirtualNode
{

public function __construct(
private NullsafePropertyFetch $originalNode,
private Type $calledOnType,
private Type $calledOnNativeType,
)
{
parent::__construct($originalNode->getAttributes());
}

public function getOriginalNode(): NullsafePropertyFetch
{
return $this->originalNode;
}

public function getCalledOnType(): Type
{
return $this->calledOnType;
}

public function getCalledOnNativeType(): Type
{
return $this->calledOnNativeType;
}

#[Override]
public function getType(): string
{
return 'PHPStan_Node_NullsafePropertyFetchExpressionNode';
}

/**
* @return string[]
*/
#[Override]
public function getSubNodeNames(): array
{
return [];
}

}
45 changes: 45 additions & 0 deletions src/Node/StaticMethodCallExpressionNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace PHPStan\Node;

use Override;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\NodeAbstract;

/**
* Emitted by NodeScopeResolver once the call has been processed and stored, so
* rules listening on it (e.g. the impossible-check rules) run on the fully
* processed call instead of asking the scope to specify its types before the
* call node itself is processed.
*
* @internal
*/
final class StaticMethodCallExpressionNode extends NodeAbstract implements VirtualNode
{

public function __construct(private StaticCall $originalNode)
{
parent::__construct($originalNode->getAttributes());
}

public function getOriginalNode(): StaticCall
{
return $this->originalNode;
}

#[Override]
public function getType(): string
{
return 'PHPStan_Node_StaticMethodCallExpressionNode';
}

/**
* @return string[]
*/
#[Override]
public function getSubNodeNames(): array
{
return [];
}

}
Loading
Loading