1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00
psalm/tests/PropertyTypeTest.php

996 lines
26 KiB
PHP
Raw Normal View History

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
{
/** @var \PhpParser\Parser */
2016-11-02 07:29:00 +01:00
protected static $parser;
/** @var \Psalm\Checker\ProjectChecker */
protected $project_checker;
2016-07-12 06:53:36 +02:00
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
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
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-07-12 06:53:36 +02:00
public function setUp()
{
2016-11-02 07:29:00 +01:00
FileChecker::clearCache();
$this->project_checker = new \Psalm\Checker\ProjectChecker();
$this->project_checker->setConfig(new TestConfig());
2016-07-12 06:53:36 +02:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
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-12-30 19:09:00 +01:00
public function barBar()
2016-07-12 06:53:36 +02:00
{
if (rand(0,10) === 5) {
$this->foo = [];
}
if (!is_array($this->foo)) {
// do something
}
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-07-12 06:53:36 +02:00
}
2016-10-03 17:40:42 +02:00
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2016-12-15 01:24:33 +01:00
public function testPropertyWithoutTypeSuppressingIssue()
{
2016-12-30 02:07:42 +01:00
Config::getInstance()->setCustomErrorLevel('MissingPropertyType', Config::REPORT_SUPPRESS);
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
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', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-15 01:24:33 +01:00
}
2016-12-17 04:16:29 +01:00
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-17 04:16:29 +01:00
* @expectedExceptionMessage UndefinedPropertyAssignment
2017-01-13 20:07:23 +01:00
* @return void
2016-12-17 04:16:29 +01:00
*/
public function testUndefinedPropertyAssignment()
{
$stmts = self::$parser->parse('<?php
class A {
}
(new A)->foo = "cool";
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-17 04:16:29 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-17 04:16:29 +01:00
* @expectedExceptionMessage UndefinedPropertyFetch
2017-01-13 20:07:23 +01:00
* @return void
2016-12-17 04:16:29 +01:00
*/
public function testUndefinedPropertyFetch()
{
$stmts = self::$parser->parse('<?php
class A {
}
echo (new A)->foo;
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-17 04:16:29 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-17 04:16:29 +01:00
* @expectedExceptionMessage UndefinedThisPropertyAssignment
2017-01-13 20:07:23 +01:00
* @return void
2016-12-17 04:16:29 +01:00
*/
public function testUndefinedThisPropertyAssignment()
{
$stmts = self::$parser->parse('<?php
class A {
2016-12-30 19:09:00 +01:00
public function fooFoo() : void {
2016-12-17 04:16:29 +01:00
$this->foo = "cool";
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-17 04:16:29 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-17 04:16:29 +01:00
* @expectedExceptionMessage UndefinedThisPropertyFetch
2017-01-13 20:07:23 +01:00
* @return void
2016-12-17 04:16:29 +01:00
*/
public function testUndefinedThisPropertyFetch()
{
$stmts = self::$parser->parse('<?php
class A {
2016-12-30 19:09:00 +01:00
public function fooFoo() : void {
2016-12-17 04:16:29 +01:00
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', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-17 04:16:29 +01:00
}
2016-12-15 01:24:33 +01:00
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-15 01:24:33 +01:00
* @expectedExceptionMessage MissingPropertyDeclaration
2017-01-13 20:07:23 +01:00
* @return void
2016-12-15 01:24:33 +01:00
*/
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', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-15 01:24:33 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-15 01:24:33 +01:00
* @expectedExceptionMessage MissingPropertyType
2017-01-13 20:07:23 +01:00
* @return void
2016-12-15 01:24:33 +01:00
*/
public function testMissingPropertyType()
{
$stmts = self::$parser->parse('<?php
class A {
public $foo;
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-15 01:24:33 +01:00
}
2016-12-12 05:41:11 +01:00
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-12 05:41:11 +01:00
* @expectedExceptionMessage InvalidPropertyAssignment
2017-01-13 20:07:23 +01:00
* @return void
2016-12-12 05:41:11 +01:00
*/
public function testBadAssignment()
{
$stmts = self::$parser->parse('<?php
class A {
/** @var string */
public $foo;
2016-12-30 19:09:00 +01:00
public function barBar() : void
2016-12-12 05:41:11 +01:00
{
$this->foo = 5;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-12 05:41:11 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-12 05:41:11 +01:00
* @expectedExceptionMessage InvalidPropertyAssignment
2017-01-13 20:07:23 +01:00
* @return void
2016-12-12 05:41:11 +01:00
*/
public function testBadAssignmentAsWell()
{
$stmts = self::$parser->parse('<?php
$a = "hello";
$a->foo = "bar";
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-12 05:41:11 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-12 05:41:11 +01:00
* @expectedExceptionMessage InvalidPropertyFetch
2017-01-13 20:07:23 +01:00
* @return void
2016-12-12 05:41:11 +01:00
*/
public function testBadFetch()
{
$stmts = self::$parser->parse('<?php
$a = "hello";
echo $a->foo;
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
2016-12-12 05:41:11 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
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 = 0;
2016-10-03 17:40:42 +02:00
}
class B {
/** @var string */
public $foo = "";
2016-10-03 17:40:42 +02: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;
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($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
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
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 = 0;
2016-10-03 17:40:42 +02:00
}
class B {
/** @var string */
public $foo = "";
2016-10-03 17:40:42 +02: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;
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($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
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage MixedPropertyFetch
2017-01-13 20:07:23 +01:00
* @return void
*/
public function testMixedPropertyFetch()
{
2016-12-30 02:07:42 +01:00
Config::getInstance()->setCustomErrorLevel('MissingPropertyType', Config::REPORT_SUPPRESS);
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
$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', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage MixedPropertyAssignment
2017-01-13 20:07:23 +01:00
* @return void
*/
public function testMixedPropertyAssignment()
{
2016-12-30 02:07:42 +01:00
Config::getInstance()->setCustomErrorLevel('MissingPropertyType', Config::REPORT_SUPPRESS);
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
$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', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
2016-12-17 04:16:29 +01:00
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-17 04:16:29 +01:00
* @expectedExceptionMessage NullPropertyAssignment
2017-01-13 20:07:23 +01:00
* @return void
2016-12-17 04:16:29 +01:00
*/
public function testNullablePropertyAssignment()
{
$stmts = self::$parser->parse('<?php
class Foo {
/** @var string */
public $foo = "";
2016-12-17 04:16:29 +01:00
}
$a = rand(0, 10) ? new Foo() : null;
$a->foo = "hello";
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-12-17 04:16:29 +01:00
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-12-17 04:16:29 +01:00
* @expectedExceptionMessage NullPropertyFetch
2017-01-13 20:07:23 +01:00
* @return void
2016-12-17 04:16:29 +01:00
*/
public function testNullablePropertyFetch()
{
$stmts = self::$parser->parse('<?php
class Foo {
/** @var string */
public $foo = "";
2016-12-17 04:16:29 +01:00
}
$a = rand(0, 10) ? new Foo() : null;
echo $a->foo;
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-12-17 04:16:29 +01:00
}
2016-12-24 12:03:55 +01:00
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testNullablePropertyCheck()
{
$stmts = self::$parser->parse('<?php
class A {
/** @var string */
public $aa = "";
}
class B {
/** @var A|null */
public $bb;
}
$b = rand(0, 10) ? new A() : new B();
if ($b instanceof B && isset($b->bb) && $b->bb->aa === "aa") {
echo $b->bb->aa;
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2017-01-07 20:35:07 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
2017-01-07 20:35:07 +01:00
public function testNullablePropertyAfterGuard()
{
$stmts = self::$parser->parse('<?php
class A {
/** @var string|null */
public $aa;
}
$a = new A();
if (!$a->aa) {
$a->aa = "hello";
}
echo substr($a->aa, 1);
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testNullableStaticPropertyWithIfCheck()
{
$stmts = self::$parser->parse('<?php
class A {
/** @var A|null */
public static $fooFoo;
public static function getFoo() : A {
if (!self::$fooFoo) {
self::$fooFoo = new A();
}
return self::$fooFoo;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
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', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
2016-12-24 12:03:55 +01:00
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testGrandparentReflectedProperties()
{
$stmts = self::$parser->parse('<?php
$a = new DOMElement("foo");
$owner = $a->ownerDocument;
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
$this->assertEquals('DOMDocument', (string) $context->vars_in_scope['$owner']);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testGoodArrayProperties()
{
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
$context = new Context();
$stmts = self::$parser->parse('<?php
interface I1 {}
class A1 implements I1{}
class B1 implements I1 {}
class C1 {
/** @var array<I1> */
public $is = [];
}
$c = new C1;
$c->is = [new A1];
$c->is = [new A1, new A1];
$c->is = [new A1, new B1];
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods($context);
}
/**
2017-01-13 20:07:23 +01:00
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidPropertyAssignment
2017-01-13 20:07:23 +01:00
* @return void
*/
public function testBadArrayProperty()
{
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
$context = new Context();
$stmts = self::$parser->parse('<?php
class A {}
class B {}
class C {
/** @var array<B> */
public $bb;
}
$c = new C;
$c->bb = [new A, new B];
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/
public function testIssetPropertyDoesNotExist()
{
$stmts = self::$parser->parse('<?php
class A {
}
$a = new A();
if (isset($a->bar)) {
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage PropertyNotSetInConstructor
* @return void
*/
public function testNotSetInEmptyConstructor()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
class A {
/** @var int */
public $a;
public function __construct() { }
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage MissingConstructor
* @return void
*/
public function testNoConstructor()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
class A {
/** @var int */
public $a;
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/
public function testNotSetInConstructorButHasDefault()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
class A {
/** @var int */
public $a = 0;
public function __construct() { }
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage PropertyNotSetInConstructor
* @return void
*/
public function testNotSetInAllBranchesOfIf()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
class A {
/** @var int */
public $a;
public function __construct() {
if (rand(0, 1)) {
$this->a = 5;
}
}
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/
public function testPropertySetInPrivateMethod()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
class A {
/** @var int */
public $a;
public function __construct() {
$this->foo();
}
private function foo() : void {
$this->a = 5;
}
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage PropertyNotSetInConstructor
* @return void
*/
public function testPropertySetInProtectedMethod()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
class A {
/** @var int */
public $a;
public function __construct() {
$this->foo();
}
protected function foo() : void {
$this->a = 5;
}
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage PropertyNotSetInConstructor
* @return void
*/
public function testDefinedInTraitNotSetInEmptyConstructor()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
trait A {
/** @var string **/
public $a;
}
class B {
use A;
public function __construct() {
}
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/
public function testDefinedInTraitSetInConstructor()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
trait A {
/** @var string **/
public $a;
}
class B {
use A;
public function __construct() {
$this->a = "hello";
}
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/
public function testPropertySetInNestedPrivateMethod()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
class A {
/** @var int */
public $a;
public function __construct() {
$this->foo();
}
private function foo() : void {
$this->bar();
}
private function bar() : void {
$this->a = 5;
}
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage PropertyNotSetInConstructor
* @return void
*/
public function testPropertySetInPrivateMethodWithIf()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
class A {
/** @var int */
public $a;
public function __construct() {
if (rand(0, 1)) {
$this->foo();
}
}
private function foo() : void {
$this->a = 5;
}
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage PropertyNotSetInConstructor
* @return void
*/
public function testPropertySetInPrivateMethodWithIfAndElse()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
class A {
/** @var int */
public $a;
public function __construct() {
if (rand(0, 1)) {
$this->foo();
} else {
$this->bar();
}
}
private function foo() : void {
$this->a = 5;
}
private function bar() : void {
$this->a = 5;
}
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/
public function testPropertyArrayIssetAssertion()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
function bar(string $s) : void { }
class A {
/** @var array<string, string> */
public $a = [];
private function foo() : void {
if (isset($this->a["hello"])) {
bar($this->a["hello"]);
}
}
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/
public function testPropertyArrayIssetAssertionWithVariableOffset()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
function bar(string $s) : void { }
class A {
/** @var array<string, string> */
public $a = [];
private function foo() : void {
$b = "hello";
if (!isset($this->a[$b])) {
return;
}
bar($this->a[$b]);
}
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/
public function testStaticPropertyArrayIssetAssertionWithVariableOffset()
{
$this->project_checker->registerFile(
getcwd() . '/somefile.php',
'<?php
function bar(string $s) : void { }
class A {
/** @var array<string, string> */
public static $a = [];
}
function foo() : void {
$b = "hello";
if (!isset(A::$a[$b])) {
return;
}
bar(A::$a[$b]);
}'
);
$file_checker = new FileChecker(getcwd() . '/somefile.php', $this->project_checker);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
2016-07-12 06:53:36 +02:00
}