Skip to content

Disable purity check for non final methods #3913

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

Open
wants to merge 3 commits into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions src/Rules/Pure/FunctionPurityCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public function check(
count($throwPoints) === 0
&& count($impurePoints) === 0
&& count($functionReflection->getAsserts()->getAll()) === 0
&& (
!$functionReflection instanceof ExtendedMethodReflection
|| $functionReflection->isFinal()->yes()
|| $functionReflection->getDeclaringClass()->isFinal()
)
) {
$errors[] = RuleErrorBuilder::message(sprintf(
'%s is marked as impure but does not have any side effects.',
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Pure/PureMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,19 @@ public function testBug12048(): void
$this->analyse([__DIR__ . '/data/bug-12048.php'], []);
}

public function testBug12382(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-12382.php'], [
[
'Method Bug12382\FinalHelloWorld1::dummy() is marked as impure but does not have any side effects.',
25,
],
[
'Method Bug12382\FinalHelloWorld2::dummy() is marked as impure but does not have any side effects.',
33,
],
]);
}

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

namespace Bug12382;

class HelloWorld
{
/** @phpstan-impure */
public function dummy() : self{
return $this;
}
}

class Child extends HelloWorld{
private int $prop = 1;

public function dummy() : HelloWorld{
$this->prop++;
return $this;
}
}

final class FinalHelloWorld1
{
/** @phpstan-impure */
public function dummy() : self{
return $this;
}
}

class FinalHelloWorld2
{
/** @phpstan-impure */
final public function dummy() : self{
return $this;
}
}
6 changes: 3 additions & 3 deletions tests/PHPStan/Rules/Pure/data/pure-constructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PureConstructor;

class Foo
final class Foo
{

private string $prop;
Expand All @@ -21,7 +21,7 @@ public function __construct(

}

class Bar
final class Bar
{

private string $prop;
Expand All @@ -37,7 +37,7 @@ public function __construct(

}

class AssignOtherThanThis
final class AssignOtherThanThis
{
private int $i = 0;

Expand Down
40 changes: 20 additions & 20 deletions tests/PHPStan/Rules/Pure/data/pure-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PureMethod;

class Foo
final class Foo
{

/**
Expand Down Expand Up @@ -92,7 +92,7 @@ public function doFoo5()

}

class PureConstructor
final class PureConstructor
{

/**
Expand All @@ -105,7 +105,7 @@ public function __construct()

}

class ImpureConstructor
final class ImpureConstructor
{

/**
Expand All @@ -118,7 +118,7 @@ public function __construct()

}

class PossiblyImpureConstructor
final class PossiblyImpureConstructor
{

public function __construct()
Expand All @@ -128,7 +128,7 @@ public function __construct()

}

class TestConstructors
final class TestConstructors
{

/**
Expand All @@ -144,7 +144,7 @@ public function doFoo(string $s)

}

class ActuallyPure
final class ActuallyPure
{

/**
Expand Down Expand Up @@ -175,7 +175,7 @@ public function impure(): int

}

class ExtendingClass extends ToBeExtended
final class ExtendingClass extends ToBeExtended
{

public function pure(): int
Expand All @@ -191,7 +191,7 @@ public function impure(): int

}

class ClassWithVoidMethods
final class ClassWithVoidMethods
{

public function voidFunctionThatThrows(): void
Expand Down Expand Up @@ -235,12 +235,12 @@ public function purePostGetAssign(array $post = [], array $get = []): int

}

class NoMagicMethods
final class NoMagicMethods
{

}

class PureMagicMethods
final class PureMagicMethods
{

/**
Expand All @@ -253,7 +253,7 @@ public function __toString(): string

}

class MaybePureMagicMethods
final class MaybePureMagicMethods
{

public function __toString(): string
Expand All @@ -263,7 +263,7 @@ public function __toString(): string

}

class ImpureMagicMethods
final class ImpureMagicMethods
{

/**
Expand All @@ -277,7 +277,7 @@ public function __toString(): string

}

class TestMagicMethods
final class TestMagicMethods
{

/**
Expand All @@ -298,12 +298,12 @@ public function doFoo(

}

class NoConstructor
final class NoConstructor
{

}

class TestNoConstructor
final class TestNoConstructor
{

/**
Expand All @@ -318,7 +318,7 @@ public function doFoo(): int

}

class MaybeCallableFromUnion
final class MaybeCallableFromUnion
{

/**
Expand All @@ -334,7 +334,7 @@ public function doFoo($p): int

}

class VoidMethods
final class VoidMethods
{

private function doFoo(): void
Expand All @@ -361,7 +361,7 @@ private function doBaz(): void

}

class AssertingImpureVoidMethod
final class AssertingImpureVoidMethod
{

/**
Expand All @@ -376,7 +376,7 @@ public function assertSth($value): void

}

class StaticMethodAccessingStaticProperty
final class StaticMethodAccessingStaticProperty
{
/** @var int */
public static $a = 0;
Expand All @@ -397,7 +397,7 @@ public static function getB(): int
}
}

class StaticMethodAssigningStaticProperty
final class StaticMethodAssigningStaticProperty
{
/** @var int */
public static $a = 0;
Expand Down
Loading