2018-01-22 05:42:57 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
2018-01-22 05:42:57 +01:00
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class FileReferenceTest extends TestCase
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
/** @var \Psalm\Internal\Analyzer\ProjectAnalyzer */
|
2018-11-11 18:01:14 +01:00
|
|
|
protected $project_analyzer;
|
2018-01-22 05:42:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
FileAnalyzer::clearCache();
|
2018-01-22 05:42:57 +01:00
|
|
|
\Psalm\FileManipulation\FunctionDocblockManipulator::clearCache();
|
|
|
|
|
|
|
|
$this->file_provider = new Provider\FakeFileProvider();
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = new \Psalm\Internal\Analyzer\ProjectAnalyzer(
|
2018-01-22 05:42:57 +01:00
|
|
|
new TestConfig(),
|
2018-11-06 03:57:36 +01:00
|
|
|
new \Psalm\Internal\Provider\Providers(
|
2018-09-28 22:18:45 +02:00
|
|
|
$this->file_provider,
|
|
|
|
new Provider\FakeParserCacheProvider()
|
|
|
|
)
|
2018-01-22 05:42:57 +01:00
|
|
|
);
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer->getCodebase()->collectReferences();
|
2018-01-22 05:42:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-26 00:37:24 +02:00
|
|
|
* @dataProvider providerReferenceLocations
|
2018-01-22 05:42:57 +01:00
|
|
|
*
|
|
|
|
* @param string $input_code
|
|
|
|
* @param string $symbol
|
|
|
|
* @param array<int, string> $expected_locations
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-09-26 00:37:24 +02:00
|
|
|
public function testReferenceLocations($input_code, $symbol, $expected_locations)
|
2018-01-22 05:42:57 +01:00
|
|
|
{
|
2018-07-13 23:44:50 +02:00
|
|
|
$test_name = $this->getTestName();
|
2018-01-22 05:42:57 +01:00
|
|
|
if (strpos($test_name, 'PHP7-') !== false) {
|
|
|
|
if (version_compare(PHP_VERSION, '7.0.0dev', '<')) {
|
|
|
|
$this->markTestSkipped('Test case requires PHP 7.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} elseif (strpos($test_name, 'SKIPPED-') !== false) {
|
|
|
|
$this->markTestSkipped('Skipped due to a bug.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$file_path = self::$src_dir_path . 'somefile.php';
|
|
|
|
|
|
|
|
$this->addFile($file_path, $input_code);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, $context);
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$found_references = $this->project_analyzer->getCodebase()->findReferencesToSymbol($symbol);
|
2018-01-22 05:42:57 +01:00
|
|
|
|
|
|
|
if (!isset($found_references[$file_path])) {
|
|
|
|
throw new \UnexpectedValueException('No file references found in this file');
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_references = $found_references[$file_path];
|
|
|
|
|
|
|
|
$this->assertSame(count($file_references), count($expected_locations));
|
|
|
|
|
|
|
|
foreach ($expected_locations as $i => $expected_location) {
|
|
|
|
$actual_location = $file_references[$i];
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expected_location,
|
|
|
|
$actual_location->getLineNumber() . ':' . $actual_location->getColumn()
|
|
|
|
. ':' . $actual_location->getSelectedText()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider providerReferencedMethods
|
|
|
|
*
|
|
|
|
* @param string $input_code
|
|
|
|
* @param string $symbol
|
|
|
|
* @param array<int, string> $expected_locations
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testReferencedMethods($input_code, array $expected_referenced_methods)
|
|
|
|
{
|
|
|
|
$test_name = $this->getTestName();
|
|
|
|
if (strpos($test_name, 'PHP7-') !== false) {
|
|
|
|
if (version_compare(PHP_VERSION, '7.0.0dev', '<')) {
|
|
|
|
$this->markTestSkipped('Test case requires PHP 7.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} elseif (strpos($test_name, 'SKIPPED-') !== false) {
|
|
|
|
$this->markTestSkipped('Skipped due to a bug.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
2018-11-02 03:03:47 +01:00
|
|
|
$file_path = '/var/www/somefile.php';
|
2018-09-26 00:37:24 +02:00
|
|
|
|
|
|
|
$this->addFile($file_path, $input_code);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, $context);
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$referenced_methods = $this->project_analyzer->file_reference_provider->getClassMethodReferences();
|
2018-09-26 00:37:24 +02:00
|
|
|
|
|
|
|
$this->assertSame($expected_referenced_methods, $referenced_methods);
|
|
|
|
}
|
|
|
|
|
2018-01-22 05:42:57 +01:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-09-26 00:37:24 +02:00
|
|
|
public function providerReferenceLocations()
|
2018-01-22 05:42:57 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'getClassLocation' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
new A();',
|
|
|
|
'A',
|
2018-09-26 00:37:24 +02:00
|
|
|
['4:25:A']
|
2018-01-22 05:42:57 +01:00
|
|
|
],
|
|
|
|
'getMethodLocation' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo(): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new A())->foo();',
|
|
|
|
'A::foo',
|
|
|
|
['6:21:(new A())->foo()'],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2018-09-26 00:37:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerReferencedMethods()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'getClassLocation' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public static function bat() : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function __construct() {
|
|
|
|
new A();
|
|
|
|
A::bat();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bar() : void {
|
|
|
|
(new C)->foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
public function foo() : void {
|
|
|
|
new A();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[
|
2018-11-02 03:03:47 +01:00
|
|
|
'use:A:d7863b8594fe57f85cb8183fe55a6c15' => [
|
2018-11-02 02:52:39 +01:00
|
|
|
'foo\b::__construct' => true,
|
|
|
|
'foo\c::foo' => true,
|
|
|
|
],
|
2018-09-26 00:37:24 +02:00
|
|
|
'foo\a::__construct' => [
|
|
|
|
'foo\b::__construct' => true,
|
|
|
|
'foo\c::foo' => true,
|
|
|
|
],
|
|
|
|
'foo\a::bat' => [
|
|
|
|
'foo\b::__construct' => true,
|
|
|
|
],
|
2018-11-02 03:03:47 +01:00
|
|
|
'use:C:d7863b8594fe57f85cb8183fe55a6c15' => [
|
2018-11-02 02:52:39 +01:00
|
|
|
'foo\b::bar' => true,
|
|
|
|
],
|
2018-09-26 00:37:24 +02:00
|
|
|
'foo\c::__construct' => [
|
|
|
|
'foo\b::bar' => true,
|
|
|
|
],
|
|
|
|
'foo\c::foo' => [
|
|
|
|
'foo\b::bar' => true,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'interpolateClassCalls' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function __construct() {}
|
|
|
|
public static function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A { }
|
|
|
|
|
|
|
|
class C extends B { }
|
|
|
|
|
|
|
|
class D {
|
|
|
|
public function bat() : void {
|
|
|
|
$c = new C();
|
|
|
|
$c->bar();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[
|
2018-11-02 03:03:47 +01:00
|
|
|
'use:C:d7863b8594fe57f85cb8183fe55a6c15' => [
|
2018-11-02 02:52:39 +01:00
|
|
|
'foo\d::bat' => true,
|
|
|
|
],
|
2018-09-26 00:37:24 +02:00
|
|
|
'foo\b::__construct' => [
|
|
|
|
'foo\d::bat' => true,
|
|
|
|
],
|
|
|
|
'foo\a::__construct' => [
|
|
|
|
'foo\d::bat' => true,
|
|
|
|
],
|
|
|
|
'foo\c::__construct' => [
|
|
|
|
'foo\d::bat' => true,
|
|
|
|
],
|
|
|
|
'foo\b::bar' => [
|
|
|
|
'foo\d::bat' => true,
|
|
|
|
],
|
|
|
|
'foo\a::bar' => [
|
|
|
|
'foo\d::bat' => true,
|
|
|
|
],
|
|
|
|
'foo\c::bar' => [
|
|
|
|
'foo\d::bat' => true,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'constantRefs' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
const C = "bar";
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function __construct() {
|
|
|
|
echo A::C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
public function foo() : void {
|
|
|
|
echo A::C;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[
|
|
|
|
'foo\a::C' => [
|
|
|
|
'foo\b::__construct' => true,
|
|
|
|
'foo\c::foo' => true,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'staticPropertyRefs' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public static $fooBar = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function __construct() {
|
|
|
|
echo A::$fooBar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
public function foo() : void {
|
|
|
|
echo A::$fooBar;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[
|
2018-11-02 03:03:47 +01:00
|
|
|
'use:A:d7863b8594fe57f85cb8183fe55a6c15' => [
|
2018-11-02 02:52:39 +01:00
|
|
|
'foo\b::__construct' => true,
|
|
|
|
'foo\c::foo' => true,
|
|
|
|
],
|
2018-09-26 00:37:24 +02:00
|
|
|
'foo\a::$fooBar' => [
|
|
|
|
'foo\b::__construct' => true,
|
|
|
|
'foo\c::foo' => true,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'instancePropertyRefs' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $fooBar = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function __construct() {
|
|
|
|
echo (new A)->fooBar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
public function foo() : void {
|
|
|
|
echo (new A)->fooBar;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[
|
2018-10-07 04:58:21 +02:00
|
|
|
'foo\a::$fooBar' => [
|
|
|
|
'foo\a::__construct' => true,
|
2018-09-26 00:37:24 +02:00
|
|
|
'foo\b::__construct' => true,
|
|
|
|
'foo\c::foo' => true,
|
|
|
|
],
|
2018-11-02 03:03:47 +01:00
|
|
|
'use:A:d7863b8594fe57f85cb8183fe55a6c15' => [
|
2018-11-02 02:52:39 +01:00
|
|
|
'foo\b::__construct' => true,
|
|
|
|
'foo\c::foo' => true,
|
|
|
|
],
|
2018-10-07 04:58:21 +02:00
|
|
|
'foo\a::__construct' => [
|
2018-09-26 00:37:24 +02:00
|
|
|
'foo\b::__construct' => true,
|
|
|
|
'foo\c::foo' => true,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2018-01-22 05:42:57 +01:00
|
|
|
}
|