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
45 changes: 33 additions & 12 deletions src/Analyser/ExprHandler/NewHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,15 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$impurePoints = [];
$isAlwaysTerminating = false;
$normalizedExpr = $expr;
$classReflection = null;
$classFound = true;
$deferConstructorThrowPoints = false;
if ($expr->class instanceof Name) {
$className = $scope->resolveName($expr->class);

[$constructorReflection, $parametersAcceptor, $constructorThrowPoints, $constructorImpurePoints] = $this->processConstructorReflection($className, $expr, $scope);
$throwPoints = array_merge($throwPoints, $constructorThrowPoints);
[$constructorReflection, $parametersAcceptor, $classReflection, $classFound, $constructorImpurePoints] = $this->processConstructorReflection($className, $expr, $scope);
$impurePoints = array_merge($impurePoints, $constructorImpurePoints);
$deferConstructorThrowPoints = true;

if ($parametersAcceptor !== null) {
$normalizedExpr = ArgumentsNormalizer::reorderNewArguments($parametersAcceptor, $expr) ?? $expr;
Expand Down Expand Up @@ -176,9 +179,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$throwPoints = array_merge($throwPoints, $additionalThrowPoints);

if ($className !== null) {
[$constructorReflection, $parametersAcceptor, $constructorThrowPoints, $constructorImpurePoints] = $this->processConstructorReflection($className, $expr, $scope);
$throwPoints = array_merge($throwPoints, $constructorThrowPoints);
[$constructorReflection, $parametersAcceptor, $classReflection, $classFound, $constructorImpurePoints] = $this->processConstructorReflection($className, $expr, $scope);
$impurePoints = array_merge($impurePoints, $constructorImpurePoints);
$deferConstructorThrowPoints = true;
} else {
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
$impurePoints[] = new ImpurePoint(
Expand All @@ -202,6 +205,10 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$impurePoints = array_merge($impurePoints, $argsResult->getImpurePoints());
$isAlwaysTerminating = $isAlwaysTerminating || $argsResult->isAlwaysTerminating();

if ($deferConstructorThrowPoints) {
$throwPoints = array_merge($throwPoints, $this->createConstructorThrowPoints($expr, $scope, $constructorReflection, $parametersAcceptor, $classReflection, $classFound));
}

return new ExpressionResult(
$scope,
hasYield: $hasYield,
Expand All @@ -212,16 +219,16 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
}

/**
* @return array{?MethodReflection, ?ParametersAcceptor, InternalThrowPoint[], ImpurePoint[]}
* @return array{?MethodReflection, ?ParametersAcceptor, ?ClassReflection, bool, ImpurePoint[]}
*/
private function processConstructorReflection(string $className, New_ $expr, MutatingScope $scope): array
{
$constructorReflection = null;
$parametersAcceptor = null;
$throwPoints = [];
$impurePoints = [];

$classReflection = null;
$classFound = true;
if ($this->reflectionProvider->hasClass($className)) {
$classReflection = $this->reflectionProvider->getClass($className);
if ($classReflection->hasConstructor()) {
Expand All @@ -232,13 +239,9 @@ private function processConstructorReflection(string $className, New_ $expr, Mut
$constructorReflection->getVariants(),
$constructorReflection->getNamedArgumentsVariants(),
);
$constructorThrowPoint = $this->getConstructorThrowPoint($constructorReflection, $parametersAcceptor, $classReflection, $expr, new Name\FullyQualified($className), $expr->getArgs(), $scope);
if ($constructorThrowPoint !== null) {
$throwPoints[] = $constructorThrowPoint;
}
}
} else {
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
$classFound = false;
}

if ($constructorReflection !== null) {
Expand All @@ -262,7 +265,25 @@ private function processConstructorReflection(string $className, New_ $expr, Mut
);
}

return [$constructorReflection, $parametersAcceptor, $throwPoints, $impurePoints];
return [$constructorReflection, $parametersAcceptor, $classReflection, $classFound, $impurePoints];
}

/**
* @return InternalThrowPoint[]
*/
private function createConstructorThrowPoints(New_ $expr, MutatingScope $scope, ?MethodReflection $constructorReflection, ?ParametersAcceptor $parametersAcceptor, ?ClassReflection $classReflection, bool $classFound): array
{
$throwPoints = [];
if ($classReflection !== null && $constructorReflection !== null && $parametersAcceptor !== null) {
$constructorThrowPoint = $this->getConstructorThrowPoint($constructorReflection, $parametersAcceptor, $classReflection, $expr, new Name\FullyQualified($classReflection->getName()), $expr->getArgs(), $scope);
if ($constructorThrowPoint !== null) {
$throwPoints[] = $constructorThrowPoint;
}
} elseif (!$classFound) {
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
}

return $throwPoints;
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,29 @@ public function testBug14318(): void
$this->analyse([__DIR__ . '/data/bug-14318.php'], []);
}

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

$this->analyse([__DIR__ . '/data/bug-14323.php'], [
[
'Variable $command might not be defined.',
40,
],
[
'Variable $command might not be defined.',
66,
],
[
'Variable $command might not be defined.',
118,
],
]);
}

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

namespace Bug14323;

class ProcessFailedException extends \RuntimeException {}

class Process {
/** @param array<string> $a */
public function __construct(array $a) {}
}

class Process2 {
/**
* @param array<string> $a
* @throws ProcessFailedException
*/
public function __construct(array $a) {}
}

class Process3 {
/**
* @param array<string> $a
* @throws void
*/
public function __construct(array $a) {}
}

abstract class DbCommand
{
/**
* @return int
*/
public function handle()
{
try {
new Process(
array_merge([$command = $this->getCommand()])
);
} catch (ProcessFailedException $e) {
echo ("{$command} not found in path.");

return 1;
}

return 0;
}

/**
* @return string
*/
abstract public function getCommand();
}

abstract class DbCommand2
{
/**
* @return int
*/
public function handle()
{
try {
new Process(
[$command = $this->getCommand()]
);
} catch (ProcessFailedException $e) {
echo ("{$command} not found in path.");

return 1;
}

return 0;
}

/**
* @return string
*/
abstract public function getCommand();
}

abstract class DbCommand3
{
/**
* @return int
*/
public function handle()
{
try {
new Process2(
array_merge([$command = $this->getCommand()])
);
} catch (ProcessFailedException $e) {
echo ("{$command} not found in path.");

return 1;
}

return 0;
}

/**
* @return string
*/
abstract public function getCommand();
}

abstract class DbCommand4
{
/**
* @return int
*/
public function handle()
{
try {
new Process3(
array_merge([$command = $this->getCommand()])
);
} catch (ProcessFailedException $e) {
echo ("{$command} not found in path.");

return 1;
}

return 0;
}

/**
* @return string
*/
abstract public function getCommand();
}
Loading