create(ParserFactory::PREFER_PHP7); $config = \Psalm\Config::getInstance(); $config->throw_exception = true; } public function setUp() { \Psalm\Checker\FileChecker::clearCache(); } /** * @expectedException Psalm\Exception\CodeException */ public function testNullableMethodCall() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithTernaryGuard() { $stmts = self::$_parser->parse('foo() : null; } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithTernaryIfNullGuard() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithTernaryEmptyGuard() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithTernaryIsNullGuard() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithIfGuard() { $stmts = self::$_parser->parse('foo(); } } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testNullableMethodCallWithThis() { $stmts = self::$_parser->parse('a = $a; $this->a->foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithTernaryGuardWithThis() { $stmts = self::$_parser->parse('a = $a; $b = $this->a ? $this->a->foo() : null; } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithTernaryIfNullGuardWithThis() { $stmts = self::$_parser->parse('a = $a; $b = $this->a === null ? null : $this->a->foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithIfGuardWithThis() { $stmts = self::$_parser->parse('a = $a; if ($this->a) { $this->a->foo(); } } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testNullableMethodWithWrongIfGuard() { $stmts = self::$_parser->parse('foo(); } } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithExceptionThrown() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithRedefinitionAndElse() { $stmts = self::$_parser->parse('two = 3; } $one->foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testNullableMethodWithWrongBooleanIfGuard() { $stmts = self::$_parser->parse('foo(); } } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithBooleanIfGuard() { $stmts = self::$_parser->parse('foo(); } } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithNonNullBooleanIfGuard() { $stmts = self::$_parser->parse('foo(); } } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithNonNullBooleanIfGuardAndBooleanAnd() { $stmts = self::$_parser->parse('foo(); } } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodInConditionWithIfGuardBefore() { $stmts = self::$_parser->parse('a && $one->foo()) { // do something } } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testNullableMethodWithWrongIfGuardBefore() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithBooleanIfGuardBefore() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testNullableMethodWithWrongBooleanIfGuardBefore() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithGuardedRedefinition() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithGuardedRedefinitionInElse() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testMethodWithMeaninglessCheck() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testNullableMethodWithGuardedNestedIncompleteRedefinition() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithGuardedNestedRedefinition() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithGuardedSwitchRedefinition() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testNullableMethodWithGuardedSwitchRedefinitionNoDefault() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testNullableMethodWithGuardedSwitchRedefinitionEmptyDefault() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithGuardedSwitchRedefinitionDueToException() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithGuardedSwitchThatAlwaysReturns() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithGuardedNestedRedefinitionWithReturn() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithGuardedNestedRedefinitionWithElseReturn() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testNullableMethodWithGuardedNestedRedefinitionWithUselessElseReturn() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithGuardedNestedRedefinitionWithElseifReturn() { $stmts = self::$_parser->parse('foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithGuardedSwitchBreak() { $stmts = self::$_parser->parse('foo(); break; } } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullableMethodWithGuardedRedefinitionOnThis() { $stmts = self::$_parser->parse('one = $one; if ($this->one === null) { $this->one = new One(); } $this->one->foo(); } }'); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testArrayUnionTypeAssertion() { $stmts = self::$_parser->parse('check(); } public function testArrayUnionTypeAssertionWithIsArray() { $stmts = self::$_parser->parse('check(); } public function testVariableReassignment() { $stmts = self::$_parser->parse('bar(); '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testVariableReassignmentInIf() { $stmts = self::$_parser->parse('bar(); } '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testVariableReassignmentInIfWithOutsideCall() { $stmts = self::$_parser->parse('bar(); } $one->bar(); '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testUnionTypeFlow() { $stmts = self::$_parser->parse('foo(); } else { if ($var instanceof Two) { $var->bar(); } else if ($var) { $var->baz(); } } '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testUnionTypeFlowWithThrow() { $stmts = self::$_parser->parse('foo(); } '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testUnionTypeFlowWithElseif() { $stmts = self::$_parser->parse('foo(); } '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testUnnecessaryInstanceof() { $stmts = self::$_parser->parse('foo(); } '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testUnNegatableInstanceof() { $stmts = self::$_parser->parse('foo(); } else { // do something } '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testTypeAdjustment() { $stmts = self::$_parser->parse('check(); $return_stmt = array_pop($stmts); $this->assertSame('int|string', (string) $return_stmt->exprs[0]->inferredType); } public function testTypeMixedAdjustment() { $stmts = self::$_parser->parse('check(); $return_stmt = array_pop($stmts); $this->assertSame('int|string', (string) $return_stmt->exprs[0]->inferredType); } public function testTypeAdjustmentIfNull() { $stmts = self::$_parser->parse(' 5 ? new A : null; if ($var === null) { $var = new B; } echo $var; '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); $return_stmt = array_pop($stmts); $this->assertSame('A|B', (string) $return_stmt->exprs[0]->inferredType); } public function testWhileTrue() { $stmts = self::$_parser->parse('foo()) { $row[0] = "bad"; } } } '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testWrongParam() { $stmts = self::$_parser->parse('bar(5); '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testPassingParam() { $stmts = self::$_parser->parse('bar(new A); '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testNullToNullableParam() { $stmts = self::$_parser->parse('bar(null); '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testIntToNullableObjectParam() { $stmts = self::$_parser->parse('bar(5); '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testObjectToNullableObjectParam() { $stmts = self::$_parser->parse('bar(new A); '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException Psalm\Exception\CodeException */ public function testParamCoercionWithBadArg() { $stmts = self::$_parser->parse('bar(); } } } '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testParamCoercion() { $stmts = self::$_parser->parse('bar(); } } } '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testParamElseifCoercion() { $stmts = self::$_parser->parse('bar(); } elseif ($a instanceof C) { $a->baz(); } } } '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testAssignInsideForeach() { $stmts = self::$_parser->parse('check(true, true, $context); $this->assertSame('bool', (string) $context->vars_in_scope['b']); } public function testAssignInsideForeachWithBreak() { $stmts = self::$_parser->parse('check(true, true, $context); $this->assertSame('bool', (string) $context->vars_in_scope['b']); } /** * @expectedException Psalm\Exception\CodeException * @expectedExceptionMessage NullReference */ public function testNullCheckInsideForeachWithNoLeaveStatement() { $stmts = self::$_parser->parse(' */ public static function loadMultiple() { return [new A, null]; } public function bar() { } } foreach (A::loadMultiple() as $a) { if ($a === null) { // do nothing } $a->bar(); } '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $context = new Context('somefile.php'); $file_checker->check(); } public function testNullCheckInsideForeachWithContinue() { $stmts = self::$_parser->parse(' */ public static function loadMultiple() { return [new A, null]; } public function bar() { } } foreach (A::loadMultiple() as $a) { if ($a === null) { continue; } $a->bar(); } '); $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $context = new Context('somefile.php'); $file_checker->check(); } }