2016-06-17 22:05:28 +02:00
|
|
|
<?php
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm\Tests;
|
2016-06-17 22:05:28 +02:00
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use function is_array;
|
2019-03-23 19:27:54 +01:00
|
|
|
use Psalm\Context;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\TypeAnalyzer;
|
|
|
|
use Psalm\Internal\Clause;
|
2016-10-11 04:49:43 +02:00
|
|
|
use Psalm\Type;
|
2018-05-07 07:26:06 +02:00
|
|
|
use Psalm\Type\Algebra;
|
2017-12-29 16:55:41 +01:00
|
|
|
use Psalm\Type\Reconciler;
|
2016-06-17 22:05:28 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class TypeReconciliationTest extends TestCase
|
2016-06-17 22:05:28 +02:00
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
/** @var FileAnalyzer */
|
2018-11-11 18:01:14 +01:00
|
|
|
protected $file_analyzer;
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
/** @var StatementsAnalyzer */
|
2018-11-11 18:01:14 +01:00
|
|
|
protected $statements_analyzer;
|
2017-06-23 06:39:37 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-05-17 00:36:36 +02:00
|
|
|
public function setUp() : void
|
2016-10-11 04:49:43 +02:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
parent::setUp();
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->file_analyzer = new FileAnalyzer($this->project_analyzer, 'somefile.php', 'somefile.php');
|
|
|
|
$this->file_analyzer->context = new Context();
|
|
|
|
$this->statements_analyzer = new StatementsAnalyzer($this->file_analyzer);
|
2016-10-11 04:49:43 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @dataProvider providerTestReconcilation
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-04-25 05:45:02 +02:00
|
|
|
* @param string $expected
|
|
|
|
* @param string $type
|
|
|
|
* @param string $string
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function testReconcilation($expected, $type, $string)
|
2016-06-17 22:05:28 +02:00
|
|
|
{
|
2017-12-29 16:55:41 +01:00
|
|
|
$reconciled = Reconciler::reconcileTypes(
|
2017-04-25 05:45:02 +02:00
|
|
|
$type,
|
|
|
|
Type::parseString($string),
|
|
|
|
null,
|
2018-12-08 19:18:55 +01:00
|
|
|
$this->statements_analyzer,
|
2019-01-23 05:42:54 +01:00
|
|
|
false,
|
|
|
|
[]
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame(
|
2017-04-25 05:45:02 +02:00
|
|
|
$expected,
|
2018-12-08 19:18:55 +01:00
|
|
|
$reconciled->getId()
|
2016-06-17 22:05:28 +02:00
|
|
|
);
|
|
|
|
|
2018-02-06 19:40:28 +01:00
|
|
|
if (is_array($reconciled->getTypes())) {
|
2018-11-04 19:00:14 +01:00
|
|
|
$this->assertContainsOnlyInstancesOf('Psalm\Type\Atomic', $reconciled->getTypes());
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2016-12-24 19:23:22 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 07:12:19 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @dataProvider providerTestTypeIsContainedBy
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-04-25 05:45:02 +02:00
|
|
|
* @param string $input
|
|
|
|
* @param string $container
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-19 07:12:19 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function testTypeIsContainedBy($input, $container)
|
2017-01-19 07:12:19 +01:00
|
|
|
{
|
|
|
|
$this->assertTrue(
|
2018-11-06 03:57:36 +01:00
|
|
|
TypeAnalyzer::isContainedBy(
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer->getCodebase(),
|
2017-04-25 05:45:02 +02:00
|
|
|
Type::parseString($input),
|
2017-07-29 21:05:06 +02:00
|
|
|
Type::parseString($container)
|
2017-01-19 07:12:19 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-11 19:48:11 +01:00
|
|
|
/**
|
2019-02-23 22:22:39 +01:00
|
|
|
* @return array<string,array{string,string,string}>
|
2017-01-15 23:52:01 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerTestReconcilation()
|
2017-01-15 23:52:01 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'notNullWithObject' => ['MyObject', '!null', 'MyObject'],
|
|
|
|
'notNullWithObjectPipeNull' => ['MyObject', '!null', 'MyObject|null'],
|
2019-01-05 06:15:53 +01:00
|
|
|
'notNullWithMyObjectPipeFalse' => ['false|MyObject', '!null', 'MyObject|false'],
|
2017-04-25 05:45:02 +02:00
|
|
|
'notNullWithMixed' => ['mixed', '!null', 'mixed'],
|
2017-01-15 23:52:01 +01:00
|
|
|
|
2017-10-22 17:57:41 +02:00
|
|
|
'notEmptyWithMyObject' => ['MyObject', '!falsy', 'MyObject'],
|
|
|
|
'notEmptyWithMyObjectPipeNull' => ['MyObject', '!falsy', 'MyObject|null'],
|
|
|
|
'notEmptyWithMyObjectPipeFalse' => ['MyObject', '!falsy', 'MyObject|false'],
|
2018-12-08 19:18:55 +01:00
|
|
|
'notEmptyWithMixed' => ['non-empty-mixed', '!falsy', 'mixed'],
|
2017-04-25 05:45:02 +02:00
|
|
|
// @todo in the future this should also work
|
2017-10-22 17:57:41 +02:00
|
|
|
//'notEmptyWithMyObjectFalseTrue' => ['MyObject|true', '!falsy', 'MyObject|bool'],
|
2016-12-12 05:41:11 +01:00
|
|
|
|
2018-01-18 23:16:50 +01:00
|
|
|
'nullWithMyObjectPipeNull' => ['null', 'null', 'MyObject|null'],
|
|
|
|
'nullWithMixed' => ['null', 'null', 'mixed'],
|
2016-12-12 05:41:11 +01:00
|
|
|
|
2018-04-05 18:11:58 +02:00
|
|
|
'falsyWithMyObject' => ['mixed', 'falsy', 'MyObject'],
|
|
|
|
'falsyWithMyObjectPipeFalse' => ['false', 'falsy', 'MyObject|false'],
|
|
|
|
'falsyWithMyObjectPipeBool' => ['false', 'falsy', 'MyObject|bool'],
|
2018-12-08 19:18:55 +01:00
|
|
|
'falsyWithMixed' => ['empty-mixed', 'falsy', 'mixed'],
|
2018-04-05 18:11:58 +02:00
|
|
|
'falsyWithBool' => ['false', 'falsy', 'bool'],
|
2018-12-08 19:18:55 +01:00
|
|
|
'falsyWithStringOrNull' => ['null|string()|string(0)', 'falsy', 'string|null'],
|
|
|
|
'falsyWithScalarOrNull' => ['empty-scalar', 'falsy', 'scalar'],
|
2016-10-11 04:49:43 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
'notMyObjectWithMyObjectPipeBool' => ['bool', '!MyObject', 'MyObject|bool'],
|
|
|
|
'notMyObjectWithMyObjectPipeNull' => ['null', '!MyObject', 'MyObject|null'],
|
|
|
|
'notMyObjectWithMyObjectAPipeMyObjectB' => ['MyObjectB', '!MyObjectA', 'MyObjectA|MyObjectB'],
|
2016-10-11 04:49:43 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
'myObjectWithMyObjectPipeBool' => ['MyObject', 'MyObject', 'MyObject|bool'],
|
|
|
|
'myObjectWithMyObjectAPipeMyObjectB' => ['MyObjectA', 'MyObjectA', 'MyObjectA|MyObjectB'],
|
2016-10-11 04:49:43 +02:00
|
|
|
|
2019-01-05 06:15:53 +01:00
|
|
|
'array' => ['array<array-key, mixed>', 'array', 'array|null'],
|
2016-10-11 04:49:43 +02:00
|
|
|
|
2019-01-05 06:15:53 +01:00
|
|
|
'2dArray' => ['array<array-key, array<array-key, string>>', 'array', 'array<array<string>>|null'],
|
2016-12-27 19:58:58 +01:00
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
'numeric' => ['numeric-string', 'numeric', 'string'],
|
2018-11-02 22:38:20 +01:00
|
|
|
|
|
|
|
'nullableClassString' => ['null', 'falsy', '?class-string'],
|
2018-12-08 19:18:55 +01:00
|
|
|
'mixedOrNullNotFalsy' => ['non-empty-mixed', '!falsy', 'mixed|null'],
|
2018-12-10 17:17:35 +01:00
|
|
|
'mixedOrNullFalsy' => ['null|empty-mixed', 'falsy', 'mixed|null'],
|
2019-01-08 21:26:54 +01:00
|
|
|
'nullableClassStringFalsy' => ['null', 'falsy', 'class-string<A>|null'],
|
|
|
|
'nullableClassStringEqualsNull' => ['null', '=null', 'class-string<A>|null'],
|
|
|
|
'nullableClassStringTruthy' => ['class-string<A>', '!falsy', 'class-string<A>|null'],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-04-07 04:56:37 +02:00
|
|
|
}
|
|
|
|
|
2017-04-16 02:36:40 +02:00
|
|
|
/**
|
2019-02-23 22:22:39 +01:00
|
|
|
* @return array<string,array{string,string}>
|
2017-04-07 04:56:37 +02:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerTestTypeIsContainedBy()
|
2017-04-07 04:56:37 +02:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'arrayContainsWithArrayOfStrings' => ['array<string>', 'array'],
|
|
|
|
'arrayContainsWithArrayOfExceptions' => ['array<Exception>', 'array'],
|
2017-04-07 04:56:37 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
'unionContainsWithstring' => ['string', 'string|false'],
|
2017-05-27 02:05:57 +02:00
|
|
|
'unionContainsWithFalse' => ['false', 'string|false'],
|
2018-04-05 00:01:53 +02:00
|
|
|
'objectLikeTypeWithPossiblyUndefinedToGeneric' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{0: array{a: string}, 1: array{c: string, e: string}}',
|
2019-03-23 19:27:54 +01:00
|
|
|
'array<int, array<string, string>>',
|
2018-04-05 00:01:53 +02:00
|
|
|
],
|
|
|
|
'objectLikeTypeWithPossiblyUndefinedToEmpty' => [
|
|
|
|
'array<empty, empty>',
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a?: string, b?: string}',
|
2018-04-05 00:01:53 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-04-07 03:53:29 +02:00
|
|
|
}
|
2017-04-16 02:36:40 +02:00
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'intIsMixed' => [
|
|
|
|
'<?php
|
2017-09-02 17:18:56 +02:00
|
|
|
/** @param mixed $a */
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo($a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = 5;
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($b === $a) { }
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'typeResolutionFromDocblock' => [
|
|
|
|
'<?php
|
|
|
|
class A { }
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @param A $a
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function fooFoo($a) {
|
|
|
|
if ($a instanceof A) {
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2018-02-07 00:44:53 +01:00
|
|
|
'assertions' => [],
|
2018-02-07 21:20:47 +01:00
|
|
|
'error_levels' => ['RedundantConditionGivenDocblockType'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayTypeResolutionFromDocblock' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string[] $strs
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function foo(array $strs) {
|
|
|
|
foreach ($strs as $str) {
|
2018-02-07 00:44:53 +01:00
|
|
|
if (is_string($str)) {}
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2018-02-07 00:44:53 +01:00
|
|
|
'assertions' => [],
|
2018-02-07 21:20:47 +01:00
|
|
|
'error_levels' => ['RedundantConditionGivenDocblockType'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'typeResolutionFromDocblockInside' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param int $length
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function foo($length) {
|
|
|
|
if (!is_int($length)) {
|
|
|
|
if (is_numeric($length)) {
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2018-02-07 00:44:53 +01:00
|
|
|
'assertions' => [],
|
2019-02-27 20:02:02 +01:00
|
|
|
'error_levels' => ['DocblockTypeContradiction'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'notInstanceof' => [
|
|
|
|
'<?php
|
|
|
|
class A { }
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A { }
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
$a = new A();
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$out = null;
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a instanceof B) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$out = $a;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$out' => 'null|A',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'notInstanceOfProperty' => [
|
|
|
|
'<?php
|
|
|
|
class B { }
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class C extends B { }
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @var B */
|
|
|
|
public $foo;
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct() {
|
|
|
|
$this->foo = new B();
|
|
|
|
}
|
|
|
|
}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = new A();
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$out = null;
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a->foo instanceof C) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$out = $a->foo;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$out' => 'null|B',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'error_levels' => [],
|
|
|
|
],
|
|
|
|
'notInstanceOfPropertyElseif' => [
|
|
|
|
'<?php
|
|
|
|
class B { }
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class C extends B { }
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @var string|B */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
$a = new A();
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$out = null;
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (is_string($a->foo)) {
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
elseif ($a->foo instanceof C) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$out = $a->foo;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$out' => 'null|B',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'error_levels' => [],
|
|
|
|
],
|
2019-01-05 06:15:53 +01:00
|
|
|
'typeRefinementWithIsNumericOnIntOrFalse' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
|
|
|
/** @return void */
|
|
|
|
function fooFoo(string $a) {
|
|
|
|
if (is_numeric($a)) { }
|
2018-11-16 19:31:53 +01:00
|
|
|
|
|
|
|
if (is_numeric($a) && $a === "1") { }
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = rand(0, 1) ? 5 : false;
|
2017-05-27 02:05:57 +02:00
|
|
|
if (is_numeric($b)) { }',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'typeRefinementWithIsNumericAndIsString' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param mixed $a
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function foo ($a) {
|
|
|
|
if (is_numeric($a)) {
|
|
|
|
if (is_string($a)) {
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-11-22 02:58:08 +01:00
|
|
|
'typeRefinementWithIsNumericOnIntOrString' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 5) > 4 ? "hello" : 5;
|
|
|
|
|
|
|
|
if (is_numeric($a)) {
|
|
|
|
exit;
|
|
|
|
}',
|
2017-12-09 22:05:31 +01:00
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'string',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'typeRefinementWithStringOrTrue' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 5) > 4 ? "hello" : true;
|
|
|
|
|
|
|
|
if (is_bool($a)) {
|
|
|
|
exit;
|
|
|
|
}',
|
2017-11-22 02:58:08 +01:00
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'updateMultipleIssetVars' => [
|
|
|
|
'<?php
|
|
|
|
/** @return void **/
|
|
|
|
function foo(string $s) {}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = rand(0, 1) ? ["hello"] : null;
|
|
|
|
if (isset($a[0])) {
|
|
|
|
foo($a[0]);
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'updateMultipleIssetVarsWithVariableOffset' => [
|
|
|
|
'<?php
|
|
|
|
/** @return void **/
|
|
|
|
function foo(string $s) {}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = rand(0, 1) ? ["hello"] : null;
|
|
|
|
$b = 0;
|
|
|
|
if (isset($a[$b])) {
|
|
|
|
foo($a[$b]);
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'instanceOfSubtypes' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {}
|
|
|
|
class B extends A {}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
abstract class C {}
|
|
|
|
class D extends C {}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function makeA(): A {
|
|
|
|
return new B();
|
|
|
|
}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function makeC(): C {
|
|
|
|
return new D();
|
|
|
|
}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
$a = rand(0, 1) ? makeA(): makeC();
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
if ($a instanceof B || $a instanceof D) { }',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'typeReconciliationAfterIfAndReturn' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string|int $a
|
|
|
|
* @return string|int
|
|
|
|
*/
|
|
|
|
function foo($a) {
|
|
|
|
if (is_string($a)) {
|
|
|
|
return $a;
|
|
|
|
} elseif (is_int($a)) {
|
|
|
|
return $a;
|
|
|
|
}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
throw new \LogicException("Runtime error");
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2018-02-07 21:20:47 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['RedundantConditionGivenDocblockType'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'ignoreNullCheckAndMaintainNullValue' => [
|
|
|
|
'<?php
|
|
|
|
$a = null;
|
|
|
|
if ($a !== null) { }
|
|
|
|
$b = $a;',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$b' => 'null',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-04-10 07:27:26 +02:00
|
|
|
'error_levels' => ['TypeDoesNotContainType', 'RedundantCondition'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'ignoreNullCheckAndMaintainNullableValue' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? 5 : null;
|
|
|
|
if ($a !== null) { }
|
|
|
|
$b = $a;',
|
|
|
|
'assertions' => [
|
2018-05-31 00:56:44 +02:00
|
|
|
'$b' => 'int|null',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'ternaryByRefVar' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = null;
|
2018-01-11 21:50:45 +01:00
|
|
|
$c = rand(0, 1) ? bar($b): null;
|
2017-04-25 05:45:02 +02:00
|
|
|
if (is_int($b)) { }
|
|
|
|
}
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar(?int &$a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = 5;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'ternaryByRefVarInConditional' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = null;
|
|
|
|
if (rand(0, 1) || bar($b)) {
|
|
|
|
if (is_int($b)) { }
|
|
|
|
}
|
|
|
|
}
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar(?int &$a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = 5;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'possibleInstanceof' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {}
|
|
|
|
interface I2 {}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A
|
|
|
|
{
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($this instanceof I1 || $this instanceof I2) {}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'intersection' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function bat(): void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesI(I $i): void {}
|
|
|
|
function takesA(A $a): void {}
|
2018-03-20 14:30:37 +01:00
|
|
|
/** @param A&I $a */
|
|
|
|
function takesAandI($a): void {}
|
|
|
|
/** @param I&A $a */
|
|
|
|
function takesIandA($a): void {}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
2018-03-22 22:55:36 +01:00
|
|
|
/**
|
|
|
|
* @return A&I|null
|
|
|
|
*/
|
|
|
|
public function foo() {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($this instanceof I) {
|
|
|
|
$this->bar();
|
|
|
|
$this->bat();
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
takesA($this);
|
|
|
|
takesI($this);
|
2018-03-20 14:30:37 +01:00
|
|
|
takesAandI($this);
|
|
|
|
takesIandA($this);
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
protected function bar(): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A implements I {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function bat(): void {}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
2018-06-29 00:13:06 +02:00
|
|
|
'createIntersectionOfInterfaceAndClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function bat() : void {}
|
|
|
|
}
|
|
|
|
interface I {
|
|
|
|
public function baz();
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(I $i) : void {
|
|
|
|
if ($i instanceof A) {
|
|
|
|
$i->bat();
|
|
|
|
$i->baz();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(A $a) : void {
|
|
|
|
if ($a instanceof I) {
|
|
|
|
$a->bat();
|
|
|
|
$a->baz();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A implements I {
|
|
|
|
public function baz() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(new B);
|
|
|
|
bar(new B);',
|
|
|
|
],
|
2018-06-29 21:39:17 +02:00
|
|
|
'unionOfArrayOrTraversable' => [
|
|
|
|
'<?php
|
|
|
|
function foo(iterable $iterable) : void {
|
|
|
|
if (\is_array($iterable) || $iterable instanceof \Traversable) {}
|
|
|
|
}',
|
|
|
|
],
|
2017-10-22 18:09:22 +02:00
|
|
|
'isTruthy' => [
|
|
|
|
'<?php
|
|
|
|
function f(string $s = null): string {
|
|
|
|
if ($s == true) {
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "backup";
|
|
|
|
}',
|
|
|
|
],
|
2017-10-23 13:35:28 +02:00
|
|
|
'stringOrCallableArg' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string|callable $param
|
|
|
|
*/
|
|
|
|
function f($param): void {}
|
|
|
|
f("is_array");',
|
|
|
|
],
|
2017-10-23 14:00:28 +02:00
|
|
|
'stringOrCallableOrObjectArg' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string|callable|object $param
|
|
|
|
*/
|
|
|
|
function f($param): void {}
|
|
|
|
f("is_array");',
|
|
|
|
],
|
2017-10-23 13:35:28 +02:00
|
|
|
'intOrFloatArg' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param int|float $param
|
|
|
|
*/
|
|
|
|
function f($param): void {}
|
2017-10-23 14:00:28 +02:00
|
|
|
f(5.0);
|
|
|
|
f(5);',
|
2017-10-23 13:35:28 +02:00
|
|
|
],
|
2017-11-21 02:06:00 +01:00
|
|
|
'nullReplacement' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string|null|false $a
|
|
|
|
* @return string|false $a
|
|
|
|
*/
|
|
|
|
function foo($a) {
|
|
|
|
if ($a === null) {
|
|
|
|
if (rand(0, 4) > 2) {
|
|
|
|
$a = "hello";
|
|
|
|
} else {
|
|
|
|
$a = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $a;
|
|
|
|
}',
|
|
|
|
],
|
2017-12-03 22:25:52 +01:00
|
|
|
'nullableIntReplacement' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? 5 : null;
|
|
|
|
|
|
|
|
$b = (bool)rand(0, 1);
|
|
|
|
|
|
|
|
if ($b || $a !== null) {
|
|
|
|
$a = 3;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int|null',
|
|
|
|
],
|
|
|
|
],
|
2017-12-14 02:48:01 +01:00
|
|
|
'eraseNullAfterInequalityCheck' => [
|
2017-12-14 02:06:19 +01:00
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
$a = mt_rand(0, 1) ? mt_rand(-10, 10): null;
|
2017-12-14 02:48:01 +01:00
|
|
|
|
2017-12-14 02:06:19 +01:00
|
|
|
if ($a > 0) {
|
|
|
|
echo $a + 3;
|
2017-12-14 02:48:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (0 < $a) {
|
|
|
|
echo $a + 3;
|
2017-12-14 02:06:19 +01:00
|
|
|
}',
|
|
|
|
],
|
2017-12-19 06:06:05 +01:00
|
|
|
'twoWrongsDontMakeARight' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = false;
|
|
|
|
} else {
|
|
|
|
$a = false;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'false',
|
|
|
|
],
|
|
|
|
],
|
2018-01-23 20:46:46 +01:00
|
|
|
'instanceofStatic' => [
|
|
|
|
'<?php
|
|
|
|
abstract class Foo {
|
|
|
|
/**
|
|
|
|
* @return static[]
|
|
|
|
*/
|
|
|
|
abstract public static function getArr() : array;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return static|null
|
|
|
|
*/
|
|
|
|
public static function getOne() {
|
|
|
|
$one = current(static::getArr());
|
|
|
|
return $one instanceof static ? $one : null;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-01-23 21:46:14 +01:00
|
|
|
'isaStaticClass' => [
|
|
|
|
'<?php
|
|
|
|
abstract class Foo {
|
|
|
|
/**
|
|
|
|
* @return static[]
|
|
|
|
*/
|
|
|
|
abstract public static function getArr() : array;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return static|null
|
|
|
|
*/
|
|
|
|
public static function getOne() {
|
|
|
|
$one = current(static::getArr());
|
|
|
|
return is_a($one, static::class, false) ? $one : null;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-01-30 22:45:29 +01:00
|
|
|
'isAClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
$a_class = rand(0, 1) ? A::class : "blargle";
|
|
|
|
if (is_a($a_class, A::class, true)) {
|
|
|
|
echo "cool";
|
|
|
|
}',
|
|
|
|
],
|
2018-02-11 05:30:40 +01:00
|
|
|
'specificArrayFields' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{field:string} $array
|
|
|
|
*/
|
|
|
|
function print_field($array) : void {
|
|
|
|
echo $array["field"];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array{field:string,otherField:string} $array
|
|
|
|
*/
|
|
|
|
function has_mix_of_fields($array) : void {
|
|
|
|
print_field($array);
|
|
|
|
}',
|
|
|
|
],
|
2018-03-23 02:48:26 +01:00
|
|
|
'numericOrStringPropertySet' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string|null $b
|
|
|
|
* @psalm-suppress DocblockTypeContradiction
|
|
|
|
*/
|
|
|
|
function foo($b = null) : void {
|
|
|
|
if (is_numeric($b) || is_string($b)) {
|
|
|
|
takesNullableString($b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesNullableString(?string $s) : void {}',
|
|
|
|
],
|
2018-04-05 18:11:58 +02:00
|
|
|
'falsyScalar' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param scalar|null $value
|
|
|
|
*/
|
|
|
|
function Foo($value = null) : bool {
|
|
|
|
if (!$value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}',
|
|
|
|
],
|
2018-05-13 01:56:17 +02:00
|
|
|
'numericStringAssertion' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param mixed $a
|
|
|
|
*/
|
|
|
|
function foo($a, string $b) : void {
|
|
|
|
if (is_numeric($b) && $a === $b) {
|
|
|
|
echo $a;
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-05-13 01:56:17 +02:00
|
|
|
],
|
2018-05-13 06:54:12 +02:00
|
|
|
'reconcileNullableStringWithWeakEquality' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $s) : void {
|
|
|
|
if ($s == "hello" || $s == "goodbye") {
|
|
|
|
if ($s == "hello") {
|
|
|
|
echo "cool";
|
|
|
|
}
|
|
|
|
echo "cooler";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-05-13 07:27:45 +02:00
|
|
|
'reconcileNullableStringWithStrictEqualityStrings' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $s, string $a, string $b) : void {
|
|
|
|
if ($s === $a || $s === $b) {
|
|
|
|
if ($s === $a) {
|
|
|
|
echo "cool";
|
|
|
|
}
|
|
|
|
echo "cooler";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'reconcileNullableStringWithWeakEqualityStrings' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $s, string $a, string $b) : void {
|
|
|
|
if ($s == $a || $s == $b) {
|
|
|
|
if ($s == $a) {
|
|
|
|
echo "cool";
|
|
|
|
}
|
|
|
|
echo "cooler";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-05-14 16:36:41 +02:00
|
|
|
'allowWeakEqualityScalarType' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $i) : void {
|
|
|
|
if ($i == "5") {}
|
2018-11-16 00:50:08 +01:00
|
|
|
if ("5" == $i) {}
|
2018-11-16 18:26:55 +01:00
|
|
|
if ($i == 5.0) {}
|
|
|
|
if (5.0 == $i) {}
|
|
|
|
if ($i == 0) {}
|
|
|
|
if (0 == $i) {}
|
|
|
|
if ($i == 0.0) {}
|
|
|
|
if (0.0 == $i) {}
|
|
|
|
}
|
2019-01-06 22:40:44 +01:00
|
|
|
function bar(float $i) : void {
|
2018-11-16 18:26:55 +01:00
|
|
|
$i = $i / 100.0;
|
|
|
|
if ($i == "5") {}
|
|
|
|
if ("5" == $i) {}
|
|
|
|
if ($i == 5) {}
|
|
|
|
if (5 == $i) {}
|
|
|
|
if ($i == "0") {}
|
|
|
|
if ("0" == $i) {}
|
|
|
|
if ($i == 0) {}
|
|
|
|
if (0 == $i) {}
|
|
|
|
}
|
2019-01-06 22:40:44 +01:00
|
|
|
function bat(string $i) : void {
|
2018-11-16 18:26:55 +01:00
|
|
|
if ($i == 5) {}
|
|
|
|
if (5 == $i) {}
|
|
|
|
if ($i == 5.0) {}
|
|
|
|
if (5.0 == $i) {}
|
|
|
|
if ($i == 0) {}
|
|
|
|
if (0 == $i) {}
|
|
|
|
if ($i == 0.0) {}
|
|
|
|
if (0.0 == $i) {}
|
2018-05-14 16:36:41 +02:00
|
|
|
}',
|
|
|
|
],
|
2018-05-16 17:47:39 +02:00
|
|
|
'filterSubclassBasedOnParentInstanceof' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {}
|
|
|
|
class D extends C {}
|
|
|
|
|
|
|
|
$b_or_d = rand(0, 1) ? new B : new D;
|
|
|
|
|
|
|
|
if ($b_or_d instanceof A) {
|
|
|
|
$b_or_d->foo();
|
|
|
|
}',
|
|
|
|
],
|
2018-05-18 17:02:50 +02:00
|
|
|
'SKIPPED-isArrayOnArrayKeyOffset' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array{s:array<mixed, array<int, string>|string>} */
|
|
|
|
$doc = [];
|
|
|
|
|
|
|
|
if (!is_array($doc["s"]["t"])) {
|
|
|
|
$doc["s"]["t"] = [$doc["s"]["t"]];
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$doc[\'s\'][\'t\']' => 'array<int, string>',
|
|
|
|
],
|
|
|
|
],
|
2018-05-19 06:41:07 +02:00
|
|
|
'removeTrue' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? new stdClass : true;
|
|
|
|
|
|
|
|
if ($a === true) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesStdClass(stdClass $s) : void {}
|
|
|
|
takesStdClass($a);',
|
|
|
|
],
|
2018-05-31 00:56:44 +02:00
|
|
|
'noReconciliationInElseIf' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
$a = rand(0, 1) ? new A : null;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
// do nothing
|
|
|
|
} elseif (!$a) {
|
|
|
|
$a = new A();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {}',
|
|
|
|
],
|
2018-06-10 16:48:19 +02:00
|
|
|
'removeStringWithIsScalar' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? "hello" : null;
|
|
|
|
|
|
|
|
if (is_scalar($a)) {
|
|
|
|
exit;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'null',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'removeNullWithIsScalar' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? "hello" : null;
|
|
|
|
|
|
|
|
if (!is_scalar($a)) {
|
|
|
|
exit;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2018-06-30 16:52:45 +02:00
|
|
|
'scalarToNumeric' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param scalar $thing
|
|
|
|
*/
|
|
|
|
function Foo($thing) : void {
|
|
|
|
if (is_numeric($thing)) {}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-06-30 16:52:45 +02:00
|
|
|
],
|
2018-07-03 03:59:40 +02:00
|
|
|
'filterSubclassBasedOnParentNegativeInstanceof' => [
|
|
|
|
'<?php
|
|
|
|
class Obj {}
|
|
|
|
class A extends Obj {}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends Obj {}
|
|
|
|
class D extends C {}
|
|
|
|
|
|
|
|
function takesD(D $d) : void {}
|
|
|
|
|
|
|
|
/** @param B|D $bar */
|
|
|
|
function foo(Obj $bar) : void {
|
|
|
|
if (!$bar instanceof A) {
|
|
|
|
takesD($bar);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-07-03 04:53:17 +02:00
|
|
|
'dontEliminateAssignOp' => [
|
|
|
|
'<?php
|
|
|
|
class Obj {}
|
|
|
|
class A extends Obj {}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends Obj {}
|
|
|
|
class D extends C {}
|
|
|
|
class E extends C {}
|
|
|
|
|
|
|
|
function bar(Obj $node) : void {
|
|
|
|
if ($node instanceof B
|
|
|
|
|| $node instanceof D
|
|
|
|
|| $node instanceof E
|
|
|
|
) {
|
|
|
|
if ($node instanceof C) {}
|
|
|
|
if ($node instanceof D) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-07-06 01:46:33 +02:00
|
|
|
'eliminateNonArrays' => [
|
|
|
|
'<?php
|
|
|
|
interface I {}
|
|
|
|
|
|
|
|
function takesArray(array $_a): void {}
|
|
|
|
|
|
|
|
/** @param string|I|string[]|I[] $p */
|
|
|
|
function eliminatesNonArray($p): void {
|
|
|
|
if (is_array($p)) {
|
|
|
|
takesArray($p);
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-07-06 01:46:33 +02:00
|
|
|
],
|
2018-07-09 14:31:43 +02:00
|
|
|
'eliminateNonIterable' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param iterable<string>|null $foo
|
|
|
|
*/
|
|
|
|
function d(?iterable $foo): void {
|
|
|
|
if (is_iterable($foo)) {
|
|
|
|
foreach ($foo as $f) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_iterable($foo)) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
foreach ($foo as $f) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-08-24 22:48:14 +02:00
|
|
|
'isStringServerVar' => [
|
|
|
|
'<?php
|
|
|
|
if (is_string($_SERVER["abc"])) {
|
|
|
|
echo substr($_SERVER["abc"], 1, 2);
|
|
|
|
}',
|
|
|
|
],
|
2018-09-07 17:01:10 +02:00
|
|
|
'notObject' => [
|
|
|
|
'<?php
|
|
|
|
function f(): ?object {
|
|
|
|
return rand(0,1) ? new stdClass : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = f();
|
|
|
|
if (!$data) {}
|
|
|
|
if ($data) {}',
|
|
|
|
],
|
2018-09-10 06:13:59 +02:00
|
|
|
'reconcileWithInstanceof' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {
|
|
|
|
public function b() : bool {
|
|
|
|
return (bool) rand(0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(?A $a) : void {
|
|
|
|
if (!$a || ($a instanceof B && $a->b())) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-09-10 21:38:31 +02:00
|
|
|
'reconcileFloatToEmpty' => [
|
|
|
|
'<?php
|
|
|
|
function bar(float $f) : void {
|
|
|
|
if (!$f) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-10-10 07:57:11 +02:00
|
|
|
'scalarToBool' => [
|
|
|
|
'<?php
|
|
|
|
/** @param mixed $s */
|
|
|
|
function foo($s) : void {
|
|
|
|
if (!is_scalar($s)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_bool($s)) {}
|
|
|
|
if (!is_bool($s)) {}
|
|
|
|
if (is_string($s)) {}
|
|
|
|
if (!is_string($s)) {}
|
|
|
|
if (is_int($s)) {}
|
|
|
|
if (!is_int($s)) {}
|
|
|
|
if (is_float($s)) {}
|
|
|
|
if (!is_float($s)) {}
|
|
|
|
}',
|
|
|
|
],
|
2019-01-03 00:14:47 +01:00
|
|
|
'removeFromArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string> $v
|
|
|
|
*/
|
|
|
|
function foo(array $v) : void {
|
|
|
|
if (!isset($v[0])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($v[0] === " ") {
|
|
|
|
array_shift($v);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($v[0])) {}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-01-03 00:14:47 +01:00
|
|
|
],
|
2019-01-05 06:15:53 +01:00
|
|
|
'arrayEquality' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string, array<array-key, string|int>> $haystack
|
|
|
|
* @param array<array-key, int|string> $needle
|
|
|
|
*/
|
|
|
|
function foo(array $haystack, array $needle) : void {
|
|
|
|
foreach ($haystack as $arr) {
|
|
|
|
if ($arr === $needle) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'classResolvesBackToSelfAfterComparison' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
function getA() : A {
|
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = getA();
|
|
|
|
if ($a instanceof B) {
|
|
|
|
$a = new B;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'A',
|
2019-03-23 19:27:54 +01:00
|
|
|
],
|
2019-01-05 06:15:53 +01:00
|
|
|
],
|
2019-01-08 03:52:58 +01:00
|
|
|
'isNumericCanBeScalar' => [
|
|
|
|
'<?php
|
|
|
|
/** @param scalar $val */
|
|
|
|
function foo($val) : void {
|
|
|
|
if (!is_numeric($val)) {}
|
|
|
|
}',
|
|
|
|
],
|
2019-01-08 23:34:58 +01:00
|
|
|
'classStringCanBeFalsy' => [
|
|
|
|
'<?php
|
|
|
|
/** @param class-string<stdClass>|null $val */
|
|
|
|
function foo(?string $val) : void {
|
|
|
|
if (!$val) {}
|
|
|
|
if ($val) {}
|
|
|
|
}',
|
|
|
|
],
|
2019-02-01 02:31:40 +01:00
|
|
|
'allowStringToObjectReconciliation' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string|object $maybe
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException but it should not
|
|
|
|
*/
|
|
|
|
function foo($maybe) : string {
|
|
|
|
/** @psalm-suppress DocblockTypeContradiction */
|
|
|
|
if ( ! is_string($maybe) && ! is_object($maybe)) {
|
|
|
|
throw new InvalidArgumentException("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_string($maybe) ? $maybe : get_class($maybe);
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-01 02:31:40 +01:00
|
|
|
],
|
|
|
|
'allowObjectToStringReconciliation' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string|object $maybe
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException but it should not
|
|
|
|
*/
|
|
|
|
function bar($maybe) : string {
|
|
|
|
/** @psalm-suppress DocblockTypeContradiction */
|
|
|
|
if ( ! is_string($maybe) && ! is_object($maybe)) {
|
|
|
|
throw new InvalidArgumentException("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_object($maybe) ? get_class($maybe) : $maybe;
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-01 02:31:40 +01:00
|
|
|
],
|
2019-02-11 05:51:26 +01:00
|
|
|
'removeArrayWithIterableCheck' => [
|
2019-02-10 21:56:35 +01:00
|
|
|
'<?php
|
|
|
|
$s = rand(0,1) ? "foo" : [1];
|
|
|
|
if (!is_iterable($s)) {
|
|
|
|
strlen($s);
|
|
|
|
}',
|
|
|
|
],
|
2019-02-11 05:51:26 +01:00
|
|
|
'removeIterableWithIterableCheck' => [
|
|
|
|
'<?php
|
|
|
|
/** @var string|iterable */
|
|
|
|
$s = rand(0,1) ? "foo" : [1];
|
|
|
|
if (!is_iterable($s)) {
|
|
|
|
strlen($s);
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'removeArrayWithIterableCheckWithExit' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0,1) ? "foo" : [1];
|
|
|
|
if (is_iterable($a)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
strlen($a);',
|
|
|
|
],
|
|
|
|
'removeIterableWithIterableCheckWithExit' => [
|
|
|
|
'<?php
|
|
|
|
/** @var string|iterable */
|
|
|
|
$a = rand(0,1) ? "foo" : [1];
|
|
|
|
if (is_iterable($a)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
strlen($a);',
|
|
|
|
],
|
2019-02-10 21:56:35 +01:00
|
|
|
'removeCallable' => [
|
|
|
|
'<?php
|
|
|
|
$s = rand(0,1) ? "strlen" : [1];
|
|
|
|
if (!is_callable($s)) {
|
|
|
|
array_pop($s);
|
2019-02-10 22:23:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 1) ? (function(): void {}) : 1.1;
|
|
|
|
if (!is_callable($a)) {
|
|
|
|
echo $a;
|
|
|
|
}',
|
2019-02-10 21:56:35 +01:00
|
|
|
],
|
2019-02-11 07:44:07 +01:00
|
|
|
'removeCallableWithAssertion' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param mixed $p
|
|
|
|
* @psalm-assert !callable $p
|
|
|
|
* @throws TypeError
|
|
|
|
*/
|
|
|
|
function assertIsNotCallable($p): void { if (!is_callable($p)) throw new TypeError; }
|
|
|
|
|
|
|
|
/** @return callable|float */
|
|
|
|
function f() { return rand(0,1) ? "f" : 1.1; }
|
|
|
|
|
|
|
|
$a = f();
|
|
|
|
assert(!is_callable($a));
|
|
|
|
|
|
|
|
$b = f();
|
|
|
|
assertIsNotCallable($b);
|
|
|
|
|
|
|
|
atan($a);
|
|
|
|
atan($b);',
|
|
|
|
],
|
2019-02-10 22:45:36 +01:00
|
|
|
'PHP71-removeNonCallable' => [
|
2019-02-10 22:27:37 +01:00
|
|
|
'<?php
|
|
|
|
$f = rand(0, 1) ? "strlen" : 1.1;
|
|
|
|
if (is_callable($f)) {
|
|
|
|
Closure::fromCallable($f);
|
|
|
|
}',
|
|
|
|
],
|
2019-02-15 20:42:17 +01:00
|
|
|
'compareObjectLikeToArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<"from"|"to", bool> $a
|
|
|
|
* @return array{from:bool, to: bool}
|
|
|
|
*/
|
|
|
|
function foo(array $a) : array {
|
|
|
|
return $a;
|
|
|
|
}',
|
|
|
|
],
|
2019-02-27 22:21:23 +01:00
|
|
|
'dontChangeScalar' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param scalar|null $val
|
|
|
|
*/
|
|
|
|
function foo($val) : ? bool {
|
|
|
|
if ("1" === $val || 1 === $val) {
|
|
|
|
return true;
|
|
|
|
} elseif ("0" === $val || 0 === $val) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-27 22:21:23 +01:00
|
|
|
],
|
2019-03-02 21:18:29 +01:00
|
|
|
'emptyArrayCheck' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param non-empty-array $x
|
|
|
|
*/
|
|
|
|
function example(array $x): void {}
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
$x = [];
|
|
|
|
if ($x !== []) {
|
|
|
|
example($x);
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-02 21:18:29 +01:00
|
|
|
],
|
|
|
|
'emptyArrayCheckInverse' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param non-empty-array $x
|
|
|
|
*/
|
|
|
|
function example(array $x): void {}
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
$x = [];
|
|
|
|
if ($x === []) {
|
|
|
|
} else {
|
|
|
|
example($x);
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-02 21:18:29 +01:00
|
|
|
],
|
2019-04-11 17:25:18 +02:00
|
|
|
'allowNumericToFoldIntoType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param mixed $width
|
|
|
|
* @param mixed $height
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
function Foo($width, $height) : void {
|
|
|
|
if (!is_numeric($width) || !is_numeric($height)) {
|
|
|
|
throw new RuntimeException("Width & Height were not numeric!");
|
|
|
|
}
|
|
|
|
|
|
|
|
echo sprintf("padding-top:%s%%;", 100 * ($height/$width));
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-04-11 17:25:18 +02:00
|
|
|
],
|
2019-05-02 19:43:18 +02:00
|
|
|
'notEmptyCheckOnMixedInTernary' => [
|
|
|
|
'<?php
|
|
|
|
$a = !empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] !== "off" ? true : false;',
|
|
|
|
],
|
|
|
|
'notEmptyCheckOnMixedInIf' => [
|
|
|
|
'<?php
|
|
|
|
if (!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] !== "off") {
|
|
|
|
$a = true;
|
|
|
|
} else {
|
|
|
|
$a = false;
|
|
|
|
}',
|
|
|
|
],
|
2019-05-28 06:32:17 +02:00
|
|
|
'dontRewriteNullableArrayAfterEmptyCheck' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{x:int,y:int}|null $start_pos
|
|
|
|
* @return array{x:int,y:int}|null
|
|
|
|
*/
|
|
|
|
function foo(?array $start_pos) : ?array {
|
|
|
|
if ($start_pos) {}
|
|
|
|
|
|
|
|
return $start_pos;
|
|
|
|
}',
|
|
|
|
],
|
2019-05-29 13:47:23 +02:00
|
|
|
'falseEqualsBoolean' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
class C extends A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
function bar(A $a) : void {
|
|
|
|
if (false === (!$a instanceof B || !$a instanceof C)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$a->foo();
|
|
|
|
}
|
|
|
|
function baz(A $a) : void {
|
|
|
|
if ((!$a instanceof B || !$a instanceof C) === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$a->foo();
|
|
|
|
}',
|
|
|
|
],
|
2019-06-20 14:59:04 +02:00
|
|
|
'selfInstanceofStatic' => [
|
2019-06-20 14:37:57 +02:00
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo(self $value): void {
|
|
|
|
if ($value instanceof static) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-06-21 01:46:42 +02:00
|
|
|
'reconcileCallable' => [
|
|
|
|
'<?php
|
|
|
|
function reflectCallable(callable $callable): ReflectionFunctionAbstract {
|
|
|
|
if (\is_array($callable)) {
|
|
|
|
return new \ReflectionMethod($callable[0], $callable[1]);
|
|
|
|
} elseif ($callable instanceof \Closure || \is_string($callable)) {
|
|
|
|
return new \ReflectionFunction($callable);
|
|
|
|
} elseif (\is_object($callable)) {
|
|
|
|
return new \ReflectionMethod($callable, "__invoke");
|
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException("Bad");
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-06-21 01:46:42 +02:00
|
|
|
],
|
2019-06-26 03:46:18 +02:00
|
|
|
'noLeakyClassType' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public array $foo = [];
|
|
|
|
public array $bar = [];
|
|
|
|
|
|
|
|
public function setter() : void {
|
|
|
|
if ($this->foo) {
|
|
|
|
$this->foo = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function iffer() : bool {
|
|
|
|
return $this->foo || $this->bar;
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-06-26 03:46:18 +02:00
|
|
|
],
|
|
|
|
'noLeakyForeachType' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var mixed */
|
|
|
|
public $_array_value;
|
|
|
|
|
|
|
|
private function getArrayValue() : ?array {
|
|
|
|
return rand(0, 1) ? [] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setValue(string $var) : void {
|
|
|
|
$this->_array_value = $this->getArrayValue();
|
|
|
|
|
|
|
|
if ($this->_array_value !== null && !count($this->_array_value)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($var) {
|
|
|
|
case "a":
|
|
|
|
foreach ($this->_array_value ?: [] as $v) {}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "b":
|
|
|
|
foreach ($this->_array_value ?: [] as $v) {}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[],
|
2019-07-05 22:24:00 +02:00
|
|
|
['MixedAssignment'],
|
2019-06-26 03:46:18 +02:00
|
|
|
],
|
|
|
|
'nonEmptyThing' => [
|
|
|
|
'<?php
|
|
|
|
/** @param mixed $clips */
|
|
|
|
function foo($clips, bool $found, int $id) : void {
|
|
|
|
if ($found === false) {
|
|
|
|
$clips = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$i = array_search($id, $clips);
|
|
|
|
|
|
|
|
if ($i !== false) {
|
|
|
|
unset($clips[$i]);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[],
|
2019-07-05 22:24:00 +02:00
|
|
|
['MixedArgument', 'MixedArrayAccess', 'MixedAssignment', 'MixedArrayOffset'],
|
2019-06-26 03:46:18 +02:00
|
|
|
],
|
2019-06-27 19:36:39 +02:00
|
|
|
'allowNonEmptyArrayComparison' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param non-empty-array $a
|
|
|
|
* @param array<string> $b
|
|
|
|
*/
|
|
|
|
function foo(array $a, array $b) : void {
|
|
|
|
if ($a === $b) {}
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-06-27 19:36:39 +02:00
|
|
|
],
|
2019-07-15 22:08:38 +02:00
|
|
|
'preventCombinatorialExpansion' => [
|
|
|
|
'<?php
|
|
|
|
function gameOver(
|
|
|
|
int $b0,
|
|
|
|
int $b1,
|
|
|
|
int $b2,
|
|
|
|
int $b3,
|
|
|
|
int $b4,
|
|
|
|
int $b5,
|
|
|
|
int $b6,
|
|
|
|
int $b7,
|
|
|
|
int $b8
|
|
|
|
): bool {
|
|
|
|
if (($b0 === 1 && $b1 === 1 && $b2 === 1)
|
|
|
|
|| ($b3 === 1 && $b4 === 1 && $b5 === 1)
|
|
|
|
|| ($b6 === 1 && $b7 === 1 && $b8 === 1)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-04-16 02:36:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'makeNonNullableNull' => [
|
|
|
|
'<?php
|
|
|
|
class A { }
|
|
|
|
$a = new A();
|
|
|
|
if ($a === null) {
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'TypeDoesNotContainNull',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'makeInstanceOfThingInElseif' => [
|
|
|
|
'<?php
|
|
|
|
class A { }
|
|
|
|
class B { }
|
|
|
|
class C { }
|
2018-01-11 21:50:45 +01:00
|
|
|
$a = rand(0, 10) > 5 ? new A(): new B();
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a instanceof A) {
|
|
|
|
} elseif ($a instanceof C) {
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'functionValueIsNotType' => [
|
|
|
|
'<?php
|
|
|
|
if (json_last_error() === "5") { }',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'stringIsNotTnt' => [
|
|
|
|
'<?php
|
|
|
|
if (5 === "5") { }',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'stringIsNotNull' => [
|
|
|
|
'<?php
|
|
|
|
if (5 === null) { }',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'TypeDoesNotContainNull',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'stringIsNotFalse' => [
|
|
|
|
'<?php
|
|
|
|
if (5 === false) { }',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'typeTransformation' => [
|
|
|
|
'<?php
|
|
|
|
$a = "5";
|
2017-05-06 01:56:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (is_numeric($a)) {
|
|
|
|
if (is_int($a)) {
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2017-12-14 02:48:01 +01:00
|
|
|
'dontEraseNullAfterLessThanCheck' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
$a = mt_rand(0, 1) ? mt_rand(-10, 10): null;
|
2017-12-14 02:48:01 +01:00
|
|
|
|
|
|
|
if ($a < 0) {
|
|
|
|
echo $a + 3;
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyNullOperand',
|
|
|
|
],
|
|
|
|
'dontEraseNullAfterGreaterThanCheck' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
$a = mt_rand(0, 1) ? mt_rand(-10, 10): null;
|
2017-12-14 02:48:01 +01:00
|
|
|
|
|
|
|
if (0 > $a) {
|
|
|
|
echo $a + 3;
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyNullOperand',
|
|
|
|
],
|
2018-02-07 21:20:47 +01:00
|
|
|
'nonRedundantConditionGivenDocblockType' => [
|
2018-02-05 22:57:33 +01:00
|
|
|
'<?php
|
|
|
|
/** @param array[] $arr */
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
if ($arr === "hello") {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2018-02-06 17:27:01 +01:00
|
|
|
],
|
2018-02-11 05:30:40 +01:00
|
|
|
'lessSpecificArrayFields' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{field:string, otherField:string} $array
|
|
|
|
*/
|
|
|
|
function print_field($array) : void {
|
|
|
|
echo $array["field"] . " " . $array["otherField"];
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:10:57 +02:00
|
|
|
print_field(["field" => "name"]);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
2018-02-11 05:30:40 +01:00
|
|
|
],
|
2018-03-21 01:56:33 +01:00
|
|
|
'intersectionIncorrect' => [
|
2018-03-20 14:30:37 +01:00
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
public function bat(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface C {}
|
|
|
|
|
|
|
|
/** @param I&C $a */
|
|
|
|
function takesIandC($a): void {}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function foo(): void {
|
|
|
|
if ($this instanceof I) {
|
|
|
|
takesIandC($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2018-04-10 07:27:26 +02:00
|
|
|
'catchTypeMismatchInBinaryOp' => [
|
|
|
|
'<?php
|
|
|
|
/** @return array<int, string|int> */
|
|
|
|
function getStrings(): array {
|
|
|
|
return ["hello", "world", 50];
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = getStrings();
|
|
|
|
|
|
|
|
if (is_bool($a[0]) && $a[0]) {}',
|
|
|
|
'error_message' => 'DocblockTypeContradiction',
|
|
|
|
],
|
2018-05-14 16:36:41 +02:00
|
|
|
'preventWeakEqualityToObject' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $i, stdClass $s) : void {
|
|
|
|
if ($i == $s) {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2018-05-31 00:56:44 +02:00
|
|
|
'properReconciliationInElseIf' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
$a = rand(0, 1) ? new A : null;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = new A();
|
|
|
|
} elseif (!$a) {
|
|
|
|
$a = new A();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {}',
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
],
|
2018-06-10 16:48:19 +02:00
|
|
|
'allRemovalOfStringWithIsScalar' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? "hello" : "goodbye";
|
|
|
|
|
|
|
|
if (is_scalar($a)) {
|
|
|
|
exit;
|
|
|
|
}',
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
],
|
|
|
|
'noRemovalOfStringWithIsScalar' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? "hello" : "goodbye";
|
|
|
|
|
|
|
|
if (!is_scalar($a)) {
|
|
|
|
exit;
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2018-06-26 00:02:05 +02:00
|
|
|
'impossibleNullEquality' => [
|
|
|
|
'<?php
|
|
|
|
$i = 5;
|
|
|
|
echo $i === null;',
|
|
|
|
'error_message' => 'TypeDoesNotContainNull',
|
|
|
|
],
|
|
|
|
'impossibleTrueEquality' => [
|
|
|
|
'<?php
|
|
|
|
$i = 5;
|
|
|
|
echo $i === true;',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
|
|
|
'impossibleFalseEquality' => [
|
|
|
|
'<?php
|
|
|
|
$i = 5;
|
|
|
|
echo $i === false;',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
|
|
|
'impossibleNumberEquality' => [
|
|
|
|
'<?php
|
|
|
|
$i = 5;
|
|
|
|
echo $i === 3;',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2018-06-29 21:39:17 +02:00
|
|
|
'SKIPPED-noIntersectionOfArrayOrTraversable' => [
|
|
|
|
'<?php
|
|
|
|
function foo(iterable $iterable) : void {
|
|
|
|
if (\is_array($iterable) && $iterable instanceof \Traversable) {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2018-10-10 07:57:11 +02:00
|
|
|
'scalarToBoolContradiction' => [
|
|
|
|
'<?php
|
|
|
|
/** @param mixed $s */
|
|
|
|
function foo($s) : void {
|
|
|
|
if (!is_scalar($s)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_bool($s)) {
|
|
|
|
if (is_bool($s)) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'ParadoxicalCondition',
|
|
|
|
],
|
2018-10-11 15:14:34 +02:00
|
|
|
'noCrashWhenCastingArray' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : string {
|
|
|
|
return (object) ["a" => 1, "b" => 2];
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
2018-11-16 18:26:55 +01:00
|
|
|
'preventStrongEqualityScalarType' => [
|
2018-11-16 17:04:45 +01:00
|
|
|
'<?php
|
|
|
|
function bar(float $f) : void {
|
|
|
|
if ($f === 0) {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2018-11-16 18:26:55 +01:00
|
|
|
'preventYodaStrongEqualityScalarType' => [
|
2018-11-16 17:04:45 +01:00
|
|
|
'<?php
|
|
|
|
function bar(float $f) : void {
|
|
|
|
if (0 === $f) {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2019-01-05 06:15:53 +01:00
|
|
|
'classCannotNotBeSelf' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
function getA() : A {
|
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = getA();
|
|
|
|
if ($a instanceof B) {
|
|
|
|
$a = new B;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a instanceof A) {}',
|
2019-03-23 19:27:54 +01:00
|
|
|
'error_message' => 'RedundantCondition',
|
2019-01-05 06:15:53 +01:00
|
|
|
],
|
2019-04-12 15:38:56 +02:00
|
|
|
'preventImpossibleComparisonToTrue' => [
|
|
|
|
'<?php
|
|
|
|
/** @return false|string */
|
|
|
|
function firstChar(string $s) {
|
|
|
|
return empty($s) ? false : $s[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (true === firstChar("sdf")) {}',
|
|
|
|
'error_message' => 'DocblockTypeContradiction',
|
|
|
|
],
|
|
|
|
'preventAlwaysPossibleComparisonToTrue' => [
|
|
|
|
'<?php
|
|
|
|
/** @return false|string */
|
|
|
|
function firstChar(string $s) {
|
|
|
|
return empty($s) ? false : $s[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (true !== firstChar("sdf")) {}',
|
|
|
|
'error_message' => 'RedundantConditionGivenDocblockType',
|
|
|
|
],
|
2019-04-12 16:30:56 +02:00
|
|
|
'preventAlwaysImpossibleComparisonToFalse' => [
|
|
|
|
'<?php
|
|
|
|
function firstChar(string $s) : string { return $s; }
|
|
|
|
|
|
|
|
if (false === firstChar("sdf")) {}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
|
|
|
'preventAlwaysPossibleComparisonToFalse' => [
|
|
|
|
'<?php
|
|
|
|
function firstChar(string $s) : string { return $s; }
|
|
|
|
|
|
|
|
if (false !== firstChar("sdf")) {}',
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-04-16 02:36:40 +02:00
|
|
|
}
|
2016-06-17 22:05:28 +02:00
|
|
|
}
|