2016-12-17 04:16:29 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class TraitTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/** @var \PhpParser\Parser */
|
|
|
|
protected static $parser;
|
|
|
|
|
2017-02-01 01:22:05 +01:00
|
|
|
/** @var TestConfig */
|
|
|
|
protected static $config;
|
|
|
|
|
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-17 04:16:29 +01:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2017-02-01 01:22:05 +01:00
|
|
|
self::$config = new TestConfig();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-17 04:16:29 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
FileChecker::clearCache();
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
2017-02-01 01:22:05 +01:00
|
|
|
$this->project_checker->setConfig(self::$config);
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-17 04:16:29 +01:00
|
|
|
public function testAccessiblePrivateMethodFromTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2017-01-02 01:09:17 +01:00
|
|
|
trait T {
|
2016-12-30 19:09:00 +01:00
|
|
|
private function fooFoo() : void {
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2017-01-02 01:09:17 +01:00
|
|
|
use T;
|
2016-12-17 04:16:29 +01:00
|
|
|
|
|
|
|
public function doFoo() : void {
|
2016-12-30 19:09:00 +01:00
|
|
|
$this->fooFoo();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-17 04:16:29 +01:00
|
|
|
public function testAccessibleProtectedMethodFromTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2017-01-02 01:09:17 +01:00
|
|
|
trait T {
|
2016-12-30 19:09:00 +01:00
|
|
|
protected function fooFoo() : void {
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2017-01-02 01:09:17 +01:00
|
|
|
use T;
|
2016-12-17 04:16:29 +01:00
|
|
|
|
|
|
|
public function doFoo() : void {
|
2016-12-30 19:09:00 +01:00
|
|
|
$this->fooFoo();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-17 04:16:29 +01:00
|
|
|
public function testAccessiblePublicMethodFromTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2017-01-02 01:09:17 +01:00
|
|
|
trait T {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() : void {
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2017-01-02 01:09:17 +01:00
|
|
|
use T;
|
2016-12-17 04:16:29 +01:00
|
|
|
|
|
|
|
public function doFoo() : void {
|
2016-12-30 19:09:00 +01:00
|
|
|
$this->fooFoo();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-07 20:35:07 +01:00
|
|
|
public function testAccessiblePrivatePropertyFromTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
trait T {
|
|
|
|
/** @var string */
|
2017-01-27 16:28:21 +01:00
|
|
|
private $fooFoo = "";
|
2017-01-07 20:35:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo $this->fooFoo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
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 testAccessibleProtectedPropertyFromTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
trait T {
|
|
|
|
/** @var string */
|
2017-01-27 16:28:21 +01:00
|
|
|
protected $fooFoo = "";
|
2017-01-07 20:35:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo $this->fooFoo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
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 testAccessiblePublicPropertyFromTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
trait T {
|
|
|
|
/** @var string */
|
2017-01-27 16:28:21 +01:00
|
|
|
public $fooFoo = "";
|
2017-01-07 20:35:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo $this->fooFoo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2017-01-07 20:35:07 +01:00
|
|
|
}
|
|
|
|
|
2016-12-17 04:16:29 +01:00
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-02 01:09:17 +01:00
|
|
|
* @expectedExceptionMessage InaccessibleMethod
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-17 04:16:29 +01:00
|
|
|
*/
|
|
|
|
public function testInccessiblePrivateMethodFromInheritedTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2017-01-02 01:09:17 +01:00
|
|
|
trait T {
|
2016-12-30 19:09:00 +01:00
|
|
|
private function fooFoo() : void {
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2017-01-02 01:09:17 +01:00
|
|
|
use T;
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public function doFoo() : void {
|
2016-12-30 19:09:00 +01:00
|
|
|
$this->fooFoo();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-17 04:16:29 +01:00
|
|
|
public function testAccessibleProtectedMethodFromInheritedTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2017-01-02 01:09:17 +01:00
|
|
|
trait T {
|
2016-12-30 19:09:00 +01:00
|
|
|
protected function fooFoo() : void {
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2017-01-02 01:09:17 +01:00
|
|
|
use T;
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public function doFoo() : void {
|
2016-12-30 19:09:00 +01:00
|
|
|
$this->fooFoo();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-17 04:16:29 +01:00
|
|
|
public function testAccessiblePublicMethodFromInheritedTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2017-01-02 01:09:17 +01:00
|
|
|
trait T {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() : void {
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2017-01-02 01:09:17 +01:00
|
|
|
use T;
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public function doFoo() : void {
|
2016-12-30 19:09:00 +01:00
|
|
|
$this->fooFoo();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-30 05:37:09 +01:00
|
|
|
public function testStaticClassMethodFromWithinTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2017-01-02 01:09:17 +01:00
|
|
|
trait T {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() : void {
|
|
|
|
self::barBar();
|
2016-12-30 05:37:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2017-01-02 01:09:17 +01:00
|
|
|
use T;
|
2016-12-30 05:37:09 +01:00
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
public static function barBar() : void {
|
2016-12-30 05:37:09 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-12-30 05:37:09 +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 UndefinedTrait
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-17 04:16:29 +01:00
|
|
|
*/
|
|
|
|
public function testUndefinedTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class B {
|
|
|
|
use A;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
2016-12-31 17:49:04 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-12 03:37:53 +01:00
|
|
|
public function testRedefinedTraitMethodWithoutAlias()
|
2017-01-07 20:35:07 +01:00
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
trait T {
|
|
|
|
public function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function fooFoo(string $a) : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new B)->fooFoo("hello");
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
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 testRedefinedTraitMethodWithAlias()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
trait T {
|
|
|
|
public function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T {
|
|
|
|
fooFoo as barBar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fooFoo() : void {
|
|
|
|
$this->barBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2017-01-07 20:35:07 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-31 17:49:04 +01:00
|
|
|
public function testTraitSelf()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
trait T {
|
|
|
|
public function g(): self
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = (new A)->g();
|
|
|
|
');
|
|
|
|
|
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-31 17:49:04 +01:00
|
|
|
$this->assertEquals('A', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-31 17:49:04 +01:00
|
|
|
public function testParentTraitSelf()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
trait T {
|
|
|
|
public function g(): self
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
class C {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
|
2016-12-31 17:49:04 +01:00
|
|
|
$a = (new B)->g();
|
|
|
|
');
|
|
|
|
|
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-31 17:49:04 +01:00
|
|
|
$this->assertEquals('A', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|