Skip to content

Commit

Permalink
Regression tests for remembering function values
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 28, 2021
1 parent bdc09a6 commit ebb5520
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -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');
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/PHPStan/Analyser/data/bug-1157.php
@@ -0,0 +1,25 @@
<?php

namespace Bug1157;

use DateTimeInterface;
use function PHPStan\Analyser\assertType;

class HelloWorld
{
private $a;

/**
* @return \DateTimeInterface|null
*/
public function getA()
{
return $this->a;
}
}

function (HelloWorld $class): void {
if ($class->getA()) {
assertType(DateTimeInterface::class, $class->getA());
}
};
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/data/bug-2288.php
@@ -0,0 +1,24 @@
<?php

namespace Bug2288;

use function PHPStan\Analyser\assertType;

class One
{
public function test() :?\DateTimeImmutable
{
return rand(0,1) === 1 ? new \DateTimeImmutable(): null;
}
}

class Two
{
public function test(): void
{
$test = new One();
if ($test->test()) {
assertType(\DateTimeImmutable::class, $test->test());
}
}
}
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/data/bug-3382.php
@@ -0,0 +1,9 @@
<?php

namespace Bug3382;

use function PHPStan\Analyser\assertType;

if (ini_get('auto_prepend_file')) {
assertType('string', ini_get('auto_prepend_file'));
}
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4091.php
@@ -0,0 +1,10 @@
<?php

namespace Bug4091;

use function PHPStan\Analyser\assertType;

if (mt_rand(0,10) > 3) {
echo 'Fizz';
assertType('int', mt_rand(0,10));
}
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4177.php
@@ -0,0 +1,32 @@
<?php

namespace Bug4177;

use function PHPStan\Analyser\assertType;

class Dto
{
private int $unixtimestamp = 12345678;

public function getPeriodFrom(): ?int
{
return rand(0,1) ? $this->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<min, -1>|int<1, max>', $request->getPeriodFrom());
}

if ($request->getPeriodTo() !== null) {
assertType('int', $request->getPeriodTo());
}
}
};
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4588.php
@@ -0,0 +1,26 @@
<?php

namespace Bug4588;

use function PHPStan\Analyser\assertType;

class c {
private $b;
public function __construct(?b $b ) {
$this->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());
}
};

0 comments on commit ebb5520

Please sign in to comment.