Skip to content
Open
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
24 changes: 12 additions & 12 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ public function withRecordedStatementDelta(self $recordedEntry, self $recordedEx
$conditionalExpressions,
[],
[],
[],
$this->inFunctionCallsStack,
$this->inFirstLevelStatement,
$recordedExit->afterExtractCall,
);
Expand Down Expand Up @@ -3041,7 +3041,7 @@ public function enterExpressionAssign(Expr $expr, bool $isPlainWrite = true): se
$this->isInFirstLevelStatement(),
$currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
[],
$this->inFunctionCallsStack,
$this->afterExtractCall,
$this->parentScope,
$this->nativeTypesPromoted,
Expand Down Expand Up @@ -3072,7 +3072,7 @@ public function exitExpressionAssign(Expr $expr): self
$this->isInFirstLevelStatement(),
$currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
[],
$this->inFunctionCallsStack,
$this->afterExtractCall,
$this->parentScope,
$this->nativeTypesPromoted,
Expand Down Expand Up @@ -3133,7 +3133,7 @@ public function setAllowedUndefinedExpression(Expr $expr): self
$this->isInFirstLevelStatement(),
$this->currentlyAssignedExpressions,
$currentlyAllowedUndefinedExpressions,
[],
$this->inFunctionCallsStack,
$this->afterExtractCall,
$this->parentScope,
$this->nativeTypesPromoted,
Expand Down Expand Up @@ -3164,7 +3164,7 @@ public function unsetAllowedUndefinedExpression(Expr $expr): self
$this->isInFirstLevelStatement(),
$this->currentlyAssignedExpressions,
$currentlyAllowedUndefinedExpressions,
[],
$this->inFunctionCallsStack,
$this->afterExtractCall,
$this->parentScope,
$this->nativeTypesPromoted,
Expand Down Expand Up @@ -4180,7 +4180,7 @@ private function invalidateExpressionWithoutAliases(Expr $expressionToInvalidate
$result[2],
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
[],
$this->inFunctionCallsStack,
$this->inFirstLevelStatement,
$this->afterExtractCall,
);
Expand Down Expand Up @@ -4224,7 +4224,7 @@ private function invalidateMethodsOnExpression(Expr $expressionToInvalidate): se
$result[2],
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
[],
$this->inFunctionCallsStack,
$this->inFirstLevelStatement,
$this->afterExtractCall,
);
Expand Down Expand Up @@ -5079,7 +5079,7 @@ private function mergeWithVariableState(?self $otherScope, bool $preserveVacuous
$conditionalExpressions,
[],
[],
[],
$this->inFunctionCallsStack,
$this->inFirstLevelStatement,
$afterExtractCall,
);
Expand All @@ -5102,7 +5102,7 @@ private function mergeWithVariableState(?self $otherScope, bool $preserveVacuous
$conditionalExpressions,
[],
[],
[],
$this->inFunctionCallsStack,
$this->inFirstLevelStatement,
$afterExtractCall,
);
Expand Down Expand Up @@ -5502,7 +5502,7 @@ public function processFinallyScope(self $finallyScope, self $originalFinallySco
$this->inFirstLevelStatement,
[],
[],
[],
$this->inFunctionCallsStack,
$this->afterExtractCall,
$this->parentScope,
$this->nativeTypesPromoted,
Expand Down Expand Up @@ -5651,7 +5651,7 @@ public function processAlwaysIterableForeachScopeWithoutPollute(self $finalScope
$this->inFirstLevelStatement,
[],
[],
[],
$this->inFunctionCallsStack,
$this->afterExtractCall,
$this->parentScope,
$this->nativeTypesPromoted,
Expand Down Expand Up @@ -5806,7 +5806,7 @@ private function generalizeWithVariableState(self $otherScope, ?array $writableV
$this->inFirstLevelStatement,
[],
[],
[],
$this->inFunctionCallsStack,
$this->afterExtractCall,
$this->parentScope,
$this->nativeTypesPromoted,
Expand Down
55 changes: 55 additions & 0 deletions tests/PHPStan/Rules/ScopeFunctionCallStackInTransitionsRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\ShouldNotHappenException;
use function count;
use function implode;
use function sprintf;

/** @implements Rule<Node\Expr> */
class ScopeFunctionCallStackInTransitionsRule implements Rule
{

public function getNodeType(): string
{
return Node\Expr::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (!$node instanceof MethodCall && !$node instanceof PropertyFetch) {
return [];
}
if (!$node->name instanceof Node\Identifier) {
throw new ShouldNotHappenException();
}

$stack = [];
foreach ($scope->getFunctionCallStackWithParameters() as [$reflection, $parameter]) {
if ($parameter === null) {
throw new ShouldNotHappenException();
}
if ($reflection instanceof FunctionReflection) {
$stack[] = sprintf('%s ($%s)', $reflection->getName(), $parameter->getName());
continue;
}

$stack[] = sprintf('%s::%s ($%s)', $reflection->getDeclaringClass()->getDisplayName(), $reflection->getName(), $parameter->getName());
}

return [
RuleErrorBuilder::message(sprintf(
'%s: %s',
$node->name->name,
count($stack) === 0 ? 'empty' : implode(', ', $stack),
))->identifier('dummy')->build(),
];
}

}
122 changes: 122 additions & 0 deletions tests/PHPStan/Rules/ScopeFunctionCallStackInTransitionsRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules;

use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;

/**
* @extends RuleTestCase<ScopeFunctionCallStackInTransitionsRule>
*/
class ScopeFunctionCallStackInTransitionsRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new ScopeFunctionCallStackInTransitionsRule();
}

#[RequiresPhp('>= 8.0.0')]
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/scope-function-call-stack-transitions.php'], [
[
'm: ScopeFunctionCallStackTransitions\foo ($cb)',
30,
],
[
'm: ScopeFunctionCallStackTransitions\foo ($cb)',
31,
],
[
'p: ScopeFunctionCallStackTransitions\foo ($cb)',
32,
],
[
'm: ScopeFunctionCallStackTransitions\foo ($cb)',
33,
],
[
'x: ScopeFunctionCallStackTransitions\foo ($cb)',
33,
],
[
'm: ScopeFunctionCallStackTransitions\foo ($cb)',
34,
],
[
'm: ScopeFunctionCallStackTransitions\foo ($cb)',
35,
],
[
'p: ScopeFunctionCallStackTransitions\foo ($cb)',
35,
],
[
'p: ScopeFunctionCallStackTransitions\foo ($cb)',
36,
],
[
'm: ScopeFunctionCallStackTransitions\foo ($cb), trim ($string), ScopeFunctionCallStackTransitions\bar ($cb)',
37,
],
[
'm: ScopeFunctionCallStackTransitions\foo ($cb)',
43,
],
[
'm: ScopeFunctionCallStackTransitions\foo ($cb)',
46,
],
[
'm: ScopeFunctionCallStackTransitions\foo ($cb)',
50,
],
[
'm: ScopeFunctionCallStackTransitions\foo ($cb)',
56,
],
[
'm: ScopeFunctionCallStackTransitions\foo ($cb)',
64,
],
[
'm: ScopeFunctionCallStackTransitions\foo ($cb)',
68,
],
[
'm: ScopeFunctionCallStackTransitions\foo ($cb)',
70,
],
[
'm: trim ($string), ScopeFunctionCallStackTransitions\foo ($cb)',
73,
],
[
'm: empty',
79,
],
[
'm: empty',
80,
],
[
'p: empty',
81,
],
[
'm: empty',
82,
],
[
'm: empty',
83,
],
[
'p: empty',
83,
],
]);
}

}
84 changes: 84 additions & 0 deletions tests/PHPStan/Rules/data/scope-function-call-stack-transitions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php // lint >= 8.0

