Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 4, 2023
1 parent 5e8b69d commit 66b09e2
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-reverse.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6889.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6891.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10088.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/shuffle.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/simplexml.php');

Expand Down
69 changes: 69 additions & 0 deletions tests/PHPStan/Analyser/data/bug-10088.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Bug10088;

use PHPStan\TrinaryLogic;
use stdClass;
use function PHPStan\Testing\assertVariableCertainty;

class Foo
{

function doFoo(): void {
if (rand(0,1)) {
$shortcut_id = 1;
assertVariableCertainty(TrinaryLogic::createYes(), $shortcut_id);
}

assertVariableCertainty(TrinaryLogic::createMaybe(), $shortcut_id);

$link_mode = isset($shortcut_id) ? "remove" : "add";
if ($link_mode === "add") {
assertVariableCertainty(TrinaryLogic::createNo(), $shortcut_id);
} else {
assertVariableCertainty(TrinaryLogic::createYes(), $shortcut_id);
}

assertVariableCertainty(TrinaryLogic::createMaybe(), $shortcut_id);
}

/**
* @param mixed[] $period
*/
public function testCarbon(array $period): void
{
foreach ($period as $date) {
break;
}

assertVariableCertainty(TrinaryLogic::createMaybe(), $date);
$this->assertInstanceOfStdClass($date ?? null);
assertVariableCertainty(TrinaryLogic::createYes(), $date);
}

/**
* @param mixed $m
* @phpstan-assert stdClass $m
*/
private function assertInstanceOfStdClass($m): void
{
if (!$m instanceof stdClass) {
throw new \Exception();
}
}

/**
* @param mixed[] $period
*/
public function testCarbon2(array $period): void
{
foreach ($period as $date) {
break;
}

assertVariableCertainty(TrinaryLogic::createMaybe(), $date);
assert(($date ?? null) instanceof stdClass);
assertVariableCertainty(TrinaryLogic::createYes(), $date);
}

}
32 changes: 31 additions & 1 deletion tests/PHPStan/Analyser/data/falsey-isset-certainty.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace FalseyIssetCertainty;

use function PHPStan\dumpType;
use function PHPStan\Testing\assertType;
use function PHPStan\Testing\assertVariableCertainty;
use PHPStan\TrinaryLogic;
Expand Down Expand Up @@ -121,6 +120,37 @@ function falseyIssetVariable(): void
assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
}

function nullableVariable(): void
{
$a = 'bar';
if (rand() % 2) {
$a = null;
}

assertVariableCertainty(TrinaryLogic::createYes(), $a);
if (isset($a)) {
assertVariableCertainty(TrinaryLogic::createYes(), $a);
} else {
assertVariableCertainty(TrinaryLogic::createYes(), $a);
}

assertVariableCertainty(TrinaryLogic::createYes(), $a);
}

function nonNullableVariable(): void
{
$a = 'bar';

assertVariableCertainty(TrinaryLogic::createYes(), $a);
if (isset($a)) {
assertVariableCertainty(TrinaryLogic::createYes(), $a);
} else {
assertVariableCertainty(TrinaryLogic::createNo(), $a);
}

assertVariableCertainty(TrinaryLogic::createYes(), $a);
}

function falseyIssetNullableVariable(): void
{
if (rand() % 2) {
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/falsy-isset.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function subtractedMixedIsset(mixed $m): void
if (isset($m)) {
assertType("mixed~null", $m);
} else {
assertType("*NEVER*", $m);
assertType("*ERROR*", $m);
}
}

Expand Down

0 comments on commit 66b09e2

Please sign in to comment.