create(ParserFactory::PREFER_PHP7); } public function setUp() { $config = new TestConfig(); $config->throw_exception = true; FileChecker::clearCache(); } public function testNewVarInIf() { $stmts = self::$parser->parse('foo = []; } if (!is_array($this->foo)) { // do something } } } '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testPropertyWithoutTypeSuppressingIssue() { $filter = new Config\FileFilter(false); $filter->addFile('somefile.php'); Config::getInstance()->setIssueHandler('MissingPropertyType', $filter); Config::getInstance()->setIssueHandler('MixedAssignment', $filter); $stmts = self::$parser->parse('foo; '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage UndefinedPropertyAssignment */ public function testUndefinedPropertyAssignment() { $stmts = self::$parser->parse('foo = "cool"; '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage UndefinedPropertyFetch */ public function testUndefinedPropertyFetch() { $stmts = self::$parser->parse('foo; '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage UndefinedThisPropertyAssignment */ public function testUndefinedThisPropertyAssignment() { $stmts = self::$parser->parse('foo = "cool"; } } '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage UndefinedThisPropertyFetch */ public function testUndefinedThisPropertyFetch() { $stmts = self::$parser->parse('foo; } } '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage MissingPropertyDeclaration */ public function testMissingPropertyDeclaration() { $stmts = self::$parser->parse('foo = "cool"; } '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage MissingPropertyType */ public function testMissingPropertyType() { $stmts = self::$parser->parse('check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InvalidPropertyAssignment */ public function testBadAssignment() { $stmts = self::$parser->parse('foo = 5; } } '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InvalidPropertyAssignment */ public function testBadAssignmentAsWell() { $stmts = self::$parser->parse('foo = "bar"; '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InvalidPropertyFetch */ public function testBadFetch() { $stmts = self::$parser->parse('foo; '); $file_checker = new FileChecker('somefile.php', $stmts); $file_checker->check(); } public function testSharedPropertyInIf() { $stmts = self::$parser->parse('foo; } '); $file_checker = new FileChecker('somefile.php', $stmts); $context = new Context('somefile.php'); $file_checker->check(true, true, $context); $this->assertEquals('null|string|int', (string) $context->vars_in_scope['$b']); } public function testSharedPropertyInElseIf() { $stmts = self::$parser->parse('foo; } '); $file_checker = new FileChecker('somefile.php', $stmts); $context = new Context('somefile.php'); $file_checker->check(true, true, $context); $this->assertEquals('null|string|int', (string) $context->vars_in_scope['$b']); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage MixedPropertyFetch */ public function testMixedPropertyFetch() { $filter = new Config\FileFilter(false); $filter->addFile('somefile.php'); Config::getInstance()->setIssueHandler('MissingPropertyType', $filter); Config::getInstance()->setIssueHandler('MixedAssignment', $filter); $stmts = self::$parser->parse('foo; '); $file_checker = new FileChecker('somefile.php', $stmts); $context = new Context('somefile.php'); $file_checker->check(true, true, $context); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage MixedPropertyAssignment */ public function testMixedPropertyAssignment() { $filter = new Config\FileFilter(false); $filter->addFile('somefile.php'); Config::getInstance()->setIssueHandler('MissingPropertyType', $filter); Config::getInstance()->setIssueHandler('MixedAssignment', $filter); $stmts = self::$parser->parse('foo = "hello"; '); $file_checker = new FileChecker('somefile.php', $stmts); $context = new Context('somefile.php'); $file_checker->check(true, true, $context); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage NullPropertyAssignment */ public function testNullablePropertyAssignment() { $stmts = self::$parser->parse('foo = "hello"; '); $file_checker = new FileChecker('somefile.php', $stmts); $context = new Context('somefile.php'); $file_checker->check(true, true, $context); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage NullPropertyFetch */ public function testNullablePropertyFetch() { $stmts = self::$parser->parse('foo; '); $file_checker = new FileChecker('somefile.php', $stmts); $context = new Context('somefile.php'); $file_checker->check(true, true, $context); } public function testReflectionProperties() { $stmts = self::$parser->parse('name . " - " . $a->class; '); $file_checker = new FileChecker('somefile.php', $stmts); $context = new Context('somefile.php'); $file_checker->check(true, true, $context); } }