From ebb5520bce1a1a09a22fa78237cfc44e28c43943 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sun, 28 Mar 2021 10:11:32 +0200 Subject: [PATCH] Regression tests for remembering function values Closes https://github.com/phpstan/phpstan/issues/4588 Closes https://github.com/phpstan/phpstan/issues/4091 Closes https://github.com/phpstan/phpstan/issues/3382 Closes https://github.com/phpstan/phpstan/issues/4177 Closes https://github.com/phpstan/phpstan/issues/2288 Closes https://github.com/phpstan/phpstan/issues/1157 --- .../Analyser/NodeScopeResolverTest.php | 7 ++++ tests/PHPStan/Analyser/data/bug-1157.php | 25 +++++++++++++++ tests/PHPStan/Analyser/data/bug-2288.php | 24 ++++++++++++++ tests/PHPStan/Analyser/data/bug-3382.php | 9 ++++++ tests/PHPStan/Analyser/data/bug-4091.php | 10 ++++++ tests/PHPStan/Analyser/data/bug-4177.php | 32 +++++++++++++++++++ tests/PHPStan/Analyser/data/bug-4588.php | 26 +++++++++++++++ 7 files changed, 133 insertions(+) create mode 100644 tests/PHPStan/Analyser/data/bug-1157.php create mode 100644 tests/PHPStan/Analyser/data/bug-2288.php create mode 100644 tests/PHPStan/Analyser/data/bug-3382.php create mode 100644 tests/PHPStan/Analyser/data/bug-4091.php create mode 100644 tests/PHPStan/Analyser/data/bug-4177.php create mode 100644 tests/PHPStan/Analyser/data/bug-4588.php diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index e4ad1bfe2a..d16b601d71 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -10313,6 +10313,13 @@ public function dataFileAsserts(): iterable require_once __DIR__ . '/data/invalidate-object-argument-function.php'; yield from $this->gatherAssertTypes(__DIR__ . '/data/invalidate-object-argument-function.php'); + + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4588.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4091.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3382.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4177.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2288.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-1157.php'); } /** diff --git a/tests/PHPStan/Analyser/data/bug-1157.php b/tests/PHPStan/Analyser/data/bug-1157.php new file mode 100644 index 0000000000..f3eb7b3ec9 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-1157.php @@ -0,0 +1,25 @@ +a; + } +} + +function (HelloWorld $class): void { + if ($class->getA()) { + assertType(DateTimeInterface::class, $class->getA()); + } +}; diff --git a/tests/PHPStan/Analyser/data/bug-2288.php b/tests/PHPStan/Analyser/data/bug-2288.php new file mode 100644 index 0000000000..4e693fce7f --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-2288.php @@ -0,0 +1,24 @@ +test()) { + assertType(\DateTimeImmutable::class, $test->test()); + } + } +} diff --git a/tests/PHPStan/Analyser/data/bug-3382.php b/tests/PHPStan/Analyser/data/bug-3382.php new file mode 100644 index 0000000000..c721f2ebd3 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-3382.php @@ -0,0 +1,9 @@ + 3) { + echo 'Fizz'; + assertType('int', mt_rand(0,10)); +} diff --git a/tests/PHPStan/Analyser/data/bug-4177.php b/tests/PHPStan/Analyser/data/bug-4177.php new file mode 100644 index 0000000000..7bf8b30a81 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-4177.php @@ -0,0 +1,32 @@ +unixtimestamp : null; + } + + public function getPeriodTo(): ?int + { + return rand(0,1) ? $this->unixtimestamp : null; + } +} + +function (Dto $request): void { + if ($request->getPeriodFrom() || $request->getPeriodTo()) { + if ($request->getPeriodFrom()) { + assertType('int|int<1, max>', $request->getPeriodFrom()); + } + + if ($request->getPeriodTo() !== null) { + assertType('int', $request->getPeriodTo()); + } + } +}; diff --git a/tests/PHPStan/Analyser/data/bug-4588.php b/tests/PHPStan/Analyser/data/bug-4588.php new file mode 100644 index 0000000000..c92a0089ab --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-4588.php @@ -0,0 +1,26 @@ +b = $b; + } + public function getB(): ?b { + + return $this->b; + } +} + +class b{ + public function callB():bool {return true;} +} + +function (c $c): void { + if ($c->getB()) { + assertType(b::class, $c->getB()); + } +};