Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes phpstan/phpstan#5759 Closes phpstan/phpstan#5668 Closes phpstan/phpstan#5496 Closes phpstan/phpstan#4666 Closes phpstan/phpstan#3892 Closes phpstan/phpstan#3314 Closes phpstan/phpstan#2870 Closes phpstan/phpstan#5354
- Loading branch information
1 parent
4321374
commit f3c13dd
Showing
11 changed files
with
313 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} |
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,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)); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
namespace Bug3314; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo() | ||
{ | ||
$values = []; | ||
while (0 == 1) { | ||
$values[] = '1'; | ||
} | ||
$result = in_array('1', $values, true); | ||
} | ||
|
||
} |
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,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)) { | ||
|
||
} | ||
|
||
} | ||
|
||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} | ||
} |
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,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']); | ||
} | ||
|
||
} |