Skip to content

Commit

Permalink
Regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 2, 2022
1 parent 4321374 commit f3c13dd
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -759,6 +759,8 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6682.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/preg_filter.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5759.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5668.php');
}

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

namespace Bug5668;

use function PHPStan\Testing\assertType;

class Foo
{


/**
* @param array<int, 'test'|'bar'> $in
*/
function has(array $in): void
{
assertType('bool', in_array('test', $in, true));
}

/**
* @param array<int, 'test'> $in
*/
function has2(array $in): void
{
assertType('bool', in_array('test', $in, true));
}

/**
* @param non-empty-array<int, 'test'|'bar'> $in
*/
function has3(array $in): void
{
assertType('bool', in_array('test', $in, true));
}


/**
* @param non-empty-array<int, 'test'> $in
*/
function has4(array $in): void
{
assertType('true', in_array('test', $in, true));
}

}
39 changes: 39 additions & 0 deletions tests/PHPStan/Analyser/data/bug-5759.php
@@ -0,0 +1,39 @@
<?php

namespace Bug5759;

use function PHPStan\Testing\assertType;

interface ITF
{
const FIELD_A = 1;
const FIELD_B = 2;
const FIELD_C = 3;
}

class Foo
{

/** @param array<ITF::FIELD_*> $fields */
function strict(array $fields): void
{
assertType('bool', in_array(ITF::FIELD_A, $fields, true));
}


/** @param array<ITF::FIELD_*> $fields */
function loose(array $fields): void
{
assertType('bool', in_array(ITF::FIELD_A, $fields, false));
}

function another(): void
{
/** @var array<'source'|'dist'> $arr */
$arr = ['source'];

assertType('bool', in_array('dist', $arr, true));
assertType('bool', in_array('dist', $arr));
}

}
Expand Up @@ -212,4 +212,17 @@ public function testBug1746(): void
]);
}

public function testBug4666(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-4666.php'], []);
}

public function testBug2870(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-2870.php'], []);
}

}
Expand Up @@ -482,4 +482,39 @@ public function testBugInArrayDateFormat(): void
]);
}

public function testBug5496(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-5496.php'], []);
}

public function testBug3892(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-3892.php'], []);
}

public function testBug3314(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-3314.php'], []);
}

public function testBug2870(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-2870.php'], []);
}

public function testBug5354(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-5354.php'], []);
}

}
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-2870.php
@@ -0,0 +1,9 @@
<?php declare(strict_types = 1);

$array = [];

for ($i = 0; $i < 100; $i++) {
if (!in_array('test', $array, true) && (bool)rand()) {
$array[] = 'test';
}
}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-3314.php
@@ -0,0 +1,17 @@
<?php

namespace Bug3314;

class Foo
{

public function doFoo()
{
$values = [];
while (0 == 1) {
$values[] = '1';
}
$result = in_array('1', $values, true);
}

}
72 changes: 72 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-3892.php
@@ -0,0 +1,72 @@
<?php

namespace Bug3892;

class OrderEntity
{
public function isLoaded(): bool
{
return rand(0,1) === 0;
}
}

abstract class OrderSaved
{
public const TYP = [PickingOrder::class => '200', ReceiptOrder::class => '300'];

/** @var Order[] */
private array $dtos;

public function __construct(OrderEntity $order)
{
$this->dtos = \array_filter([ReceiptOrder::fromOrder($order), PickingOrder::fromOrder($order)]);
}


/**
* @return Order[]
*/
public function getDTOs(): array
{
return $this->dtos;
}
}

abstract class Order
{
public const TYP = [PickingOrder::class => '200', ReceiptOrder::class => '300'];
}

class PickingOrder extends Order
{
public static function fromOrder(OrderEntity $order): ?self
{
return $order->isLoaded() ? new self() : null;
}
}

class ReceiptOrder extends Order
{
public static function fromOrder(OrderEntity $order): ?self
{
return $order->isLoaded() ? new self() : null;
}
}

class Foo
{

public function doFoo(OrderSaved $event)
{
$DTOs = $event->getDTOs();

$DTOClasses = \array_map('\get_class', $DTOs);
$missingClasses = \array_diff(\array_keys(Order::TYP), $DTOClasses);

if (\in_array(ReceiptOrder::class, $missingClasses, true)) {

}

}

}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-4666.php
@@ -0,0 +1,29 @@
<?php

namespace Bug4666;

class HelloWorld
{
private const CONST_1 = 'xxx';
private const CONST_2 = 'yyy';

/** @param array<MyObject> $objects */
public function test(array $objects): void
{
$types = [];
foreach ($objects as $object) {
if (self::CONST_1 === $object->getType() && !in_array(self::CONST_2, $types, true)) {
$types[] = self::CONST_2;
}
}
}
}

class MyObject
{
/** @var string */
private $type;
public function getType(): string{
return $this->type;
}
}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-5354.php
@@ -0,0 +1,21 @@
<?php

namespace Bug5354;

class HelloWorld
{
/**
* @param mixed[] $foo
*/
public function sayHello(array $foo): void
{
$a = [];
foreach ($foo as $e) {
$a[] = rand(5, 15) > 10 ? 0 : 1;
}

if (\in_array(0, $a, true)) {
return;
}
}
}
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-5496.php
@@ -0,0 +1,32 @@
<?php

namespace Bug5496;

class ConstParamTypes
{
/**
* @param 'none'|array<string, 'auto'|'copy'> $propagation
*/
public function propagate($propagation): void
{
}
}

class Foo
{

public function doFoo()
{
$type = new ConstParamTypes();

/** @var array<string, 'auto'|'copy'> $propagation */
$propagation = [];

if (\in_array('auto', $propagation, true)) {
$type->propagate($propagation);
}

$type->propagate(['yakdam' => 'copy']);
}

}

0 comments on commit f3c13dd

Please sign in to comment.