namespace ScopeFunctionCallStackTransitions;

class Foo
{

public ?Foo $p = null;

public ?Foo $x = null;

public function m(): ?Foo
{
return null;
}

}

function foo(callable $cb): void
{
}

function bar(callable $cb): ?Foo
{
return null;
}

function arrowFunctions(Foo $o, ?Foo $a): void
{
foo(fn () => $o->m());
foo(fn () => $o->m() ?? null);
foo(fn () => $o->p ?? null);
foo(fn () => $o->m()->x ?? null);
foo(fn () => $a ?? $o->m());
foo(fn () => isset($o->p) ? $o->m() : null);
foo(fn () => empty($o->p));
foo(fn () => trim(bar(fn () => $o->m() ?? null)));
}

function closures(Foo $o, bool $x): void
{
foo(function () use ($o) {
return $o->m();
});
foo(function () use ($o) {
return $o->m() ?? null;
});
foo(function () use ($o) {
$a = 1;
return $o->m();
});
foo(function () use ($o, $x) {
if ($x) {
$a = 1;
}
return $o->m();
});
foo(function () use ($o) {
try {
$a = 1;
} finally {
$a = 2;
}
return $o->m();
});
foo(function () use ($o) {
foreach ([1, 2] as $i) {
$o->m();
}
return $o->m();
});
trim(foo(function () use ($o) {
return $o->m() ?? null;
}));
}

function outsideAnyCall(Foo $o, ?Foo $a): void
{
$o->m() ?? null;
$a = $o->m();
$o->p ?? null;
$o->m();
$x = isset($o->p) ? $o->m() : null;
}
Loading