2017-01-02 01:09:17 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2023-10-19 13:12:06 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class ClassScopeTest extends TestCase
|
2017-01-02 01:09:17 +01:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2017-01-02 01:09:17 +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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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
|
|
|
}',
|
|
|
|
],
|
2021-04-25 18:44:53 +02:00
|
|
|
'allowMethodCallToProtectedFromParent' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-04-25 18:44:53 +02:00
|
|
|
class A {
|
|
|
|
public function __construct() {
|
|
|
|
B::foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
protected static function foo(): void {
|
|
|
|
echo "here";
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-04-25 18:44:53 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-05-25 05:55:54 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-05-25 05:55:54 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-01 23:25:50 +01:00
|
|
|
function foo() : void {
|
|
|
|
echo self::SOMETHING;
|
|
|
|
}',
|
|
|
|
'error_message' => 'NonStaticSelfCall',
|
|
|
|
],
|
|
|
|
'noSelfInFunctionCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-01 23:25:50 +01:00
|
|
|
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
|
|
|
}
|