2017-01-02 01:09:17 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class ClassScopeTest 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
|
|
|
|
*/
|
2017-01-02 01:09:17 +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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 01:09:17 +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);
|
2017-01-02 01:09:17 +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
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
|
|
|
public function testInaccessiblePrivateMethod()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
private function fooFoo() : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new A())->fooFoo();
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +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
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
|
|
|
public function testInaccessibleProtectedMethod()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
protected function fooFoo() : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new A())->fooFoo();
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 01:09:17 +01:00
|
|
|
public function testAccessiblePrivateMethodFromSubclass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
private function fooFoo() : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function barBar() : void {
|
|
|
|
$this->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +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
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
|
|
|
public function testInaccessiblePrivateMethodFromSubclass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
private function fooFoo() : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function doFoo() : void {
|
|
|
|
$this->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 01:09:17 +01:00
|
|
|
public function testAccessibleProtectedMethodFromSubclass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
protected function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function doFoo() : void {
|
|
|
|
$this->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 01:09:17 +01:00
|
|
|
public function testAccessibleProtectedMethodFromOtherSubclass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
protected function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A { }
|
|
|
|
|
|
|
|
class C extends A {
|
|
|
|
public function doFoo() : void {
|
|
|
|
(new B)->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +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
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
|
|
|
public function testInaccessibleProtectedMethodFromOtherSubclass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
trait T {
|
|
|
|
protected function fooFoo() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function doFoo() : void {
|
|
|
|
(new B)->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-02 01:09:17 +01:00
|
|
|
* @expectedExceptionMessage InaccessibleProperty
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
|
|
|
public function testInaccessiblePrivateProperty()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private $fooFoo;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new A())->fooFoo;
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-02 01:09:17 +01:00
|
|
|
* @expectedExceptionMessage InaccessibleProperty
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
|
|
|
public function testInaccessibleProtectedProperty()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
protected $fooFoo;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new A())->fooFoo;
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-02 01:09:17 +01:00
|
|
|
* @expectedExceptionMessage InaccessibleProperty
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
|
|
|
public function testInaccessiblePrivatePropertyFromSubclass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
2017-01-27 07:23:12 +01:00
|
|
|
private $fooFoo = "";
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo $this->fooFoo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-02 01:09:17 +01:00
|
|
|
* @expectedExceptionMessage InaccessibleProperty
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
|
|
|
public function testInaccessibleStaticPrivateProperty()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private static $fooFoo;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo A::$fooFoo;
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-02 01:09:17 +01:00
|
|
|
* @expectedExceptionMessage InaccessibleProperty
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
|
|
|
public function testInaccessibleStaticProtectedProperty()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
protected static $fooFoo;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo A::$fooFoo;
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-02 01:09:17 +01:00
|
|
|
* @expectedExceptionMessage InaccessibleProperty
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
|
|
|
public function testInaccessibleStaticPrivatePropertyFromSubclass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private static $fooFoo;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo A::$fooFoo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 01:09:17 +01:00
|
|
|
public function testAccessibleProtectedPropertyFromSubclass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
2017-01-27 07:23:12 +01:00
|
|
|
protected $fooFoo = "";
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo $this->fooFoo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 01:09:17 +01:00
|
|
|
public function testAccessibleProtectedPropertyFromGreatGrandparent()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
2017-01-27 07:23:12 +01:00
|
|
|
protected $fooFoo = "";
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A { }
|
|
|
|
|
|
|
|
class C extends B { }
|
|
|
|
|
|
|
|
class D extends C {
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo $this->fooFoo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 01:09:17 +01:00
|
|
|
public function testAccessibleProtectedPropertyFromOtherSubclass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
2017-01-27 07:23:12 +01:00
|
|
|
protected $fooFoo = "";
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends A {
|
|
|
|
public function fooFoo() : void {
|
|
|
|
$b = new B();
|
|
|
|
$b->fooFoo = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 01:09:17 +01:00
|
|
|
public function testAccessibleStaticPropertyFromSubclass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
2017-01-27 07:23:12 +01:00
|
|
|
protected static $fooFoo = "";
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
public function barBar() : void {
|
|
|
|
echo self::$fooFoo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function doFoo() : void {
|
|
|
|
echo A::$fooFoo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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();
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
2017-04-16 03:10:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDefinedPrivateMethod()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : void {
|
|
|
|
if ($this instanceof B) {
|
|
|
|
$this->boop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function boop() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
private function boop() : void {}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|