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
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-01-02 01:09:17 +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()
|
2017-01-02 01:09:17 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'accessiblePrivateMethodFromSubclass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
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
|
|
|
private function barBar() : void {
|
|
|
|
$this->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessibleProtectedMethodFromSubclass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
protected function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
|
|
|
public function doFoo() : void {
|
|
|
|
$this->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'accessibleProtectedMethodFromOtherSubclass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
protected function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
public function doFoo() : void {
|
|
|
|
(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 {
|
|
|
|
public function doFoo() : void {
|
|
|
|
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 {
|
|
|
|
public function doFoo() : void {
|
|
|
|
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 {
|
|
|
|
public function fooFoo() : void {
|
|
|
|
$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
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function barBar() : void {
|
|
|
|
echo self::$fooFoo;
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo A::$fooFoo;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'definedPrivateMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : void {
|
|
|
|
if ($this instanceof B) {
|
|
|
|
$this->boop();
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
private function boop() : void {}
|
|
|
|
}
|
2017-05-25 05:55:54 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2017-01-02 01:09:17 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'inaccessiblePrivateMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
public function doFoo() : void {
|
|
|
|
$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 {
|
|
|
|
protected function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function doFoo() : void {
|
|
|
|
(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 {
|
|
|
|
public function doFoo() : void {
|
|
|
|
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 {
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo A::$fooFoo;
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InaccessibleProperty',
|
2017-05-25 05:55:54 +02:00
|
|
|
],
|
|
|
|
'privateConstructorInheritance' => [
|
|
|
|
'<?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',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-04-16 03:10:49 +02:00
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|