Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
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
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Variables/EmptyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ public function testBug10367(): void
$this->analyse([__DIR__ . '/data/bug-10367.php'], []);
}

public function testBug11284(): void
{
$this->treatPhpDocTypesAsCertain = true;

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

public function testBug7806(): void
{
$this->treatPhpDocTypesAsCertain = true;

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

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

namespace Bug11284;

class HelloWorld
{

/**
* @param array<string> $err
* @throws \RuntimeException
*/
public function maybeThrows(array &$err): void
{
$err[] = 'error';
if (random_int(0, 1) === 1) {
throw new \RuntimeException();
}
}

public function test(): void
{
$err = [];
try {
$this->maybeThrows($err);
} catch (\RuntimeException $e) {
if (!empty($err)) {
echo implode(', ', $err);
}
}
}

}
115 changes: 115 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-14318.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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();
}
}
}

class HelloWorld7
{
public function test7(): void
{
global $pdo;

try {
self::maybeThrows7($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 static function maybeThrows7(string $s): void
{
if (random_int(0, 1) === 1) {
throw new \RuntimeException();
}
}
}

class HelloWorld8
{
public function test8(): void
{
global $pdo;

try {
self::maybeThrows8(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 static function maybeThrows8(int $s): void
{
if (random_int(0, 1) === 1) {
throw new \RuntimeException();
}
}
}
54 changes: 54 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-7806.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types = 1);

namespace Bug7806;

class TestMethod {
/**
* @param array<string>|null $reasons
* @throws \Exception
*/
function check(array &$reasons = null): void {
$fileName = time() % 2 ? "abc":null;
if (!$fileName) {
$reasons[] = sprintf("Dependency check fail");
throw new \Exception("check failed");
}
}

function test():void {
try {
$this->check($reasons);
printf("ok\n");
} catch (\Exception $e) {
if (!empty($reasons)) {
$e = new \Exception("Dependency check failed: " . implode(', ', $reasons), 0, $e);
}
throw new \Exception("Failed", 0, $e);
}
}
}

/**
* @param array<string>|null $reasons
* @throws \Exception
*/
function check1(array &$reasons = null): void {
$fileName = time() % 2 ? "abc":null;
if (!$fileName) {
$reasons[] = sprintf("Dependency check fail");
throw new \Exception("check failed");
}
}

function test1():void {
try {
check1($reasons);
printf("ok\n");
} catch (\Exception $e) {
if (!empty($reasons)) {
$e = new \Exception("Dependency check failed: " . implode(', ', $reasons), 0, $e);
}
throw new \Exception("Failed", 0, $e);
}
}

Loading