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); } /** * @dataProvider providerTestReconcilation * * @param string $expected * @param string $type * @param string $string * * @return void */ public function testReconcilation($expected, $type, $string) { $reconciled = Reconciler::reconcileTypes( $type, Type::parseString($string), null, $this->statements_checker ); $this->assertSame( $expected, (string) $reconciled ); if (is_array($reconciled->getTypes())) { foreach ($reconciled->getTypes() as $type) { $this->assertInstanceOf('Psalm\Type\Atomic', $type); } } } /** * @dataProvider providerTestTypeIsContainedBy * * @param string $input * @param string $container * * @return void */ public function testTypeIsContainedBy($input, $container) { $this->assertTrue( TypeChecker::isContainedBy( $this->project_checker->codebase, Type::parseString($input), Type::parseString($container) ) ); } /** * @return void */ public function testNegateFormula() { $formula = [ new Clause(['$a' => ['!falsy']]), ]; $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']]), ]; $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']]), ]; $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']]), ]; $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); } /** * @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'], ] ) ) ); } /** * @return void */ public function testSimplifyCNF() { $formula = [ new Clause(['$a' => ['!falsy']]), new Clause(['$a' => ['falsy'], '$b' => ['falsy']]), ]; $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'], 'nullWithMyObjectPipeNull' => ['null', 'null', 'MyObject|null'], 'nullWithMixed' => ['null', 'null', 'mixed'], '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'], 'falsyWithScalarOrNull' => ['scalar', 'falsy', 'scalar'], 'notMyObjectWithMyObjectPipeBool' => ['bool', '!MyObject', 'MyObject|bool'], 'notMyObjectWithMyObjectPipeNull' => ['null', '!MyObject', 'MyObject|null'], 'notMyObjectWithMyObjectAPipeMyObjectB' => ['MyObjectB', '!MyObjectA', 'MyObjectA|MyObjectB'], 'myObjectWithMyObjectPipeBool' => ['MyObject', 'MyObject', 'MyObject|bool'], 'myObjectWithMyObjectAPipeMyObjectB' => ['MyObjectA', 'MyObjectA', 'MyObjectA|MyObjectB'], 'array' => ['array', 'array', 'array|null'], '2dArray' => ['array>', 'array', 'array>|null'], 'numeric' => ['string', 'numeric', 'string'], ]; } /** * @return array */ public function providerTestTypeIsContainedBy() { return [ 'arrayContainsWithArrayOfStrings' => ['array', 'array'], 'arrayContainsWithArrayOfExceptions' => ['array', 'array'], 'unionContainsWithstring' => ['string', 'string|false'], 'unionContainsWithFalse' => ['false', 'string|false'], 'objectLikeTypeWithPossiblyUndefinedToGeneric' => [ 'array{0:array{a:string}, 1:array{c:string, e:string}}', 'array>' ], 'objectLikeTypeWithPossiblyUndefinedToEmpty' => [ 'array', 'array{a?:string, b?:string}', ], ]; } /** * @return array */ public function providerFileCheckerValidCodeParse() { return [ 'intIsMixed' => [ ' [ ' [], 'error_levels' => ['RedundantConditionGivenDocblockType'], ], 'arrayTypeResolutionFromDocblock' => [ ' [], 'error_levels' => ['RedundantConditionGivenDocblockType'], ], 'typeResolutionFromDocblockInside' => [ ' [], 'error_levels' => ['RedundantConditionGivenDocblockType'], ], 'notInstanceof' => [ ' [ '$out' => 'null|A', ], 'error_levels' => [], 'scope_vars' => [ '$a' => Type::parseString('A'), ], ], 'notInstanceOfProperty' => [ 'foo = new B(); } } $a = new A(); $out = null; if ($a->foo instanceof C) { // do something } else { $out = $a->foo; }', 'assertions' => [ '$out' => 'null|B', ], 'error_levels' => [], 'scope_vars' => [ '$a' => Type::parseString('A'), ], ], 'notInstanceOfPropertyElseif' => [ 'foo)) { } elseif ($a->foo instanceof C) { // do something } else { $out = $a->foo; }', 'assertions' => [ '$out' => 'null|B', ], 'error_levels' => [], 'scope_vars' => [ '$a' => Type::parseString('A'), ], ], 'typeArguments' => [ ' [ '$a' => 'int', '$b' => 'int', '$c' => 'string', '$hours' => 'string|int|float', '$minutes' => 'string|int|float', '$seconds' => 'string|int|float', ], ], 'typeRefinementWithIsNumeric' => [ ' [ ' [ ' 4 ? "hello" : 5; if (is_numeric($a)) { exit; }', 'assertions' => [ '$a' => 'string', ], ], 'typeRefinementWithStringOrTrue' => [ ' 4 ? "hello" : true; if (is_bool($a)) { exit; }', 'assertions' => [ '$a' => 'string', ], ], 'updateMultipleIssetVars' => [ ' [ ' [ ' [ ' [], 'error_levels' => ['RedundantConditionGivenDocblockType'], ], 'ignoreNullCheckAndMaintainNullValue' => [ ' [ '$b' => 'null', ], 'error_levels' => ['TypeDoesNotContainType', 'RedundantCondition'], ], 'ignoreNullCheckAndMaintainNullableValue' => [ ' [ '$b' => 'int|null', ], ], 'ternaryByRefVar' => [ ' [ ' [ ' [ 'bar(); $this->bat(); takesA($this); takesI($this); takesAandI($this); takesIandA($this); } } protected function bar(): void {} } class B extends A implements I { public function bat(): void {} }', ], 'createIntersectionOfInterfaceAndClass' => [ '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);', ], 'unionOfArrayOrTraversable' => [ ' [ ' [ ' [ ' [ ' [ ' 2) { $a = "hello"; } else { $a = false; } } return $a; }', ], 'nullableIntReplacement' => [ ' [ '$a' => 'int|null', ], ], 'eraseNullAfterInequalityCheck' => [ ' 0) { echo $a + 3; } if (0 < $a) { echo $a + 3; }', ], 'twoWrongsDontMakeARight' => [ ' [ '$a' => 'false', ], ], 'instanceofStatic' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ 'foo(); }', ], 'SKIPPED-isArrayOnArrayKeyOffset' => [ '|string>} */ $doc = []; if (!is_array($doc["s"]["t"])) { $doc["s"]["t"] = [$doc["s"]["t"]]; }', 'assertions' => [ '$doc[\'s\'][\'t\']' => 'array', ], ], 'removeTrue' => [ ' [ ' [ ' [ '$a' => 'null', ], ], 'removeNullWithIsScalar' => [ ' [ '$a' => 'string', ], ], 'scalarToNumeric' => [ ' [ ' [ ' 'TypeDoesNotContainNull', ], 'makeInstanceOfThingInElseif' => [ ' 5 ? new A(): new B(); if ($a instanceof A) { } elseif ($a instanceof C) { }', 'error_message' => 'TypeDoesNotContainType', ], 'functionValueIsNotType' => [ ' 'TypeDoesNotContainType', ], 'stringIsNotTnt' => [ ' 'TypeDoesNotContainType', ], 'stringIsNotNull' => [ ' 'TypeDoesNotContainNull', ], 'stringIsNotFalse' => [ ' 'TypeDoesNotContainType', ], 'typeTransformation' => [ ' 'TypeDoesNotContainType', ], 'dontEraseNullAfterLessThanCheck' => [ ' 'PossiblyNullOperand', ], 'dontEraseNullAfterGreaterThanCheck' => [ ' $a) { echo $a + 3; }', 'error_message' => 'PossiblyNullOperand', ], 'nonRedundantConditionGivenDocblockType' => [ ' 'TypeDoesNotContainType', ], 'lessSpecificArrayFields' => [ ' "name"]);', 'error_message' => 'InvalidArgument', ], 'intersectionIncorrect' => [ ' 'InvalidArgument', ], 'catchTypeMismatchInBinaryOp' => [ ' */ function getStrings(): array { return ["hello", "world", 50]; } $a = getStrings(); if (is_bool($a[0]) && $a[0]) {}', 'error_message' => 'DocblockTypeContradiction', ], 'preventWeakEqualityToObject' => [ ' 'TypeDoesNotContainType', ], 'properReconciliationInElseIf' => [ ' 'RedundantCondition', ], 'allRemovalOfStringWithIsScalar' => [ ' 'RedundantCondition', ], 'noRemovalOfStringWithIsScalar' => [ ' 'TypeDoesNotContainType', ], 'impossibleNullEquality' => [ ' 'TypeDoesNotContainNull', ], 'impossibleTrueEquality' => [ ' 'TypeDoesNotContainType', ], 'impossibleFalseEquality' => [ ' 'TypeDoesNotContainType', ], 'impossibleNumberEquality' => [ ' 'TypeDoesNotContainType', ], 'SKIPPED-noIntersectionOfArrayOrTraversable' => [ ' 'TypeDoesNotContainType', ], ]; } }