2017-01-12 03:37:53 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class MethodMutationTest extends TestCase
|
2017-01-12 03:37:53 +01:00
|
|
|
{
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-12 03:37:53 +01:00
|
|
|
public function testControllerMutation()
|
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
2017-04-25 05:45:02 +02:00
|
|
|
'somefile.php',
|
2017-01-12 03:37:53 +01:00
|
|
|
'<?php
|
|
|
|
class User {
|
|
|
|
/** @var string */
|
|
|
|
public $name;
|
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
protected function __construct($name) {
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
/** @return User|null */
|
|
|
|
public static function loadUser(int $id) {
|
|
|
|
if ($id === 3) {
|
2017-01-27 07:23:12 +01:00
|
|
|
$user = new User("bob");
|
2017-01-12 03:37:53 +01:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UserViewData {
|
|
|
|
/** @var string|null */
|
|
|
|
public $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Response {
|
|
|
|
public function __construct (UserViewData $viewdata) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UnauthorizedException extends Exception { }
|
|
|
|
|
|
|
|
class Controller {
|
|
|
|
/** @var UserViewData */
|
|
|
|
public $user_viewdata;
|
|
|
|
|
|
|
|
/** @var string|null */
|
|
|
|
public $title;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->user_viewdata = new UserViewData();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUser() : void
|
|
|
|
{
|
|
|
|
$user_id = (int)$_GET["id"];
|
|
|
|
|
|
|
|
if (!$user_id) {
|
|
|
|
throw new UnauthorizedException("No user id supplied");
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::loadUser($user_id);
|
|
|
|
|
|
|
|
if (!$user) {
|
|
|
|
throw new UnauthorizedException("User not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->user_viewdata->name = $user->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FooController extends Controller {
|
|
|
|
public function barBar() : Response {
|
|
|
|
$this->setUser();
|
|
|
|
|
2017-01-19 05:19:36 +01:00
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->title = "hello";
|
|
|
|
}
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
return new Response($this->user_viewdata);
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
2017-11-09 05:27:51 +01:00
|
|
|
new FileChecker('somefile.php', $this->project_checker);
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->project_checker->scanFiles();
|
2017-01-17 00:33:04 +01:00
|
|
|
$method_context = new Context();
|
2017-01-12 06:54:41 +01:00
|
|
|
$this->project_checker->getMethodMutations('FooController::barBar', $method_context);
|
2017-01-12 03:37:53 +01:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame('UserViewData', (string)$method_context->vars_in_scope['$this->user_viewdata']);
|
|
|
|
$this->assertSame('string', (string)$method_context->vars_in_scope['$this->user_viewdata->name']);
|
|
|
|
$this->assertSame(true, $method_context->vars_possibly_in_scope['$this->title']);
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
2017-05-13 01:15:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testParentControllerSet()
|
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
2017-05-13 01:15:08 +02:00
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class Foo { }
|
|
|
|
|
|
|
|
class Controller {
|
|
|
|
/** @var Foo|null */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->foo = new Foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FooController extends Controller {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
2017-11-09 05:27:51 +01:00
|
|
|
new FileChecker('somefile.php', $this->project_checker);
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->project_checker->scanFiles();
|
2017-05-13 01:15:08 +02:00
|
|
|
$method_context = new Context();
|
|
|
|
$this->project_checker->getMethodMutations('FooController::__construct', $method_context);
|
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame('Foo', (string)$method_context->vars_in_scope['$this->foo']);
|
2017-05-13 01:15:08 +02:00
|
|
|
}
|
2017-06-13 04:51:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTraitMethod()
|
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
2017-06-13 04:51:39 +02:00
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class Foo { }
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
private function setFoo() : void {
|
|
|
|
$this->foo = new Foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FooController {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
/** @var Foo|null */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->setFoo();
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
2017-11-09 05:27:51 +01:00
|
|
|
new FileChecker('somefile.php', $this->project_checker);
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->project_checker->scanFiles();
|
2017-06-13 04:51:39 +02:00
|
|
|
$method_context = new Context();
|
|
|
|
$this->project_checker->getMethodMutations('FooController::__construct', $method_context);
|
|
|
|
|
|
|
|
$this->assertSame('Foo', (string)$method_context->vars_in_scope['$this->foo']);
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|