-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setting for rememberPossiblyImpureFunctionValues (default true)
- Loading branch information
1 parent
79e18f3
commit 50ed38f
Showing
8 changed files
with
280 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
tests/PHPStan/Analyser/DoNotRememberPossiblyImpureFunctionValuesTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Analyser; | ||
|
||
use PHPStan\Testing\TypeInferenceTestCase; | ||
|
||
class DoNotRememberPossiblyImpureFunctionValuesTest extends TypeInferenceTestCase | ||
{ | ||
|
||
public function dataAsserts(): iterable | ||
{ | ||
yield from $this->gatherAssertTypes(__DIR__ . '/data/do-not-remember-possibly-impure-function-values.php'); | ||
} | ||
|
||
/** | ||
* @dataProvider dataAsserts | ||
* @param mixed ...$args | ||
*/ | ||
public function testAsserts( | ||
string $assertType, | ||
string $file, | ||
...$args, | ||
): void | ||
{ | ||
$this->assertFileAsserts($assertType, $file, ...$args); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/do-not-remember-possibly-impure-function-values.neon', | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
tests/PHPStan/Analyser/data/do-not-remember-possibly-impure-function-values.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace DoNotRememberPossiblyImpureFunctionValues; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
|
||
/** @phpstan-pure */ | ||
public function pure(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
public function maybePure(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
/** @phpstan-impure */ | ||
public function impure(): int | ||
{ | ||
return rand(0, 1); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
if ($this->pure() === 1) { | ||
assertType('1', $this->pure()); | ||
} | ||
|
||
if ($this->maybePure() === 1) { | ||
assertType('int', $this->maybePure()); | ||
} | ||
|
||
if ($this->impure() === 1) { | ||
assertType('int', $this->impure()); | ||
} | ||
} | ||
|
||
} | ||
|
||
class FooStatic | ||
{ | ||
|
||
/** @phpstan-pure */ | ||
public static function pure(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
public static function maybePure(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
/** @phpstan-impure */ | ||
public static function impure(): int | ||
{ | ||
return rand(0, 1); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
if (self::pure() === 1) { | ||
assertType('1', self::pure()); | ||
} | ||
|
||
if (self::maybePure() === 1) { | ||
assertType('int', self::maybePure()); | ||
} | ||
|
||
if (self::impure() === 1) { | ||
assertType('int', self::impure()); | ||
} | ||
} | ||
|
||
} | ||
|
||
/** @phpstan-pure */ | ||
function pure(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
function maybePure(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
/** @phpstan-impure */ | ||
function impure(): int | ||
{ | ||
return rand(0, 1); | ||
} | ||
|
||
function test(): void | ||
{ | ||
if (pure() === 1) { | ||
assertType('1', pure()); | ||
} | ||
|
||
if (maybePure() === 1) { | ||
assertType('int', maybePure()); | ||
} | ||
|
||
if (impure() === 1) { | ||
assertType('int', impure()); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
tests/PHPStan/Analyser/data/remember-possibly-impure-function-values.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace RememberPossiblyImpureFunctionValues; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
|
||
/** @phpstan-pure */ | ||
public function pure(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
public function maybePure(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
/** @phpstan-impure */ | ||
public function impure(): int | ||
{ | ||
return rand(0, 1); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
if ($this->pure() === 1) { | ||
assertType('1', $this->pure()); | ||
} | ||
|
||
if ($this->maybePure() === 1) { | ||
assertType('1', $this->maybePure()); | ||
} | ||
|
||
if ($this->impure() === 1) { | ||
assertType('int', $this->impure()); | ||
} | ||
} | ||
|
||
} | ||
|
||
class FooStatic | ||
{ | ||
|
||
/** @phpstan-pure */ | ||
public static function pure(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
public static function maybePure(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
/** @phpstan-impure */ | ||
public static function impure(): int | ||
{ | ||
return rand(0, 1); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
if (self::pure() === 1) { | ||
assertType('1', self::pure()); | ||
} | ||
|
||
if (self::maybePure() === 1) { | ||
assertType('1', self::maybePure()); | ||
} | ||
|
||
if (self::impure() === 1) { | ||
assertType('int', self::impure()); | ||
} | ||
} | ||
|
||
} | ||
|
||
/** @phpstan-pure */ | ||
function pure(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
function maybePure(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
/** @phpstan-impure */ | ||
function impure(): int | ||
{ | ||
return rand(0, 1); | ||
} | ||
|
||
function test(): void | ||
{ | ||
if (pure() === 1) { | ||
assertType('1', pure()); | ||
} | ||
|
||
if (maybePure() === 1) { | ||
assertType('1', maybePure()); | ||
} | ||
|
||
if (impure() === 1) { | ||
assertType('int', impure()); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
tests/PHPStan/Analyser/do-not-remember-possibly-impure-function-values.neon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
parameters: | ||
rememberPossiblyImpureFunctionValues: false |