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
73 changes: 48 additions & 25 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3653,31 +3653,6 @@ static function (): void {

} else {
$classReflection = $this->reflectionProvider->getAnonymousClassReflection($expr->class, $scope); // populates $expr->class->name
$constructorResult = null;
$this->processStmtNode($expr->class, $scope, static function (Node $node, Scope $scope) use ($nodeCallback, $classReflection, &$constructorResult): void {
$nodeCallback($node, $scope);
if (!$node instanceof MethodReturnStatementsNode) {
return;
}
if ($constructorResult !== null) {
return;
}
$currentClassReflection = $node->getClassReflection();
if ($currentClassReflection->getName() !== $classReflection->getName()) {
return;
}
if (!$currentClassReflection->hasConstructor()) {
return;
}
if ($currentClassReflection->getConstructor()->getName() !== $node->getMethodReflection()->getName()) {
return;
}
$constructorResult = $node;
}, StatementContext::createTopLevel());
if ($constructorResult !== null) {
$throwPoints = array_merge($throwPoints, $constructorResult->getStatementResult()->getThrowPoints());
$impurePoints = array_merge($impurePoints, $constructorResult->getImpurePoints());
}
if ($classReflection->hasConstructor()) {
$constructorReflection = $classReflection->getConstructor();
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
Expand All @@ -3686,6 +3661,54 @@ static function (): void {
$constructorReflection->getVariants(),
$constructorReflection->getNamedArgumentsVariants(),
);

if ($constructorReflection->getDeclaringClass()->getName() === $classReflection->getName()) {
$constructorResult = null;
$this->processStmtNode($expr->class, $scope, static function (Node $node, Scope $scope) use ($nodeCallback, $classReflection, &$constructorResult): void {
$nodeCallback($node, $scope);
if (!$node instanceof MethodReturnStatementsNode) {
return;
}
if ($constructorResult !== null) {
return;
}
$currentClassReflection = $node->getClassReflection();
if ($currentClassReflection->getName() !== $classReflection->getName()) {
return;
}
if (!$currentClassReflection->hasConstructor()) {
return;
}
if ($currentClassReflection->getConstructor()->getName() !== $node->getMethodReflection()->getName()) {
return;
}
$constructorResult = $node;
}, StatementContext::createTopLevel());
if ($constructorResult !== null) {
$throwPoints = $constructorResult->getStatementResult()->getThrowPoints();
$impurePoints = $constructorResult->getImpurePoints();
}
} else {
$this->processStmtNode($expr->class, $scope, $nodeCallback, StatementContext::createTopLevel());
$declaringClass = $constructorReflection->getDeclaringClass();
$constructorThrowPoint = $this->getConstructorThrowPoint($constructorReflection, $parametersAcceptor, $classReflection, $expr, new Name\FullyQualified($declaringClass->getName()), $expr->getArgs(), $scope);
if ($constructorThrowPoint !== null) {
$throwPoints[] = $constructorThrowPoint;
}

if (!$constructorReflection->hasSideEffects()->no()) {
$certain = $constructorReflection->isPure()->no();
$impurePoints[] = new ImpurePoint(
$scope,
$expr,
'new',
sprintf('instantiation of class %s', $declaringClass->getDisplayName()),
$certain,
);
}
}
} else {
$this->processStmtNode($expr->class, $scope, $nodeCallback, StatementContext::createTopLevel());
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/DeadCode/NoopRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,18 @@ public function testBug13067(): void
$this->analyse([__DIR__ . '/data/bug-13067.php'], []);
}

public function testBug13698(): void
{
$this->analyse([__DIR__ . '/data/bug-13698.php'], [
[
'Expression "new class extends \Bug13698\NoConstructorClass…" on a separate line does not do anything.',
47,
],
[
'Expression "new class…" on a separate line does not do anything.',
50,
],
]);
}

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

namespace Bug13698;

class ImpureConstructorClass
{
public function __construct()
{
echo 'foo';
}
}

class ThrowingConstructorClass
{
public function __construct()
{
if (rand(0, 1) === 0) {
throw new \RuntimeException('Error in constructor');
}
}
}

class NoConstructorClass {}

function test(): void
{
new class() extends ImpureConstructorClass {
};

new class() extends ImpureConstructorClass {
public function __construct()
{
parent::__construct();
}
};

new class() extends ThrowingConstructorClass {
};

new class() extends ThrowingConstructorClass {
public function __construct()
{
parent::__construct();
}
};

new class() extends NoConstructorClass {
};

new class() {
};
}
Loading