2016-12-17 04:16:29 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class TraitTest extends TestCase
|
2016-12-17 04:16:29 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2016-12-17 04:16:29 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2016-12-17 04:16:29 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'accessiblePrivateMethodFromTrait' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
private function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function doFoo() : void {
|
|
|
|
$this->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessibleProtectedMethodFromTrait' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
protected function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function doFoo() : void {
|
|
|
|
$this->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessiblePublicMethodFromTrait' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
public function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function doFoo() : void {
|
|
|
|
$this->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessiblePrivatePropertyFromTrait' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
/** @var string */
|
|
|
|
private $fooFoo = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo $this->fooFoo;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessibleProtectedPropertyFromTrait' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
/** @var string */
|
|
|
|
protected $fooFoo = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo $this->fooFoo;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessiblePublicPropertyFromTrait' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
/** @var string */
|
|
|
|
public $fooFoo = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo $this->fooFoo;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessibleProtectedMethodFromInheritedTrait' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
protected function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public function doFoo() : void {
|
|
|
|
$this->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessiblePublicMethodFromInheritedTrait' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
public function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public function doFoo() : void {
|
|
|
|
$this->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'staticClassMethodFromWithinTrait' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
public function fooFoo() : void {
|
|
|
|
self::barBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public static function barBar() : void {
|
|
|
|
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'redefinedTraitMethodWithoutAlias' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
public function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function fooFoo(string $a) : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
(new B)->fooFoo("hello");',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'redefinedTraitMethodWithAlias' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
public function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T {
|
|
|
|
fooFoo as barBar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fooFoo() : void {
|
|
|
|
$this->barBar();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'traitSelf' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
public function g(): self
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = (new A)->g();',
|
|
|
|
'assertions' => [
|
2017-05-27 02:05:57 +02:00
|
|
|
['A' => '$a'],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'parentTraitSelf' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
public function g(): self
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = (new B)->g();',
|
|
|
|
'assertions' => [
|
2017-05-27 02:05:57 +02:00
|
|
|
['A' => '$a'],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'directStaticCall' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
/** @return void */
|
|
|
|
public static function foo() {}
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
/** @return void */
|
|
|
|
public function bar() {
|
|
|
|
T::foo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'abstractTraitMethod' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
/** @return void */
|
|
|
|
abstract public function foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class A {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
/** @return void */
|
|
|
|
public function bar() {
|
|
|
|
$this->foo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2016-12-17 04:16:29 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'inaccessiblePrivateMethodFromInheritedTrait' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
private function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public function doFoo() : void {
|
|
|
|
$this->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'undefinedTrait' => [
|
|
|
|
'<?php
|
|
|
|
class B {
|
|
|
|
use A;
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedTrait',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingPropertyType' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function assignToFoo() : void {
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingPropertyType - somefile.php:3 - Property T::$foo does not have a ' .
|
2017-05-27 02:05:57 +02:00
|
|
|
'declared type - consider null|int',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingPropertyTypeWithConstructorInit' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function __construct() : void {
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingPropertyType - somefile.php:3 - Property T::$foo does not have a ' .
|
2017-05-27 02:05:57 +02:00
|
|
|
'declared type - consider int',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingPropertyTypeWithConstructorInitAndNull' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function __construct() : void {
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function makeNull() : void {
|
|
|
|
$this->foo = null;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingPropertyType - somefile.php:3 - Property T::$foo does not have a ' .
|
2017-05-27 02:05:57 +02:00
|
|
|
'declared type - consider null|int',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingPropertyTypeWithConstructorInitAndNullDefault' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
public $foo = null;
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function __construct() : void {
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingPropertyType - somefile.php:3 - Property T::$foo does not have a ' .
|
2017-05-27 02:05:57 +02:00
|
|
|
'declared type - consider int|nul',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-04-15 06:04:03 +02:00
|
|
|
}
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|