2016-12-06 19:49:57 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Checker\TypeChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Type;
|
|
|
|
|
|
|
|
class SwitchTypeTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-12-14 18:55:23 +01:00
|
|
|
/** @var \PhpParser\Parser */
|
2016-12-06 19:49:57 +01:00
|
|
|
protected static $parser;
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-06 19:49:57 +01:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
|
2016-12-14 18:28:38 +01:00
|
|
|
$config = new TestConfig();
|
2016-12-06 19:49:57 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-06 19:49:57 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
FileChecker::clearCache();
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
2016-12-06 19:49:57 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-06 19:49:57 +01:00
|
|
|
public function testGetClassArg()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
2016-12-07 20:13:39 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {
|
2016-12-06 19:49:57 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-12-07 20:13:39 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar() {
|
2016-12-06 19:49:57 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? new A() : new B();
|
|
|
|
|
|
|
|
switch (get_class($a)) {
|
|
|
|
case "A":
|
2016-12-30 19:09:00 +01:00
|
|
|
$a->fooFoo();
|
2016-12-06 19:49:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "B":
|
2016-12-30 19:09:00 +01:00
|
|
|
$a->barBar();
|
2016-12-06 19:49:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-06 19:49:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-06 19:49:57 +01:00
|
|
|
* @expectedExceptionMessage UndefinedMethod
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-06 19:49:57 +01:00
|
|
|
*/
|
|
|
|
public function testGetClassArgWrongClass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
2016-12-07 20:13:39 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() {
|
2016-12-06 19:49:57 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-12-07 20:13:39 +01:00
|
|
|
/** @return void */
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar() {
|
2016-12-06 19:49:57 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? new A() : new B();
|
|
|
|
|
|
|
|
switch (get_class($a)) {
|
|
|
|
case "A":
|
2016-12-30 19:09:00 +01:00
|
|
|
$a->barBar();
|
2016-12-06 19:49:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-06 19:49:57 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-06 19:49:57 +01:00
|
|
|
public function testGetTypeArg()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function testInt(int $var) : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testString(string $var) : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? 1 : "two";
|
|
|
|
|
|
|
|
switch (gettype($a)) {
|
|
|
|
case "string":
|
|
|
|
testString($a);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "int":
|
|
|
|
testInt($a);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-06 19:49:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-06 19:49:57 +01:00
|
|
|
* @expectedExceptionMessage InvalidScalarArgument
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-06 19:49:57 +01:00
|
|
|
*/
|
|
|
|
public function testGetTypeArgWrongArgs()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function testInt(int $var) : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testString(string $var) : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? 1 : "two";
|
|
|
|
|
|
|
|
switch (gettype($a)) {
|
|
|
|
case "string":
|
|
|
|
testInt($a);
|
|
|
|
|
|
|
|
case "int":
|
|
|
|
testString($a);
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-06 19:49:57 +01:00
|
|
|
}
|
|
|
|
}
|