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

967 lines
31 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
2016-11-02 07:29:00 +01:00
use Psalm\Checker\FileChecker;
use Psalm\Checker\StatementsChecker;
2016-10-11 04:49:43 +02:00
use Psalm\Checker\TypeChecker;
use Psalm\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
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
/** @var FileChecker */
protected $file_checker;
/** @var StatementsChecker */
protected $statements_checker;
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-10-11 04:49:43 +02:00
public function setUp()
{
parent::setUp();
2018-01-21 18:44:46 +01:00
$this->file_checker = new FileChecker($this->project_checker, 'somefile.php', 'somefile.php');
$this->file_checker->context = new Context();
$this->statements_checker = new StatementsChecker($this->file_checker);
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,
$this->statements_checker
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())) {
foreach ($reconciled->getTypes() as $type) {
$this->assertInstanceOf('Psalm\Type\Atomic', $type);
}
}
}
/**
* @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(
TypeChecker::isContainedBy(
2018-02-01 06:50:01 +01:00
$this->project_checker->codebase,
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' => ['string|null', 'falsy', 'string|null'],
'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'],
];
}
/**
* @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
*/
public function providerFileCheckerValidCodeParse()
{
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' => 'null|int',
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
}',
],
'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") {}
}',
],
];
}
/**
* @return array
*/
public function providerFileCheckerInvalidCodeParse()
{
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',
],
];
}
2016-06-17 22:05:28 +02:00
}