2017-01-02 01:09:17 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class ClassScopeTest extends TestCase
|
2017-01-02 01:09:17 +01:00
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2017-01-02 01:09:17 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'accessiblePrivateMethodFromSubclass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
private function fooFoo(): void {
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
private function barBar(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessibleProtectedMethodFromSubclass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
protected function fooFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessibleProtectedMethodFromOtherSubclass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
protected function fooFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A { }
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class C extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
(new B)->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessibleProtectedPropertyFromSubclass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
protected $fooFoo = "";
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $this->fooFoo;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessibleProtectedPropertyFromGreatGrandparent' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
protected $fooFoo = "";
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A { }
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class C extends B { }
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class D extends C {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $this->fooFoo;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessibleProtectedPropertyFromOtherSubclass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
protected $fooFoo = "";
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class C extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = new B();
|
|
|
|
$b->fooFoo = "hello";
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessibleStaticPropertyFromSubclass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
protected static $fooFoo = "";
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
public function barBar(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
echo self::$fooFoo;
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
echo A::$fooFoo;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'definedPrivateMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($this instanceof B) {
|
|
|
|
$this->boop();
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
private function boop(): void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
private function boop(): void {}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2017-01-02 01:09:17 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'inaccessiblePrivateMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
private function fooFoo(): void {
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
(new A())->fooFoo();',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'inaccessibleProtectMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
protected function fooFoo(): void {
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
(new A())->fooFoo();',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'inaccessiblePrivateMethodFromSubclass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
private function fooFoo(): void {
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
2017-11-09 03:27:23 +01:00
|
|
|
'error_message' => 'UndefinedMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'inaccessibleProtectredMethodFromOtherSubclass' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
2018-01-11 21:50:45 +01:00
|
|
|
protected function fooFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class C {
|
|
|
|
use T;
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
(new B)->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'inaccessiblePrivateProperty' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private $fooFoo;
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo (new A())->fooFoo;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleProperty',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'inaccessibleProtectedProperty' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
protected $fooFoo;
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo (new A())->fooFoo;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleProperty',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'inaccessiblePrivatePropertyFromSubclass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private $fooFoo = "";
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $this->fooFoo;
|
|
|
|
}
|
|
|
|
}',
|
2017-06-30 07:24:45 +02:00
|
|
|
'error_message' => 'UndefinedThisPropertyFetch',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'inaccessibleStaticPrivateProperty' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private static $fooFoo;
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo A::$fooFoo;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleProperty',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'inaccessibleStaticProtectedProperty' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
protected static $fooFoo;
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo A::$fooFoo;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleProperty',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'inaccessibleStaticPrivatePropertyFromSubclass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private static $fooFoo;
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
echo A::$fooFoo;
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleProperty',
|
2017-05-25 05:55:54 +02:00
|
|
|
],
|
2018-04-17 18:16:25 +02:00
|
|
|
'privateConstructorInheritanceNoCall' => [
|
2017-05-25 05:55:54 +02:00
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private function __construct() { }
|
|
|
|
}
|
|
|
|
class B extends A {}
|
|
|
|
new B();',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleMethod',
|
2017-05-25 05:55:54 +02:00
|
|
|
],
|
|
|
|
'privateConstructorInheritanceCall' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private function __construct() { }
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleMethod',
|
|
|
|
],
|
2020-03-01 23:25:50 +01:00
|
|
|
'noSelfInFunctionConstant' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : void {
|
|
|
|
echo self::SOMETHING;
|
|
|
|
}',
|
|
|
|
'error_message' => 'NonStaticSelfCall',
|
|
|
|
],
|
|
|
|
'noSelfInFunctionCall' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : void {
|
|
|
|
echo self::bar();
|
|
|
|
}',
|
|
|
|
'error_message' => 'NonStaticSelfCall',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-04-16 03:10:49 +02:00
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|