1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/TypeTest.php

1689 lines
40 KiB
PHP
Raw Normal View History

2016-04-04 01:47:06 +02:00
<?php
2016-07-26 00:37:44 +02:00
namespace Psalm\Tests;
2016-04-04 01:47:06 +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-04-04 01:47:06 +02:00
class TypeTest extends PHPUnit_Framework_TestCase
{
/** @var \PhpParser\Parser */
2016-11-02 07:29:00 +01:00
protected static $parser;
2016-04-04 06:17:19 +02:00
public static function setUpBeforeClass()
{
2016-11-02 07:29:00 +01:00
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
2016-12-14 18:28:38 +01:00
$config = new TestConfig();
$config->throw_exception = true;
2016-04-04 06:17:19 +02:00
}
2016-06-20 07:05:44 +02:00
public function setUp()
{
2016-11-02 07:29:00 +01:00
FileChecker::clearCache();
2016-06-20 07:05:44 +02:00
}
2016-04-04 01:47:06 +02:00
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-04-04 01:47:06 +02:00
*/
public function testNullableMethodCall()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 01:47:06 +02:00
class A {
public function foo() {}
}
class B {
public function bar(A $a = null) {
$a->foo();
}
2016-04-04 06:17:19 +02:00
}');
2016-04-04 01:47:06 +02:00
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 01:47:06 +02:00
$file_checker->check();
}
2016-04-04 02:14:19 +02:00
public function testNullableMethodWithTernaryGuard()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 02:14:19 +02:00
class A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 02:14:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 02:14:19 +02:00
public function bar(A $a = null) {
$b = $a ? $a->foo() : null;
}
2016-04-04 06:17:19 +02:00
}');
2016-04-04 02:14:19 +02:00
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 02:14:19 +02:00
$file_checker->check();
}
public function testNullableMethodWithTernaryIfNullGuard()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 02:14:19 +02:00
class A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 02:14:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 02:14:19 +02:00
public function bar(A $a = null) {
$b = $a === null ? null : $a->foo();
}
2016-04-04 06:17:19 +02:00
}');
2016-04-04 02:14:19 +02:00
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 02:14:19 +02:00
$file_checker->check();
}
2016-04-04 22:33:26 +02:00
public function testNullableMethodWithTernaryEmptyGuard()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 22:33:26 +02:00
class A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 22:33:26 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 22:33:26 +02:00
public function bar(A $a = null) {
$b = empty($a) ? null : $a->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 22:33:26 +02:00
$file_checker->check();
}
public function testNullableMethodWithTernaryIsNullGuard()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 22:33:26 +02:00
class A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 22:33:26 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 22:33:26 +02:00
public function bar(A $a = null) {
$b = is_null($a) ? null : $a->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 22:33:26 +02:00
$file_checker->check();
}
2016-04-04 02:14:19 +02:00
public function testNullableMethodWithIfGuard()
2016-04-04 01:47:06 +02:00
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 01:47:06 +02:00
class A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 01:47:06 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 01:47:06 +02:00
public function bar(A $a = null) {
if ($a) {
$a->foo();
}
}
2016-04-04 06:17:19 +02:00
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 06:17:19 +02:00
$file_checker->check();
}
2016-04-04 07:21:28 +02:00
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-04-04 07:21:28 +02:00
*/
public function testNullableMethodCallWithThis()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 07:21:28 +02:00
class A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function bar(A $a = null) {
$this->a = $a;
$this->a->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 07:21:28 +02:00
$file_checker->check();
}
public function testNullableMethodWithTernaryGuardWithThis()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 07:21:28 +02:00
class A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function foo() {}
}
class B {
2016-11-01 19:32:19 +01:00
/** @var A|null */
2016-10-15 19:10:05 +02:00
public $a;
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function bar(A $a = null) {
$this->a = $a;
$b = $this->a ? $this->a->foo() : null;
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 07:21:28 +02:00
$file_checker->check();
}
public function testNullableMethodWithTernaryIfNullGuardWithThis()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 07:21:28 +02:00
class A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function foo() {}
}
class B {
2016-11-01 19:32:19 +01:00
/** @var A|null */
2016-10-15 19:10:05 +02:00
public $a;
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function bar(A $a = null) {
$this->a = $a;
$b = $this->a === null ? null : $this->a->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 07:21:28 +02:00
$file_checker->check();
}
public function testNullableMethodWithIfGuardWithThis()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 07:21:28 +02:00
class A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function foo() {}
}
class B {
2016-11-01 19:32:19 +01:00
/** @var A|null */
2016-10-15 19:10:05 +02:00
public $a;
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function bar(A $a = null) {
$this->a = $a;
if ($this->a) {
$this->a->foo();
}
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 07:21:28 +02:00
$file_checker->check();
}
2016-04-04 06:17:19 +02:00
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-04-04 06:17:19 +02:00
*/
public function testNullableMethodWithWrongIfGuard()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 06:17:19 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function bar(One $one = null, Two $two = null) {
if ($one) {
$two->foo();
}
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 06:17:19 +02:00
$file_checker->check();
}
public function testNullableMethodWithExceptionThrown()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one = null) {
if (!$one) {
throw new Exception();
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();
}
public function testNullableMethodWithRedefinitionAndElse()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
class One {
2016-11-01 19:32:19 +01:00
/** @var int|null */
2016-10-15 19:10:05 +02:00
public $two;
2016-11-11 23:13:24 +01:00
/** @return void */
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one = null) {
if (!$one) {
$one = new One();
}
else {
$one->two = 3;
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();
}
2016-04-04 06:17:19 +02:00
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-04-04 06:17:19 +02:00
*/
public function testNullableMethodWithWrongBooleanIfGuard()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 06:17:19 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function bar(One $one = null, Two $two = null) {
if ($one || $two) {
$two->foo();
}
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 06:17:19 +02:00
$file_checker->check();
}
public function testNullableMethodWithBooleanIfGuard()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 06:17:19 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function bar(One $one = null, Two $two = null) {
if ($one && $two) {
$two->foo();
}
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 06:17:19 +02:00
$file_checker->check();
}
2016-04-04 07:21:28 +02:00
public function testNullableMethodWithNonNullBooleanIfGuard()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 07:21:28 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function bar(One $one = null, Two $two = null) {
if ($one !== null && $two) {
$one->foo();
}
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 07:21:28 +02:00
$file_checker->check();
}
public function testNullableMethodWithNonNullBooleanIfGuardAndBooleanAnd()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 07:21:28 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function bar(One $one = null, Two $two = null) {
if ($one !== null && ($two || 1 + 1 === 3)) {
$one->foo();
}
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 07:21:28 +02:00
$file_checker->check();
}
2016-04-04 22:33:26 +02:00
public function testNullableMethodInConditionWithIfGuardBefore()
2016-04-04 06:17:19 +02:00
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 06:17:19 +02:00
class One {
2016-11-01 19:32:19 +01:00
/** @var string */
2016-04-04 22:33:26 +02:00
public $a;
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function bar(One $one = null, Two $two = null) {
if ($one === null) {
return;
}
2016-04-04 22:33:26 +02:00
if (!$one->a && $one->foo()) {
// do something
}
2016-04-04 06:17:19 +02:00
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 06:17:19 +02:00
$file_checker->check();
}
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-04-04 06:17:19 +02:00
*/
public function testNullableMethodWithWrongIfGuardBefore()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 06:17:19 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function bar(One $one = null, Two $two = null) {
if ($two === null) {
return;
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 06:17:19 +02:00
$file_checker->check();
}
public function testNullableMethodWithBooleanIfGuardBefore()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 06:17:19 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function bar(One $one = null, Two $two = null) {
if ($one === null || $two === null) {
return;
}
$one->foo();
}
}');
2016-04-04 01:47:06 +02:00
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 06:17:19 +02:00
$file_checker->check();
}
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-04-04 06:17:19 +02:00
*/
public function testNullableMethodWithWrongBooleanIfGuardBefore()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 06:17:19 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function bar(One $one = null, Two $two = null) {
if ($one === null && $two === null) {
return;
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 06:17:19 +02:00
$file_checker->check();
}
public function testNullableMethodWithGuardedRedefinition()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 06:17:19 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one = null) {
2016-04-04 06:17:19 +02:00
if ($one === null) {
$one = new One();
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 06:17:19 +02:00
$file_checker->check();
}
2016-06-16 07:19:52 +02:00
public function testNullableMethodWithGuardedRedefinitionInElse()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-16 07:19:52 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-16 07:19:52 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one = null) {
2016-06-16 07:19:52 +02:00
if ($one) {
// do nothing
}
else {
$one = new One();
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-16 07:19:52 +02:00
$file_checker->check();
}
2016-06-20 07:05:44 +02:00
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-06-20 07:05:44 +02:00
*/
public function testMethodWithMeaninglessCheck()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one) {
if (!$one) {
// do nothing
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();
}
2016-04-04 06:17:19 +02:00
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-04-04 06:17:19 +02:00
*/
public function testNullableMethodWithGuardedNestedIncompleteRedefinition()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 06:17:19 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function bar(One $one = null, Two $two = null) {
$a = 4;
if ($one === null) {
if ($a === 4) {
$one = new One();
}
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 06:17:19 +02:00
$file_checker->check();
}
public function testNullableMethodWithGuardedNestedRedefinition()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 06:17:19 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one = null) {
2016-04-04 06:17:19 +02:00
$a = 4;
if ($one === null) {
if ($a === 4) {
$one = new One();
}
else {
$one = new One();
}
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 06:17:19 +02:00
$file_checker->check();
}
public function testNullableMethodWithGuardedSwitchRedefinition()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one = null) {
$a = 4;
if ($one === null) {
switch ($a) {
case 4:
$one = new One();
break;
default:
$one = new One();
break;
}
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();
}
2016-06-30 00:15:51 +02:00
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-06-30 00:15:51 +02:00
*/
public function testNullableMethodWithGuardedSwitchRedefinitionNoDefault()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-30 00:15:51 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-30 00:15:51 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-30 00:15:51 +02:00
public function bar(One $one = null) {
$a = 4;
if ($one === null) {
switch ($a) {
case 4:
$one = new One();
break;
}
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-30 00:15:51 +02:00
$file_checker->check();
}
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-06-30 00:15:51 +02:00
*/
public function testNullableMethodWithGuardedSwitchRedefinitionEmptyDefault()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-30 00:15:51 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-30 00:15:51 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-30 00:15:51 +02:00
public function bar(One $one = null) {
$a = 4;
if ($one === null) {
switch ($a) {
case 4:
$one = new One();
break;
default:
break;
}
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-30 00:15:51 +02:00
$file_checker->check();
}
public function testNullableMethodWithGuardedSwitchRedefinitionDueToException()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-30 00:15:51 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-30 00:15:51 +02:00
public function foo() {}
}
class B {
2016-10-15 19:10:05 +02:00
/**
2016-11-11 23:13:24 +01:00
* @return void
2016-10-15 19:10:05 +02:00
*/
2016-06-30 00:15:51 +02:00
public function bar(One $one = null) {
$a = 4;
if ($one === null) {
switch ($a) {
case 4:
$one = new One();
break;
default:
throw new \Exception("bad");
}
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-30 00:15:51 +02:00
$file_checker->check();
}
2016-07-08 00:10:01 +02:00
public function testNullableMethodWithGuardedSwitchThatAlwaysReturns()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-07-08 00:10:01 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-07-08 00:10:01 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-07-08 00:10:01 +02:00
public function bar(One $one = null) {
$a = 4;
if ($one === null) {
switch ($a) {
case 4:
return;
default:
return;
}
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-07-08 00:10:01 +02:00
$file_checker->check();
}
2016-04-04 06:17:19 +02:00
public function testNullableMethodWithGuardedNestedRedefinitionWithReturn()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 06:17:19 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one = null) {
2016-04-04 06:17:19 +02:00
$a = 4;
if ($one === null) {
if ($a === 4) {
$one = new One();
return;
}
else {
$one = new One();
}
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 06:17:19 +02:00
$file_checker->check();
}
2016-04-04 22:33:26 +02:00
public function testNullableMethodWithGuardedNestedRedefinitionWithElseReturn()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 22:33:26 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 22:33:26 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one = null) {
2016-04-04 22:33:26 +02:00
$a = 4;
if ($one === null) {
if ($a === 4) {
$one = new One();
}
else {
$one = new One();
return;
}
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 22:33:26 +02:00
$file_checker->check();
}
2016-04-04 06:17:19 +02:00
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-04-04 06:17:19 +02:00
*/
2016-04-04 22:33:26 +02:00
public function testNullableMethodWithGuardedNestedRedefinitionWithUselessElseReturn()
2016-04-04 06:17:19 +02:00
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 06:17:19 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 06:17:19 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one = null) {
2016-04-04 06:17:19 +02:00
$a = 4;
if ($one === null) {
if ($a === 4) {
$one = new One();
}
2016-04-04 22:33:26 +02:00
else if ($a === 3) {
// do nothing
}
2016-04-04 06:17:19 +02:00
else {
$one = new One();
return;
}
}
$one->foo();
}
}');
2016-04-04 01:47:06 +02:00
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 01:47:06 +02:00
$file_checker->check();
}
2016-04-04 07:21:28 +02:00
2016-04-04 22:33:26 +02:00
public function testNullableMethodWithGuardedNestedRedefinitionWithElseifReturn()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 22:33:26 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 22:33:26 +02:00
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one = null) {
2016-04-04 22:33:26 +02:00
$a = 4;
if ($one === null) {
if ($a === 4) {
$one = new One();
}
else if ($a === 3) {
// do nothing
return;
}
else {
$one = new One();
}
}
$one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 22:33:26 +02:00
$file_checker->check();
}
public function testNullableMethodWithGuardedSwitchBreak()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
public function foo() {}
}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one = null) {
$a = 4;
switch ($a) {
case 4:
if ($one === null) {
break;
}
$one->foo();
break;
}
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
$file_checker->check();
}
2016-04-04 07:21:28 +02:00
public function testNullableMethodWithGuardedRedefinitionOnThis()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-04 07:21:28 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-04 07:21:28 +02:00
public function foo() {}
}
class B {
2016-11-01 19:32:19 +01:00
/** @var One|null */
2016-10-15 19:10:05 +02:00
public $one;
2016-11-11 23:13:24 +01:00
/** @return void */
public function bar(One $one = null) {
2016-04-04 07:21:28 +02:00
$this->one = $one;
if ($this->one === null) {
$this->one = new One();
}
$this->one->foo();
}
}');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-04 07:21:28 +02:00
$file_checker->check();
}
public function testArrayUnionTypeAssertion()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
/** @var array|null */
$ids = (1 + 1 === 2) ? [] : null;
if ($ids === null) {
$ids = [];
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('array<empty, empty>', (string) $context->vars_in_scope['$ids']);
}
public function testArrayUnionTypeAssertionWithIsArray()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
/** @var array|null */
$ids = (1 + 1 === 2) ? [] : null;
if (!is_array($ids)) {
$ids = [];
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('array<empty, empty>', (string) $context->vars_in_scope['$ids']);
}
2016-04-12 22:12:38 +02:00
public function testVariableReassignment()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-12 22:12:38 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-12 22:12:38 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-12 22:12:38 +02:00
public function bar() {}
}
$one = new One();
$one = new Two();
$one->bar();
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-12 22:12:38 +02:00
$file_checker->check();
}
public function testVariableReassignmentInIf()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-12 22:12:38 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-12 22:12:38 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-12 22:12:38 +02:00
public function bar() {}
}
$one = new One();
if (1 + 1 === 2) {
$one = new Two();
$one->bar();
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-12 22:12:38 +02:00
$file_checker->check();
}
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-04-12 22:12:38 +02:00
*/
public function testVariableReassignmentInIfWithOutsideCall()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-12 22:12:38 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-12 22:12:38 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-12 22:12:38 +02:00
public function bar() {}
}
$one = new One();
if (1 + 1 === 2) {
$one = new Two();
$one->bar();
}
$one->bar();
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-12 22:12:38 +02:00
$file_checker->check();
}
public function testUnionTypeFlow()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-04-12 22:12:38 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-12 22:12:38 +02:00
public function foo() {}
}
class Two {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-12 22:12:38 +02:00
public function bar() {}
}
class Three {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-04-12 22:12:38 +02:00
public function baz() {}
}
/** @var One|Two|Three|null */
$var = null;
if ($var instanceof One) {
$var->foo();
}
else {
if ($var instanceof Two) {
$var->bar();
}
else if ($var) {
$var->baz();
}
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-04-12 22:12:38 +02:00
$file_checker->check();
}
2016-08-23 06:35:22 +02:00
public function testUnionTypeFlowWithThrow()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-08-23 06:35:22 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-08-23 06:35:22 +02:00
public function foo() {}
}
2016-11-11 23:13:24 +01:00
/** @return void */
2016-10-15 19:10:05 +02:00
function a(One $var = null) {
if (!$var) {
throw new \Exception("some exception");
}
else {
$var->foo();
}
2016-08-23 06:35:22 +02:00
}
2016-10-15 19:10:05 +02:00
2016-08-23 06:35:22 +02:00
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-08-23 06:35:22 +02:00
$file_checker->check();
}
public function testUnionTypeFlowWithElseif()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-08-23 06:35:22 +02:00
class One {
2016-12-07 20:13:39 +01:00
/** @return void */
2016-08-23 06:35:22 +02:00
public function foo() {}
}
/** @var One|null */
$var = null;
if (rand(0,100) === 5) {
}
elseif (!$var) {
}
else {
$var->foo();
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-08-23 06:35:22 +02:00
$file_checker->check();
}
2016-08-23 05:02:17 +02:00
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-08-23 05:02:17 +02:00
*/
2016-06-13 07:48:29 +02:00
public function testUnnecessaryInstanceof()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-13 07:48:29 +02:00
class One {
public function foo() {}
}
$var = new One();
if ($var instanceof One) {
$var->foo();
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-13 07:48:29 +02:00
$file_checker->check();
}
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-06-13 07:48:29 +02:00
*/
public function testUnNegatableInstanceof()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-13 07:48:29 +02:00
class One {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-13 07:48:29 +02:00
public function foo() {}
}
$var = new One();
if ($var instanceof One) {
$var->foo();
}
else {
// do something
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-13 07:48:29 +02:00
$file_checker->check();
}
public function testTypeAdjustment()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-20 07:05:44 +02:00
$var = 0;
2016-06-20 07:05:44 +02:00
if (5 + 3 === 8) {
$var = "hello";
}
2016-06-20 07:05:44 +02:00
echo $var;
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('int|string', (string) $context->vars_in_scope['$var']);
2016-04-30 20:14:22 +02:00
}
public function testTypeMixedAdjustment()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-20 07:05:44 +02:00
$var = 0;
2016-04-30 20:14:22 +02:00
2016-06-20 07:05:44 +02:00
$arr = ["hello"];
2016-04-30 20:14:22 +02:00
2016-06-20 07:05:44 +02:00
if (5 + 3 === 8) {
$var = $arr[0];
2016-04-30 20:14:22 +02:00
}
2016-06-20 07:05:44 +02:00
echo $var;
2016-04-30 20:14:22 +02:00
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('int|string', (string) $context->vars_in_scope['$var']);
2016-06-20 07:05:44 +02:00
}
public function testTypeAdjustmentIfNull()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-20 07:05:44 +02:00
class A {}
class B {}
$var = rand(0,10) > 5 ? new A : null;
2016-04-30 20:14:22 +02:00
2016-06-20 07:05:44 +02:00
if ($var === null) {
$var = new B;
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('A|B', (string) $context->vars_in_scope['$var']);
}
2016-06-17 22:05:28 +02:00
public function testWhileTrue()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-17 22:05:28 +02:00
class One {
/**
* @return array|false
*/
public function foo(){
return rand(0,100) ? ["hello"] : false;
}
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-17 22:05:28 +02:00
public function bar(){
while ($row = $this->foo()) {
$row[0] = "bad";
}
}
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-17 22:05:28 +02:00
$file_checker->check();
}
2016-06-20 07:05:44 +02:00
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-06-20 07:05:44 +02:00
*/
public function testWrongParam()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-20 07:05:44 +02:00
class A {}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-20 07:05:44 +02:00
public function bar(A $a) {}
}
$b = new B();
$b->bar(5);
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-20 07:05:44 +02:00
$file_checker->check();
}
public function testPassingParam()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-20 07:05:44 +02:00
class A {}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-20 07:05:44 +02:00
public function bar(A $a) {}
}
$b = new B();
$b->bar(new A);
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-20 07:05:44 +02:00
$file_checker->check();
}
public function testNullToNullableParam()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-20 07:05:44 +02:00
class A {}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-20 07:05:44 +02:00
public function bar(A $a = null) {}
}
$b = new B();
$b->bar(null);
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-20 07:05:44 +02:00
$file_checker->check();
}
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-06-20 07:05:44 +02:00
*/
public function testIntToNullableObjectParam()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-20 07:05:44 +02:00
class A {}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-20 07:05:44 +02:00
public function bar(A $a = null) {}
}
$b = new B();
$b->bar(5);
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-20 07:05:44 +02:00
$file_checker->check();
}
public function testObjectToNullableObjectParam()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-20 07:05:44 +02:00
class A {}
class B {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-20 07:05:44 +02:00
public function bar(A $a = null) {}
}
$b = new B();
$b->bar(new A);
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-20 07:05:44 +02:00
$file_checker->check();
}
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-06-20 07:05:44 +02:00
*/
public function testParamCoercionWithBadArg()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-20 07:05:44 +02:00
class A {}
class B extends A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-20 07:05:44 +02:00
public function blab() {}
}
class C {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-20 07:05:44 +02:00
function foo(A $a) {
if ($a instanceof B) {
$a->bar();
}
}
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-20 07:05:44 +02:00
$file_checker->check();
}
public function testParamCoercion()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-20 07:05:44 +02:00
class A {}
class B extends A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-20 07:05:44 +02:00
public function bar() {}
}
class C {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-20 07:05:44 +02:00
function foo(A $a) {
if ($a instanceof B) {
$a->bar();
}
}
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-20 07:05:44 +02:00
$file_checker->check();
}
public function testParamElseifCoercion()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-06-20 07:05:44 +02:00
class A {}
class B extends A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-20 07:05:44 +02:00
public function bar() {}
}
class C extends A {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-20 07:05:44 +02:00
public function baz() {}
}
class D {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-06-20 07:05:44 +02:00
function foo(A $a) {
if ($a instanceof B) {
$a->bar();
}
elseif ($a instanceof C) {
$a->baz();
}
}
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-06-20 07:05:44 +02:00
$file_checker->check();
}
2016-08-23 06:35:22 +02:00
public function testAssignInsideForeach()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-08-23 06:35:22 +02:00
$b = false;
foreach ([1, 2, 3, 4] as $a) {
if ($a === rand(0, 10)) {
$b = true;
}
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-08-23 06:35:22 +02:00
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
2016-10-15 19:10:05 +02:00
$this->assertSame('bool', (string) $context->vars_in_scope['$b']);
2016-08-23 06:35:22 +02:00
}
public function testAssignInsideForeachWithBreak()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-08-23 06:35:22 +02:00
$b = false;
foreach ([1, 2, 3, 4] as $a) {
if ($a === rand(0, 10)) {
$b = true;
break;
}
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-08-23 06:35:22 +02:00
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
2016-10-15 19:10:05 +02:00
$this->assertSame('bool', (string) $context->vars_in_scope['$b']);
2016-08-23 06:35:22 +02:00
}
2016-08-24 06:30:55 +02:00
/**
2016-11-02 07:29:00 +01:00
* @expectedException \Psalm\Exception\CodeException
2016-08-24 06:30:55 +02:00
* @expectedExceptionMessage NullReference
*/
public function testNullCheckInsideForeachWithNoLeaveStatement()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-08-24 06:30:55 +02:00
class A {
/** @return array<A|null> */
public static function loadMultiple()
{
return [new A, null];
}
2016-11-11 23:13:24 +01:00
/** @return void */
2016-08-24 06:30:55 +02:00
public function bar() {
}
}
foreach (A::loadMultiple() as $a) {
if ($a === null) {
// do nothing
}
$a->bar();
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-08-24 06:30:55 +02:00
$context = new Context('somefile.php');
$file_checker->check();
}
public function testNullCheckInsideForeachWithContinue()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-08-24 06:30:55 +02:00
class A {
/** @return array<A|null> */
public static function loadMultiple()
{
return [new A, null];
}
2016-11-11 23:13:24 +01:00
/** @return void */
2016-08-24 06:30:55 +02:00
public function bar() {
}
}
foreach (A::loadMultiple() as $a) {
if ($a === null) {
continue;
}
$a->bar();
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-08-24 06:30:55 +02:00
$context = new Context('somefile.php');
$file_checker->check();
}
public function testTypeRefinementWithIsNumeric()
{
$stmts = self::$parser->parse('<?php
function foo(string $a) : void {
if (is_numeric($a)) {
}
}
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check();
}
2016-12-17 06:48:31 +01:00
public function testPlusPlus()
{
$stmts = self::$parser->parse('<?php
$a = 0;
$b = $a++;
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertSame('int', (string) $context->vars_in_scope['$a']);
}
2016-04-04 01:47:06 +02:00
}