1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/tests/TypeReconciliationTest.php

1295 lines
42 KiB
PHP
Raw Normal View History

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
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-11-02 07:29:00 +01:00
use Psalm\Context;
2016-10-11 04:49:43 +02:00
use Psalm\Type;
2018-05-07 07:26:06 +02:00
use Psalm\Type\Algebra;
use Psalm\Type\Reconciler;
2016-06-17 22:05:28 +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;
2018-11-06 03:57:36 +01:00
/** @var FileAnalyzer */
2018-11-11 18:01:14 +01:00
protected $file_analyzer;
2018-11-06 03:57:36 +01:00
/** @var StatementsAnalyzer */
2018-11-11 18:01:14 +01:00
protected $statements_analyzer;
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-10-11 04:49:43 +02:00
public function setUp()
{
parent::setUp();
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
/**
* @dataProvider providerTestReconcilation
2017-05-27 02:16:18 +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
*/
public function testReconcilation($expected, $type, $string)
2016-06-17 22:05:28 +02:00
{
$reconciled = Reconciler::reconcileTypes(
$type,
Type::parseString($string),
null,
2018-11-11 18:01:14 +01:00
$this->statements_analyzer
2016-06-17 22:05:28 +02:00
);
2017-05-27 02:05:57 +02:00
$this->assertSame(
$expected,
(string) $reconciled
2016-06-17 22:05:28 +02:00
);
if (is_array($reconciled->getTypes())) {
$this->assertContainsOnlyInstancesOf('Psalm\Type\Atomic', $reconciled->getTypes());
}
}
/**
* @dataProvider providerTestTypeIsContainedBy
2017-05-27 02:16:18 +02:00
*
* @param string $input
* @param string $container
2017-05-27 02:16:18 +02:00
*
* @return void
*/
public function testTypeIsContainedBy($input, $container)
{
$this->assertTrue(
2018-11-06 03:57:36 +01:00
TypeAnalyzer::isContainedBy(
2018-11-11 18:01:14 +01:00
$this->project_analyzer->getCodebase(),
Type::parseString($input),
Type::parseString($container)
)
);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testNegateFormula()
{
$formula = [
new Clause(['$a' => ['!falsy']]),
];
2018-05-07 07:26:06 +02:00
$negated_formula = Algebra::negateFormula($formula);
$this->assertSame(1, count($negated_formula));
$this->assertSame(['$a' => ['falsy']], $negated_formula[0]->possibilities);
$formula = [
new Clause(['$a' => ['!falsy'], '$b' => ['!falsy']]),
];
2018-05-07 07:26:06 +02:00
$negated_formula = Algebra::negateFormula($formula);
$this->assertSame(2, count($negated_formula));
$this->assertSame(['$a' => ['falsy']], $negated_formula[0]->possibilities);
$this->assertSame(['$b' => ['falsy']], $negated_formula[1]->possibilities);
$formula = [
new Clause(['$a' => ['!falsy']]),
new Clause(['$b' => ['!falsy']]),
];
2018-05-07 07:26:06 +02:00
$negated_formula = Algebra::negateFormula($formula);
$this->assertSame(1, count($negated_formula));
$this->assertSame(['$a' => ['falsy'], '$b' => ['falsy']], $negated_formula[0]->possibilities);
$formula = [
new Clause(['$a' => ['int', 'string'], '$b' => ['!falsy']]),
];
2018-05-07 07:26:06 +02:00
$negated_formula = Algebra::negateFormula($formula);
$this->assertSame(3, count($negated_formula));
$this->assertSame(['$a' => ['!int']], $negated_formula[0]->possibilities);
$this->assertSame(['$a' => ['!string']], $negated_formula[1]->possibilities);
$this->assertSame(['$b' => ['falsy']], $negated_formula[2]->possibilities);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testContainsClause()
{
$this->assertTrue(
(new Clause(
[
'$a' => ['!falsy'],
'$b' => ['!falsy'],
]
))->contains(
new Clause(
[
'$a' => ['!falsy'],
]
)
)
);
$this->assertFalse(
(new Clause(
[
'$a' => ['!falsy'],
]
))->contains(
new Clause(
[
'$a' => ['!falsy'],
'$b' => ['!falsy'],
]
)
)
);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testSimplifyCNF()
{
$formula = [
new Clause(['$a' => ['!falsy']]),
new Clause(['$a' => ['falsy'], '$b' => ['falsy']]),
];
2018-05-07 07:26:06 +02:00
$simplified_formula = Algebra::simplifyCNF($formula);
$this->assertSame(2, count($simplified_formula));
$this->assertSame(['$a' => ['!falsy']], $simplified_formula[0]->possibilities);
$this->assertSame(['$b' => ['falsy']], $simplified_formula[1]->possibilities);
}
/**
* @return array
*/
public function providerTestReconcilation()
{
return [
'notNullWithObject' => ['MyObject', '!null', 'MyObject'],
'notNullWithObjectPipeNull' => ['MyObject', '!null', 'MyObject|null'],
'notNullWithMyObjectPipeFalse' => ['MyObject|false', '!null', 'MyObject|false'],
'notNullWithMixed' => ['mixed', '!null', 'mixed'],
'notEmptyWithMyObject' => ['MyObject', '!falsy', 'MyObject'],
'notEmptyWithMyObjectPipeNull' => ['MyObject', '!falsy', 'MyObject|null'],
'notEmptyWithMyObjectPipeFalse' => ['MyObject', '!falsy', 'MyObject|false'],
'notEmptyWithMixed' => ['mixed', '!falsy', 'mixed'],
// @todo in the future this should also work
//'notEmptyWithMyObjectFalseTrue' => ['MyObject|true', '!falsy', 'MyObject|bool'],
2016-12-12 05:41:11 +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'],
'falsyWithMixed' => ['mixed', 'falsy', 'mixed'],
'falsyWithBool' => ['false', 'falsy', 'bool'],
'falsyWithStringOrNull' => ['null|string', 'falsy', 'string|null'],
2018-04-05 18:11:58 +02:00
'falsyWithScalarOrNull' => ['scalar', 'falsy', 'scalar'],
2016-10-11 04:49:43 +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
'myObjectWithMyObjectPipeBool' => ['MyObject', 'MyObject', 'MyObject|bool'],
'myObjectWithMyObjectAPipeMyObjectB' => ['MyObjectA', 'MyObjectA', 'MyObjectA|MyObjectB'],
2016-10-11 04:49:43 +02:00
'array' => ['array<mixed, mixed>', 'array', 'array|null'],
2016-10-11 04:49:43 +02:00
'2dArray' => ['array<mixed, array<mixed, string>>', 'array', 'array<array<string>>|null'],
2017-05-27 02:05:57 +02:00
'numeric' => ['string', 'numeric', 'string'],
'nullableClassString' => ['null', 'falsy', '?class-string'],
];
}
/**
* @return array
*/
public function providerTestTypeIsContainedBy()
{
return [
'arrayContainsWithArrayOfStrings' => ['array<string>', 'array'],
'arrayContainsWithArrayOfExceptions' => ['array<Exception>', 'array'],
'unionContainsWithstring' => ['string', 'string|false'],
2017-05-27 02:05:57 +02:00
'unionContainsWithFalse' => ['false', 'string|false'],
'objectLikeTypeWithPossiblyUndefinedToGeneric' => [
'array{0:array{a:string}, 1:array{c:string, e:string}}',
'array<int, array<string, string>>'
],
'objectLikeTypeWithPossiblyUndefinedToEmpty' => [
'array<empty, empty>',
'array{a?:string, b?:string}',
],
];
}
/**
* @return array
*/
2018-11-06 03:57:36 +01:00
public function providerValidCodeParse()
{
return [
'intIsMixed' => [
'<?php
/** @param mixed $a */
2018-01-11 21:50:45 +01:00
function foo($a): void {
$b = 5;
2017-05-06 01:56:45 +02:00
if ($b === $a) { }
2017-05-27 02:05:57 +02:00
}',
],
'typeResolutionFromDocblock' => [
'<?php
class A { }
2017-05-06 01:56:45 +02:00
/**
* @param A $a
* @return void
*/
function fooFoo($a) {
if ($a instanceof A) {
}
2017-05-27 02:05:57 +02:00
}',
'assertions' => [],
'error_levels' => ['RedundantConditionGivenDocblockType'],
],
'arrayTypeResolutionFromDocblock' => [
'<?php
/**
* @param string[] $strs
* @return void
*/
function foo(array $strs) {
foreach ($strs as $str) {
if (is_string($str)) {}
}
2017-05-27 02:05:57 +02:00
}',
'assertions' => [],
'error_levels' => ['RedundantConditionGivenDocblockType'],
],
'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
}',
'assertions' => [],
'error_levels' => ['RedundantConditionGivenDocblockType'],
],
'notInstanceof' => [
'<?php
class A { }
2017-05-06 01:56:45 +02:00
class B extends A { }
2017-05-06 01:56:45 +02:00
$out = null;
2017-05-06 01:56:45 +02:00
if ($a instanceof B) {
// do something
}
else {
$out = $a;
}',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$out' => 'null|A',
],
'error_levels' => [],
'scope_vars' => [
2017-05-27 02:05:57 +02:00
'$a' => Type::parseString('A'),
],
],
'notInstanceOfProperty' => [
'<?php
class B { }
2017-05-06 01:56:45 +02:00
class C extends B { }
2017-05-06 01:56:45 +02:00
class A {
/** @var B */
public $foo;
2017-05-06 01:56:45 +02:00
public function __construct() {
$this->foo = new B();
}
}
2017-05-06 01:56:45 +02:00
$a = new A();
2017-05-06 01:56:45 +02:00
$out = null;
2017-05-06 01:56:45 +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',
],
'error_levels' => [],
'scope_vars' => [
2017-05-27 02:05:57 +02:00
'$a' => Type::parseString('A'),
],
],
'notInstanceOfPropertyElseif' => [
'<?php
class B { }
2017-05-06 01:56:45 +02:00
class C extends B { }
2017-05-06 01:56:45 +02:00
class A {
/** @var string|B */
public $foo = "";
}
2017-05-06 01:56:45 +02:00
$out = null;
2017-05-06 01:56:45 +02:00
if (is_string($a->foo)) {
2017-05-06 01:56:45 +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',
],
'error_levels' => [],
'scope_vars' => [
2017-05-27 02:05:57 +02:00
'$a' => Type::parseString('A'),
],
],
'typeArguments' => [
'<?php
$a = min(0, 1);
$b = min([0, 1]);
$c = min("a", "b");
$d = min(1, 2, 3, 4);
$e = min(1, 2, 3, 4, 5);
sscanf("10:05:03", "%d:%d:%d", $hours, $minutes, $seconds);',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$a' => 'int',
'$b' => 'int',
'$c' => 'string',
'$hours' => 'string|int|float',
'$minutes' => 'string|int|float',
'$seconds' => 'string|int|float',
2017-05-27 02:05:57 +02:00
],
],
'typeRefinementWithIsNumeric' => [
'<?php
/** @return void */
function fooFoo(string $a) {
if (is_numeric($a)) { }
}
2017-05-06 01:56:45 +02:00
$b = rand(0, 1) ? 5 : false;
2017-05-27 02:05:57 +02:00
if (is_numeric($b)) { }',
],
'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
}',
],
'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;
}',
'assertions' => [
'$a' => 'string',
],
],
'updateMultipleIssetVars' => [
'<?php
/** @return void **/
function foo(string $s) {}
2017-05-06 01:56:45 +02:00
$a = rand(0, 1) ? ["hello"] : null;
if (isset($a[0])) {
foo($a[0]);
2017-05-27 02:05:57 +02:00
}',
],
'updateMultipleIssetVarsWithVariableOffset' => [
'<?php
/** @return void **/
function foo(string $s) {}
2017-05-06 01:56:45 +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
}',
],
'instanceOfSubtypes' => [
'<?php
abstract class A {}
class B extends A {}
2017-05-06 01:56:45 +02:00
abstract class C {}
class D extends C {}
2017-05-06 01:56:45 +02:00
function makeA(): A {
return new B();
}
2017-05-06 01:56:45 +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) { }',
],
'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
throw new \LogicException("Runtime error");
2017-05-27 02:05:57 +02:00
}',
'assertions' => [],
'error_levels' => ['RedundantConditionGivenDocblockType'],
],
'ignoreNullCheckAndMaintainNullValue' => [
'<?php
$a = null;
if ($a !== null) { }
$b = $a;',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$b' => 'null',
],
'error_levels' => ['TypeDoesNotContainType', 'RedundantCondition'],
],
'ignoreNullCheckAndMaintainNullableValue' => [
'<?php
$a = rand(0, 1) ? 5 : null;
if ($a !== null) { }
$b = $a;',
'assertions' => [
'$b' => 'int|null',
2017-05-27 02:05:57 +02:00
],
],
'ternaryByRefVar' => [
'<?php
2018-01-11 21:50:45 +01:00
function foo(): void {
$b = null;
2018-01-11 21:50:45 +01:00
$c = rand(0, 1) ? bar($b): null;
if (is_int($b)) { }
}
2018-01-11 21:50:45 +01:00
function bar(?int &$a): void {
$a = 5;
2017-05-27 02:05:57 +02:00
}',
],
'ternaryByRefVarInConditional' => [
'<?php
2018-01-11 21:50:45 +01:00
function foo(): void {
$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 {
$a = 5;
2017-05-27 02:05:57 +02:00
}',
],
'possibleInstanceof' => [
'<?php
interface I1 {}
interface I2 {}
2017-05-06 01:56:45 +02:00
class A
{
2018-01-11 21:50:45 +01:00
public function foo(): void {
if ($this instanceof I1 || $this instanceof I2) {}
}
2017-05-27 02:05:57 +02:00
}',
],
'intersection' => [
'<?php
interface I {
2018-01-11 21:50:45 +01:00
public function bat(): void;
}
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 {}
/** @param A&I $a */
function takesAandI($a): void {}
/** @param I&A $a */
function takesIandA($a): void {}
2017-05-06 01:56:45 +02:00
class A {
2018-03-22 22:55:36 +01:00
/**
* @return A&I|null
*/
public function foo() {
if ($this instanceof I) {
$this->bar();
$this->bat();
2017-05-06 01:56:45 +02:00
takesA($this);
takesI($this);
takesAandI($this);
takesIandA($this);
}
}
2017-05-06 01:56:45 +02:00
2018-01-11 21:50:45 +01:00
protected function bar(): void {}
}
2017-05-06 01:56:45 +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
}',
],
'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) {}
}',
],
'isTruthy' => [
'<?php
function f(string $s = null): string {
if ($s == true) {
return $s;
}
return "backup";
}',
],
'stringOrCallableArg' => [
'<?php
/**
* @param string|callable $param
*/
function f($param): void {}
f("is_array");',
],
'stringOrCallableOrObjectArg' => [
'<?php
/**
* @param string|callable|object $param
*/
function f($param): void {}
f("is_array");',
],
'intOrFloatArg' => [
'<?php
/**
* @param int|float $param
*/
function f($param): void {}
f(5.0);
f(5);',
],
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;
}',
],
'nullableIntReplacement' => [
'<?php
$a = rand(0, 1) ? 5 : null;
$b = (bool)rand(0, 1);
if ($b || $a !== null) {
$a = 3;
}',
'assertions' => [
'$a' => 'int|null',
],
],
'eraseNullAfterInequalityCheck' => [
'<?php
2018-01-11 21:50:45 +01:00
$a = mt_rand(0, 1) ? mt_rand(-10, 10): null;
if ($a > 0) {
echo $a + 3;
}
if (0 < $a) {
echo $a + 3;
}',
],
'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;
}
}',
],
'isAClass' => [
'<?php
class A {}
$a_class = rand(0, 1) ? A::class : "blargle";
if (is_a($a_class, A::class, true)) {
echo "cool";
}',
],
'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);
}',
],
'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;
}',
],
'numericStringAssertion' => [
'<?php
/**
* @param mixed $a
*/
function foo($a, string $b) : void {
if (is_numeric($b) && $a === $b) {
echo $a;
}
}'
],
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";
}
}',
],
'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";
}
}',
],
'allowWeakEqualityScalarType' => [
'<?php
function foo(int $i) : void {
if ($i == "5") {}
if ("5" == $i) {}
}
function bar(float $f) : void {
if ($f === 0) {}
if (0 === $f) {}
}',
],
'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();
}',
],
'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>',
],
],
'removeTrue' => [
'<?php
$a = rand(0, 1) ? new stdClass : true;
if ($a === true) {
exit;
}
function takesStdClass(stdClass $s) : void {}
takesStdClass($a);',
],
'noReconciliationInElseIf' => [
'<?php
class A {}
$a = rand(0, 1) ? new A : null;
if (rand(0, 1)) {
// do nothing
} elseif (!$a) {
$a = new A();
}
if ($a) {}',
],
'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',
],
],
'scalarToNumeric' => [
'<?php
/**
* @param scalar $thing
*/
function Foo($thing) : void {
if (is_numeric($thing)) {}
}'
],
'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);
}
}',
],
'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) {}
}
}',
],
'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);
}
}'
],
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) {}
}
}',
],
'isStringServerVar' => [
'<?php
if (is_string($_SERVER["abc"])) {
echo substr($_SERVER["abc"], 1, 2);
}',
],
'notObject' => [
'<?php
function f(): ?object {
return rand(0,1) ? new stdClass : null;
}
$data = f();
if (!$data) {}
if ($data) {}',
],
'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) {}
}',
],
'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)) {}
}',
],
];
}
/**
* @return array
*/
2018-11-06 03:57:36 +01:00
public function providerInvalidCodeParse()
{
return [
'makeNonNullableNull' => [
'<?php
class A { }
$a = new A();
if ($a === null) {
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'TypeDoesNotContainNull',
],
'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();
if ($a instanceof A) {
} elseif ($a instanceof C) {
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'TypeDoesNotContainType',
],
'functionValueIsNotType' => [
'<?php
if (json_last_error() === "5") { }',
2017-05-27 02:05:57 +02:00
'error_message' => 'TypeDoesNotContainType',
],
'stringIsNotTnt' => [
'<?php
if (5 === "5") { }',
2017-05-27 02:05:57 +02:00
'error_message' => 'TypeDoesNotContainType',
],
'stringIsNotNull' => [
'<?php
if (5 === null) { }',
2017-05-27 02:05:57 +02:00
'error_message' => 'TypeDoesNotContainNull',
],
'stringIsNotFalse' => [
'<?php
if (5 === false) { }',
2017-05-27 02:05:57 +02:00
'error_message' => 'TypeDoesNotContainType',
],
'typeTransformation' => [
'<?php
$a = "5";
2017-05-06 01:56:45 +02:00
if (is_numeric($a)) {
if (is_int($a)) {
echo $a;
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'TypeDoesNotContainType',
],
'dontEraseNullAfterLessThanCheck' => [
'<?php
2018-01-11 21:50:45 +01:00
$a = mt_rand(0, 1) ? mt_rand(-10, 10): null;
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;
if (0 > $a) {
echo $a + 3;
}',
'error_message' => 'PossiblyNullOperand',
],
'nonRedundantConditionGivenDocblockType' => [
'<?php
/** @param array[] $arr */
function foo(array $arr) : void {
if ($arr === "hello") {}
}',
'error_message' => 'TypeDoesNotContainType',
],
'lessSpecificArrayFields' => [
'<?php
/**
* @param array{field:string, otherField:string} $array
*/
function print_field($array) : void {
echo $array["field"] . " " . $array["otherField"];
}
print_field(["field" => "name"]);',
'error_message' => 'InvalidArgument',
],
'intersectionIncorrect' => [
'<?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',
],
'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',
],
'preventWeakEqualityToObject' => [
'<?php
function foo(int $i, stdClass $s) : void {
if ($i == $s) {}
}',
'error_message' => 'TypeDoesNotContainType',
],
'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',
],
'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',
],
'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',
],
'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',
],
'noCrashWhenCastingArray' => [
'<?php
function foo() : string {
return (object) ["a" => 1, "b" => 2];
}',
'error_message' => 'InvalidReturnStatement',
],
];
}
2016-06-17 22:05:28 +02:00
}