create(ParserFactory::PREFER_PHP7); $config = new TestConfig(); } public function setUp() { FileChecker::clearCache(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:6 - Possibly undefined variable $b, first seen * on line 3 */ public function testPossiblyUndefinedVarInIf() { $stmts = self::$parser->parse('check(); } public function testNewVarInIf() { $stmts = self::$parser->parse('check(); } public function testNewVarInIfWithElseReturn() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:3 - Possibly undefined variable $array, first * seen on line 3 */ public function testPossiblyUndefinedArrayInIf() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:3 - Possibly undefined variable $array, first * seen on line 3 */ public function testPossiblyUndefinedArrayInForeach() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:4 - Possibly undefined variable $array, first * seen on line 4 */ public function testPossiblyUndefinedArrayInWhileAndForeach() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:6 - Possibly undefined variable $car, first * seen on line 3 */ public function testPossiblyUndefinedVariableInForeach() { $stmts = self::$parser->parse('check(); } public function testSwitchVariableWithContinue() { $stmts = self::$parser->parse('check(); } public function testSwitchVariableWithContinueAndIfs() { $stmts = self::$parser->parse('check(); } public function testSwitchVariableWithFallthrough() { $stmts = self::$parser->parse('check(); } public function testSwitchVariableWithFallthroughStatement() { $stmts = self::$parser->parse('check(); } public function testTryCatchVar() { $stmts = self::$parser->parse('check(true, true, $context); $this->assertEquals('bool', (string) $context->vars_in_scope['$worked']); } public function testWhileVar() { $stmts = self::$parser->parse('check(true, true, $context); $this->assertEquals('bool', (string) $context->vars_in_scope['$worked']); } public function testDoWhileVar() { $stmts = self::$parser->parse('check(true, true, $context); $this->assertEquals('bool', (string) $context->vars_in_scope['$worked']); } public function testAssignmentInIf() { $stmts = self::$parser->parse('check(); } public function testNegatedAssignmentInIf() { $stmts = self::$parser->parse('check(); } public function testAssignInElseIf() { $stmts = self::$parser->parse(' 5) { echo "hello"; } elseif ($row = (rand(0, 10) ? [5] : null)) { echo $row[0]; } '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testIfNotEqualsFalse() { $stmts = self::$parser->parse('check(); } public function testIfNotEqualsNull() { $stmts = self::$parser->parse('check(); } public function testIfNullNotEquals() { $stmts = self::$parser->parse('check(); } public function testIfNullEquals() { $stmts = self::$parser->parse('check(); } public function testPassByRefInIf() { $stmts = self::$parser->parse('check(); } public function testPassByRefInIfCheckAfter() { $stmts = self::$parser->parse('check(); } public function testPassByRefInIfWithBoolean() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage PossiblyUndefinedVariable - somefile.php:9 - Possibly undefined variable $a, first * seen on line 4 */ public function testPossiblyUndefinedVariableInForeachAndIf() { $stmts = self::$parser->parse('setIssueHandler('PossiblyUndefinedVariable', self::$file_filter); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testFunctionExists() { $stmts = self::$parser->parse('check(); } public function testNestedPropertyFetchInElseif() { $stmts = self::$parser->parse('foo) { echo $a; } '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testGlobalReturn() { $stmts = self::$parser->parse('check(); } public function testStatic() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InaccessibleMethod */ public function testInaccessiblePrivateMethod() { $stmts = self::$parser->parse('foo(); '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InaccessibleMethod */ public function testInaccessibleProtectedMethod() { $stmts = self::$parser->parse('foo(); '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testAccessiblePrivateMethodFromSubclass() { $stmts = self::$parser->parse('foo(); } } '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage UndefinedMethod */ public function testInaccessiblePrivateMethodFromSubclass() { $stmts = self::$parser->parse('foo(); } } '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testAccessibleProtectedMethodFromSubclass() { $stmts = self::$parser->parse('foo(); } } '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InaccessibleProperty */ public function testInaccessiblePrivateProperty() { $stmts = self::$parser->parse('foo; '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InaccessibleProperty */ public function testInaccessibleProtectedProperty() { $stmts = self::$parser->parse('foo; '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage UndefinedThisPropertyFetch */ public function testInaccessiblePrivatePropertyFromSubclass() { $stmts = self::$parser->parse('foo; } } '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InaccessibleProperty */ public function testInaccessibleStaticPrivateProperty() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InaccessibleProperty */ public function testInaccessibleStaticProtectedProperty() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InaccessibleProperty */ public function testInaccessibleStaticPrivatePropertyFromSubclass() { $stmts = self::$parser->parse('check(); } public function testAccessibleProtectedPropertyFromSubclass() { $stmts = self::$parser->parse('foo; } } '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testAccessibleStaticPropertyFromSubclass() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InvalidGlobal */ public function testInvalidGlobal() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InvalidStaticVariable */ public function testThisInStatic() { $stmts = self::$parser->parse('check(); } public function testTwoVarLogic() { $stmts = self::$parser->parse('check(); } public function testThreeVarLogic() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage NullArgument */ public function testThreeVarLogicWithChange() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage NullArgument */ public function testThreeVarLogicWithException() { $stmts = self::$parser->parse('check(); } public function testNegateAssertionAndOther() { $stmts = self::$parser->parse(' 1 && is_string($a)) { throw new \Exception("bad"); } '); $file_checker = new FileChecker('somefile.php', $stmts); $context = new Context('somefile.php'); $file_checker->check(true, true, $context); $this->assertEquals('string|null', (string) $context->vars_in_scope['$a']); } public function testRepeatAssertionWithOther() { $stmts = self::$parser->parse(' 1 || is_string($a)) { if (is_string($a)) { echo strpos("e", $a); } } '); $file_checker = new FileChecker('somefile.php', $stmts); $context = new Context('somefile.php'); $file_checker->check(true, true, $context); $this->assertEquals('string|null', (string) $context->vars_in_scope['$a']); } public function testRefineORedType() { $stmts = self::$parser->parse('check(true, true, $context); } }