Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 7 additions & 4 deletions src/Analyser/ExprHandler/MethodCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$methodReflection->getNamedArgumentsVariants(),
);

$methodThrowPoint = $this->getMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope);
if ($methodThrowPoint !== null) {
$throwPoints[] = $methodThrowPoint;
}
}
} else {
$methodNameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep());
Expand Down Expand Up @@ -157,6 +153,13 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$scope = $argsResult->getScope();

if ($methodReflection !== null) {
if ($parametersAcceptor !== null) {
Comment thread
staabm marked this conversation as resolved.
$methodThrowPoint = $this->getMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope);
if ($methodThrowPoint !== null) {
$throwPoints[] = $methodThrowPoint;
}
}

if ($methodReflection->getName() === '__construct' || $methodReflection->hasSideEffects()->yes()) {
$nodeScopeResolver->callNodeCallback($nodeCallback, new InvalidateExprNode($normalizedExpr->var), $scope, $storage);
$scope = $scope->invalidateExpression($normalizedExpr->var, true, $methodReflection->getDeclaringClass());
Expand Down
12 changes: 7 additions & 5 deletions src/Analyser/ExprHandler/StaticCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$methodReflection->getNamedArgumentsVariants(),
);

$methodThrowPoint = $this->getStaticMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope);
if ($methodThrowPoint !== null) {
$throwPoints[] = $methodThrowPoint;
}

$declaringClass = $methodReflection->getDeclaringClass();
if (
$declaringClass->getName() === 'Closure'
Expand Down Expand Up @@ -203,6 +198,13 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$scope = $argsResult->getScope();
$scopeFunction = $scope->getFunction();

if ($methodReflection !== null && $parametersAcceptor !== null) {
$methodThrowPoint = $this->getStaticMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope);
if ($methodThrowPoint !== null) {
$throwPoints[] = $methodThrowPoint;
}
}
Comment thread
staabm marked this conversation as resolved.

if (
$methodReflection !== null
&& (
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,16 @@ public function testBug9349(): void
]);
}

public function testBug14318(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-14318.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug14274(): void
{
Expand Down
59 changes: 59 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-14318.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types = 1);

namespace Bug14318;

class HelloWorld5
{
public function test5(): void
{
global $pdo;

try {
$this->maybeThrows5($sql = "SELECT * FROM foo");
$rs = $pdo->query($sql);
if ($result = $rs->fetch(\PDO::FETCH_ASSOC)) {
// do something
}
} catch (\PDOException $e) {
var_dump($sql);
}
}

/**
* @throws \RuntimeException
*/
public function maybeThrows5(string $s): void
{
if (random_int(0, 1) === 1) {
throw new \RuntimeException();
}
}
}

class HelloWorld6
{
public function test6(): void
{
global $pdo;

try {
$this->maybeThrows6(strlen($sql = "SELECT * FROM foo"));
$rs = $pdo->query($sql);
if ($result = $rs->fetch(\PDO::FETCH_ASSOC)) {
// do something
}
} catch (\PDOException $e) {
var_dump($sql);
}
}

/**
* @throws \RuntimeException
*/
public function maybeThrows6(int $s): void
{
if (random_int(0, 1) === 1) {
throw new \RuntimeException();
}
}
}
Loading