2016-07-12 06:53:36 +02:00
|
|
|
<?php
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm\Tests;
|
2016-07-12 06:53:36 +02:00
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2016-07-12 06:53:36 +02:00
|
|
|
|
|
|
|
class PropertyTypeTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-12-14 18:55:23 +01:00
|
|
|
/** @var \PhpParser\Parser */
|
2016-11-02 07:29:00 +01:00
|
|
|
protected static $parser;
|
|
|
|
protected static $file_filter;
|
2016-07-12 06:53:36 +02:00
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-07-12 06:53:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2016-12-15 01:24:33 +01:00
|
|
|
$config = new TestConfig();
|
|
|
|
$config->throw_exception = true;
|
2016-11-02 07:29:00 +01:00
|
|
|
FileChecker::clearCache();
|
2016-07-12 06:53:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNewVarInIf()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-07-12 06:53:36 +02:00
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
public $foo;
|
|
|
|
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-07-12 06:53:36 +02:00
|
|
|
public function bar()
|
|
|
|
{
|
|
|
|
if (rand(0,10) === 5) {
|
|
|
|
$this->foo = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_array($this->foo)) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
2016-07-12 06:53:36 +02:00
|
|
|
$file_checker->check();
|
|
|
|
}
|
2016-10-03 17:40:42 +02:00
|
|
|
|
2016-12-15 01:24:33 +01:00
|
|
|
public function testPropertyWithoutTypeSuppressingIssue()
|
|
|
|
{
|
|
|
|
$filter = new Config\FileFilter(false);
|
2016-12-29 16:24:10 +01:00
|
|
|
$filter->addFile('somefile.php');
|
2016-12-15 01:24:33 +01:00
|
|
|
Config::getInstance()->setIssueHandler('MissingPropertyType', $filter);
|
2016-12-17 00:56:23 +01:00
|
|
|
Config::getInstance()->setIssueHandler('MixedAssignment', $filter);
|
2016-12-15 01:24:33 +01:00
|
|
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = (new A)->foo;
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-12-17 04:16:29 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage UndefinedPropertyAssignment
|
|
|
|
*/
|
|
|
|
public function testUndefinedPropertyAssignment()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
}
|
|
|
|
|
|
|
|
(new A)->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('<?php
|
|
|
|
class A {
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new A)->foo;
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage UndefinedThisPropertyAssignment
|
|
|
|
*/
|
|
|
|
public function testUndefinedThisPropertyAssignment()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : void {
|
|
|
|
$this->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('<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : void {
|
|
|
|
echo $this->foo;
|
|
|
|
}
|
2016-12-29 14:42:39 +01:00
|
|
|
}
|
2016-12-17 04:16:29 +01:00
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-12-15 01:24:33 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage MissingPropertyDeclaration
|
|
|
|
*/
|
|
|
|
public function testMissingPropertyDeclaration()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress UndefinedPropertyAssignment */
|
|
|
|
function fooDo() : void {
|
|
|
|
(new A)->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('<?php
|
|
|
|
class A {
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-12-12 05:41:11 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidPropertyAssignment
|
|
|
|
*/
|
|
|
|
public function testBadAssignment()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function bar() : void
|
|
|
|
{
|
|
|
|
$this->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('<?php
|
|
|
|
$a = "hello";
|
|
|
|
$a->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('<?php
|
|
|
|
$a = "hello";
|
|
|
|
echo $a->foo;
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$file_checker->check();
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:40:42 +02:00
|
|
|
public function testSharedPropertyInIf()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-03 17:40:42 +02:00
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
class B {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
2016-12-11 19:48:11 +01:00
|
|
|
$a = rand(0, 10) ? new A() : (rand(0, 10) ? new B() : null);
|
2016-10-03 17:40:42 +02:00
|
|
|
$b = null;
|
|
|
|
|
|
|
|
if ($a instanceof A || $a instanceof B) {
|
|
|
|
$b = $a->foo;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
2016-10-03 17:40:42 +02:00
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
$this->assertEquals('null|string|int', (string) $context->vars_in_scope['$b']);
|
2016-10-03 17:40:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSharedPropertyInElseIf()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-03 17:40:42 +02:00
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
class B {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
2016-12-11 19:48:11 +01:00
|
|
|
$a = rand(0, 10) ? new A() : new B();
|
2016-10-03 17:40:42 +02:00
|
|
|
$b = null;
|
|
|
|
|
|
|
|
if (rand(0, 10) === 4) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
elseif ($a instanceof A || $a instanceof B) {
|
|
|
|
$b = $a->foo;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
2016-10-03 17:40:42 +02:00
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
$this->assertEquals('null|string|int', (string) $context->vars_in_scope['$b']);
|
2016-10-03 17:40:42 +02:00
|
|
|
}
|
2016-12-17 00:56:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage MixedPropertyFetch
|
|
|
|
*/
|
|
|
|
public function testMixedPropertyFetch()
|
|
|
|
{
|
|
|
|
$filter = new Config\FileFilter(false);
|
2016-12-29 16:24:10 +01:00
|
|
|
$filter->addFile('somefile.php');
|
2016-12-17 00:56:23 +01:00
|
|
|
Config::getInstance()->setIssueHandler('MissingPropertyType', $filter);
|
|
|
|
Config::getInstance()->setIssueHandler('MixedAssignment', $filter);
|
|
|
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class Foo {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var mixed */
|
|
|
|
$a = (new Foo());
|
|
|
|
|
|
|
|
echo $a->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);
|
2016-12-29 16:24:10 +01:00
|
|
|
$filter->addFile('somefile.php');
|
2016-12-17 00:56:23 +01:00
|
|
|
Config::getInstance()->setIssueHandler('MissingPropertyType', $filter);
|
|
|
|
Config::getInstance()->setIssueHandler('MixedAssignment', $filter);
|
|
|
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class Foo {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var mixed */
|
|
|
|
$a = (new Foo());
|
|
|
|
|
|
|
|
$a->foo = "hello";
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
2016-12-17 04:16:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage NullPropertyAssignment
|
|
|
|
*/
|
|
|
|
public function testNullablePropertyAssignment()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class Foo {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? new Foo() : null;
|
|
|
|
|
|
|
|
$a->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('<?php
|
|
|
|
class Foo {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? new Foo() : null;
|
|
|
|
|
|
|
|
echo $a->foo;
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
2016-12-24 12:03:55 +01:00
|
|
|
|
|
|
|
public function testReflectionProperties()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class Foo {
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new \ReflectionMethod("Foo", "__construct");
|
|
|
|
|
|
|
|
echo $a->name . " - " . $a->class;
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
2016-07-12 06:53:36 +02:00
|
|
|
}
|