2018-01-22 05:42:57 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use function count;
|
2018-01-22 05:42:57 +01:00
|
|
|
use Psalm\Context;
|
2020-08-23 16:32:07 +02:00
|
|
|
use Psalm\Internal\RuntimeCaches;
|
2018-11-12 16:57:05 +01:00
|
|
|
use Psalm\Tests\Internal\Provider;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strpos;
|
2018-01-22 05:42:57 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-05-17 00:36:36 +02:00
|
|
|
public function setUp() : void
|
2018-01-22 05:42:57 +01:00
|
|
|
{
|
2020-08-23 16:32:07 +02:00
|
|
|
RuntimeCaches::clearAll();
|
2018-01-22 05:42:57 +01:00
|
|
|
|
|
|
|
$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
|
|
|
);
|
|
|
|
|
2019-04-13 00:28:07 +02:00
|
|
|
$this->project_analyzer->getCodebase()->collectLocations();
|
2019-02-07 21:27:43 +01:00
|
|
|
$this->project_analyzer->setPhpVersion('7.3');
|
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
|
|
|
|
*
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testReferenceLocations($input_code, $symbol, $expected_locations): void
|
2018-01-22 05:42:57 +01:00
|
|
|
{
|
2018-07-13 23:44:50 +02:00
|
|
|
$test_name = $this->getTestName();
|
2019-02-07 18:25:57 +01:00
|
|
|
if (strpos($test_name, 'SKIPPED-') !== false) {
|
2018-01-22 05:42:57 +01:00
|
|
|
$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
|
|
|
|
2019-04-13 00:28:07 +02:00
|
|
|
if (!$found_references) {
|
2018-01-22 05:42:57 +01:00
|
|
|
throw new \UnexpectedValueException('No file references found in this file');
|
|
|
|
}
|
|
|
|
|
2019-04-13 00:28:07 +02:00
|
|
|
$this->assertSame(count($found_references), count($expected_locations));
|
2018-01-22 05:42:57 +01:00
|
|
|
|
|
|
|
foreach ($expected_locations as $i => $expected_location) {
|
2019-04-13 00:28:07 +02:00
|
|
|
$actual_location = $found_references[$i];
|
2018-01-22 05:42:57 +01:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expected_location,
|
|
|
|
$actual_location->getLineNumber() . ':' . $actual_location->getColumn()
|
|
|
|
. ':' . $actual_location->getSelectedText()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 00:37:24 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider providerReferencedMethods
|
|
|
|
*
|
2019-04-16 22:07:48 +02:00
|
|
|
* @param array<string,array<string,bool>> $expected_method_references_to_members
|
|
|
|
* @param array<string,array<string,bool>> $expected_file_references_to_members
|
|
|
|
* @param array<string,array<string,bool>> $expected_method_references_to_missing_members
|
|
|
|
* @param array<string,array<string,bool>> $expected_file_references_to_missing_members
|
2018-09-26 00:37:24 +02:00
|
|
|
*
|
|
|
|
*/
|
2019-04-16 22:07:48 +02:00
|
|
|
public function testReferencedMethods(
|
|
|
|
string $input_code,
|
|
|
|
array $expected_method_references_to_members,
|
|
|
|
array $expected_method_references_to_missing_members,
|
|
|
|
array $expected_file_references_to_members,
|
|
|
|
array $expected_file_references_to_missing_members
|
2020-09-12 17:24:05 +02:00
|
|
|
): void {
|
2018-09-26 00:37:24 +02:00
|
|
|
$test_name = $this->getTestName();
|
2019-02-07 18:25:57 +01:00
|
|
|
if (strpos($test_name, 'SKIPPED-') !== false) {
|
2018-09-26 00:37:24 +02:00
|
|
|
$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);
|
|
|
|
|
2019-04-16 22:07:48 +02:00
|
|
|
$referenced_members = $this->project_analyzer->getCodebase()->file_reference_provider->getAllMethodReferencesToClassMembers();
|
|
|
|
|
|
|
|
$this->assertSame($expected_method_references_to_members, $referenced_members);
|
|
|
|
|
|
|
|
$referenced_missing_members = $this->project_analyzer->getCodebase()->file_reference_provider->getAllMethodReferencesToMissingClassMembers();
|
|
|
|
|
|
|
|
$this->assertSame($expected_method_references_to_missing_members, $referenced_missing_members);
|
|
|
|
|
|
|
|
$referenced_files = $this->project_analyzer->getCodebase()->file_reference_provider->getAllFileReferencesToClassMembers();
|
|
|
|
|
|
|
|
$this->assertSame($expected_file_references_to_members, $referenced_files);
|
2018-09-26 00:37:24 +02:00
|
|
|
|
2019-04-16 22:07:48 +02:00
|
|
|
$referenced_missing_files = $this->project_analyzer->getCodebase()->file_reference_provider->getAllFileReferencesToMissingClassMembers();
|
|
|
|
|
|
|
|
$this->assertSame($expected_file_references_to_missing_members, $referenced_missing_files);
|
2018-09-26 00:37:24 +02:00
|
|
|
}
|
|
|
|
|
2018-01-22 05:42:57 +01:00
|
|
|
/**
|
2019-02-23 22:22:39 +01:00
|
|
|
* @return array<string,array{string,string,array<int,string>}>
|
2018-01-22 05:42:57 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerReferenceLocations(): array
|
2018-01-22 05:42:57 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'getClassLocation' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
new A();',
|
|
|
|
'A',
|
2019-03-23 19:27:54 +01: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',
|
2019-01-12 15:13:54 +01:00
|
|
|
['6:32:foo'],
|
2018-01-22 05:42:57 +01:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2018-09-26 00:37:24 +02:00
|
|
|
|
|
|
|
/**
|
2019-04-16 22:07:48 +02:00
|
|
|
* @return array<string, array{
|
|
|
|
* 0: string,
|
|
|
|
* 1: array<string,array<string,bool>>,
|
|
|
|
* 2: array<string,array<string,bool>>,
|
|
|
|
* 3: array<string,array<string,bool>>,
|
|
|
|
* 4: array<string,array<string,bool>>
|
|
|
|
* }>
|
2018-09-26 00:37:24 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerReferencedMethods(): array
|
2018-09-26 00:37:24 +02:00
|
|
|
{
|
|
|
|
return [
|
2019-04-16 22:07:48 +02:00
|
|
|
'getClassReferences' => [
|
2018-09-26 00:37:24 +02:00
|
|
|
'<?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();
|
|
|
|
}
|
2019-04-16 22:07:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class D {
|
|
|
|
/** @var ?string */
|
|
|
|
public $foo;
|
|
|
|
public function __construct() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$d = new D();
|
|
|
|
$d->foo = "bar";
|
|
|
|
|
|
|
|
$a = new A();',
|
2018-09-26 00:37:24 +02:00
|
|
|
[
|
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::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,
|
|
|
|
],
|
2019-04-16 22:07:48 +02:00
|
|
|
'foo\c::foo' => [
|
2018-09-26 00:37:24 +02:00
|
|
|
'foo\b::bar' => true,
|
|
|
|
],
|
2019-04-16 22:07:48 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'foo\a::__construct' => [
|
|
|
|
'foo\b::__construct' => true,
|
|
|
|
'foo\c::foo' => true,
|
|
|
|
],
|
|
|
|
'foo\c::__construct' => [
|
2018-09-26 00:37:24 +02:00
|
|
|
'foo\b::bar' => true,
|
2019-03-23 19:27:54 +01:00
|
|
|
],
|
2018-09-26 00:37:24 +02:00
|
|
|
],
|
2019-04-16 22:07:48 +02:00
|
|
|
[
|
|
|
|
'foo\d::__construct' => [
|
|
|
|
'/var/www/somefile.php' => true,
|
|
|
|
],
|
|
|
|
'foo\d::$foo' => [
|
|
|
|
'/var/www/somefile.php' => true,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'foo\a::__construct' => [
|
|
|
|
'/var/www/somefile.php' => true,
|
|
|
|
],
|
|
|
|
],
|
2018-09-26 00:37:24 +02:00
|
|
|
],
|
|
|
|
'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,
|
|
|
|
],
|
|
|
|
],
|
2019-04-16 22:07:48 +02:00
|
|
|
[],
|
|
|
|
[],
|
2019-07-05 22:24:00 +02:00
|
|
|
[],
|
2018-09-26 00:37:24 +02:00
|
|
|
],
|
|
|
|
'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,
|
|
|
|
],
|
|
|
|
],
|
2019-04-16 22:07:48 +02:00
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
2019-07-05 22:24:00 +02:00
|
|
|
[],
|
2018-09-26 00:37:24 +02:00
|
|
|
],
|
|
|
|
'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,
|
|
|
|
],
|
|
|
|
],
|
2019-04-16 22:07:48 +02:00
|
|
|
[],
|
|
|
|
[],
|
2019-07-05 22:24:00 +02:00
|
|
|
[],
|
2018-09-26 00:37:24 +02:00
|
|
|
],
|
|
|
|
'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;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[
|
2019-04-16 22:07:48 +02:00
|
|
|
'use:A:d7863b8594fe57f85cb8183fe55a6c15' => [
|
2018-09-26 00:37:24 +02:00
|
|
|
'foo\b::__construct' => true,
|
|
|
|
'foo\c::foo' => true,
|
|
|
|
],
|
2019-04-16 22:07:48 +02:00
|
|
|
'foo\a::$fooBar' => [
|
2018-11-02 02:52:39 +01:00
|
|
|
'foo\b::__construct' => true,
|
|
|
|
'foo\c::foo' => true,
|
|
|
|
],
|
2019-04-16 22:07:48 +02:00
|
|
|
|
|
|
|
],
|
|
|
|
[
|
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,
|
|
|
|
],
|
|
|
|
],
|
2019-04-16 22:07:48 +02:00
|
|
|
[],
|
2019-07-05 22:24:00 +02:00
|
|
|
[],
|
2018-09-26 00:37:24 +02:00
|
|
|
],
|
2019-05-23 19:10:23 +02:00
|
|
|
'traitAbstractRefs' => [
|
2019-05-23 18:59:09 +02:00
|
|
|
'<?php
|
|
|
|
namespace Ns;
|
|
|
|
|
|
|
|
abstract class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
public function bar(A $a) : void {
|
|
|
|
$a->foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
use T;
|
|
|
|
}',
|
|
|
|
[
|
2021-02-15 17:31:34 +01:00
|
|
|
'use:A:d7863b8594fe57f85cb8183fe55a6c15' => [
|
|
|
|
'ns\c::bar' => true
|
|
|
|
],
|
2019-05-23 18:59:09 +02:00
|
|
|
'ns\a::foo' => [
|
|
|
|
'ns\c::bar' => true,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[],
|
2019-07-05 22:24:00 +02:00
|
|
|
[],
|
2019-05-23 18:59:09 +02:00
|
|
|
],
|
2018-09-26 00:37:24 +02:00
|
|
|
];
|
|
|
|
}
|
2018-01-22 05:42:57 +01:00
|
|
|
}
|