Skip to content

Handle side effects of constructor calls #1205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2022
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
15 changes: 12 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,7 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
$scope = $result->getScope();
if ($methodReflection !== null) {
$hasSideEffects = $methodReflection->hasSideEffects();
if ($hasSideEffects->yes()) {
if ($hasSideEffects->yes() || $methodReflection->getName() === '__construct') {
$scope = $scope->invalidateExpression($expr->var, true);
foreach ($expr->getArgs() as $arg) {
$scope = $scope->invalidateExpression($arg->value, true);
Expand Down Expand Up @@ -2118,7 +2118,10 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
if (
$methodReflection !== null
&& !$methodReflection->isStatic()
&& $methodReflection->hasSideEffects()->yes()
&& (
$methodReflection->hasSideEffects()->yes()
|| $methodReflection->getName() === '__construct'
)
&& $scopeFunction instanceof MethodReflection
&& !$scopeFunction->isStatic()
&& $scope->isInClass()
Expand All @@ -2131,7 +2134,7 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
}

if ($methodReflection !== null) {
if ($methodReflection->hasSideEffects()->yes()) {
if ($methodReflection->hasSideEffects()->yes() || $methodReflection->getName() === '__construct') {
foreach ($expr->getArgs() as $arg) {
$scope = $scope->invalidateExpression($arg->value, true);
}
Expand Down Expand Up @@ -2444,6 +2447,12 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
$expr->getArgs(),
$constructorReflection->getVariants(),
);
$hasSideEffects = $constructorReflection->hasSideEffects();
if ($hasSideEffects->yes()) {
foreach ($expr->getArgs() as $arg) {
$scope = $scope->invalidateExpression($arg->value, true);
}
}
$constructorThrowPoint = $this->getConstructorThrowPoint($constructorReflection, $classReflection, $expr, $expr->class, $expr->getArgs(), $scope);
if ($constructorThrowPoint !== null) {
$throwPoints[] = $constructorThrowPoint;
Expand Down
4 changes: 0 additions & 4 deletions src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,6 @@ public function hasSideEffects(): TrinaryLogic
return TrinaryLogic::createFromBoolean(!$this->isPure);
}

if ($isVoid) {
return TrinaryLogic::createYes();
}

return TrinaryLogic::createMaybe();
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ public function dataFileAsserts(): iterable

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4343.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/impure-method.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/impure-constructor.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4351.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/var-above-use.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/var-above-declare.php');
Expand Down
130 changes: 130 additions & 0 deletions tests/PHPStan/Analyser/data/impure-constructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace ImpureConstuctor;

use function PHPStan\Testing\assertType;

class Foo
{

/** @var bool */
private $active;

public function __construct()
{
$this->active = false;
}

public function getActive(): bool
{
return $this->active;
}

public function activate(): void
{
$this->active = true;
}

}


class ClassWithImpureConstructorNotMarked
{

public function __construct(Foo $foo)
{
$foo->activate();
}

}

class ClassWithImpureConstructorMarked
{

/**
* @phpstan-impure
*/
public function __construct(Foo $foo)
{
$foo->activate();
}

}

class ClassWithPureConstructorMarked
{

/**
* @phpstan-pure
*/
public function __construct(Foo $foo)
{
}

}

class ClassWithImpureConstructorNotMarkedWithoutParameters
{

/** @var string */
private $lorem;

public function __construct()
{
$this->lorem = 'lorem';
}

}

class Test
{

public function testClassWithImpureConstructorNotMarked()
{
$foo = new Foo();
assertType('bool', $foo->getActive());

assert(!$foo->getActive());
assertType('false', $foo->getActive());

new ClassWithImpureConstructorNotMarked($foo);
assertType('false', $foo->getActive());
}

public function testClassWithImpureConstructorMarked()
{
$foo = new Foo();
assertType('bool', $foo->getActive());

assert(!$foo->getActive());
assertType('false', $foo->getActive());

new ClassWithImpureConstructorMarked($foo);
assertType('bool', $foo->getActive());
}

public function testClassWithPureConstructorMarked()
{
$foo = new Foo();
assertType('bool', $foo->getActive());

assert(!$foo->getActive());
assertType('false', $foo->getActive());

new ClassWithPureConstructorMarked($foo);
assertType('false', $foo->getActive());
}

public function testClassWithImpureConstructorNotMarkedWithoutParameters()
{
$foo = new Foo();
assertType('bool', $foo->getActive());

assert(!$foo->getActive());
assertType('false', $foo->getActive());

new ClassWithImpureConstructorNotMarkedWithoutParameters();
assertType('false', $foo->getActive());
}

}