2017-01-08 01:07:58 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class IncludeTest extends TestCase
|
2017-01-08 01:07:58 +01:00
|
|
|
{
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @dataProvider providerTestValidIncludes
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-06-05 22:46:04 +02:00
|
|
|
* @param array<int, string> $files_to_check
|
|
|
|
* @param array<string, string> $files
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public function testValidInclude(array $files, array $files_to_check)
|
2017-01-08 01:33:33 +01:00
|
|
|
{
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase = $this->project_checker->getCodebase();
|
|
|
|
|
2018-03-17 20:02:25 +01:00
|
|
|
foreach ($files as $file_path => $contents) {
|
|
|
|
$this->addFile($file_path, $contents);
|
|
|
|
$codebase->scanner->addFilesToShallowScan([$file_path => $file_path]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($files_to_check as $file_path) {
|
|
|
|
$codebase->addFilesToAnalyze([$file_path => $file_path]);
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-01-08 01:33:33 +01:00
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase->scanFiles();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$config = $codebase->config;
|
2018-01-21 18:44:46 +01:00
|
|
|
|
|
|
|
foreach ($files_to_check as $file_path) {
|
|
|
|
$file_checker = new FileChecker($this->project_checker, $file_path, $config->shortenFileName($file_path));
|
2017-07-25 22:11:02 +02:00
|
|
|
$file_checker->analyze();
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 01:33:33 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider providerTestInvalidIncludes
|
|
|
|
*
|
|
|
|
* @param array<int, string> $files_to_check
|
|
|
|
* @param array<string, string> $files
|
|
|
|
* @param mixed $error_message
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvalidInclude(array $files, array $files_to_check, $error_message)
|
|
|
|
{
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase = $this->project_checker->getCodebase();
|
|
|
|
|
2018-03-17 20:02:25 +01:00
|
|
|
foreach ($files as $file_path => $contents) {
|
|
|
|
$this->addFile($file_path, $contents);
|
|
|
|
$codebase->scanner->addFilesToShallowScan([$file_path => $file_path]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($files_to_check as $file_path) {
|
|
|
|
$codebase->addFilesToAnalyze([$file_path => $file_path]);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase->scanFiles();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
$this->expectException('\Psalm\Exception\CodeException');
|
2017-11-16 03:04:25 +01:00
|
|
|
$this->expectExceptionMessageRegexp('/\b' . preg_quote($error_message, '/') . '\b/');
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$config = $codebase->config;
|
2018-01-21 18:44:46 +01:00
|
|
|
|
|
|
|
foreach ($files_to_check as $file_path) {
|
|
|
|
$file_checker = new FileChecker($this->project_checker, $file_path, $config->shortenFileName($file_path));
|
2017-07-25 22:11:02 +02:00
|
|
|
$file_checker->analyze();
|
2017-06-02 05:16:45 +02:00
|
|
|
}
|
2017-01-08 01:33:33 +01:00
|
|
|
}
|
2017-01-08 17:24:01 +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 providerTestValidIncludes()
|
2017-01-08 17:24:01 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'basicRequire' => [
|
2017-06-02 05:16:45 +02:00
|
|
|
'files' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
class B {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
(new A)->fooFoo();
|
2017-06-02 05:16:45 +02:00
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-06-02 05:16:45 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
2017-07-25 22:11:02 +02:00
|
|
|
class A{
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}',
|
2017-06-02 05:16:45 +02:00
|
|
|
],
|
|
|
|
'files_to_check' => [
|
2017-06-06 04:12:19 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nestedRequire' => [
|
2017-06-02 05:16:45 +02:00
|
|
|
'files' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
class A{
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void {
|
2017-06-02 05:16:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}',
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
require("file1.php");
|
2017-06-02 05:16:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A{
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-06-02 05:16:45 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file3.php' => '<?php
|
|
|
|
require("file2.php");
|
|
|
|
|
|
|
|
class C extends B {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doFoo(): void {
|
2017-06-02 05:16:45 +02:00
|
|
|
$this->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'files_to_check' => [
|
2017-06-06 04:12:19 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file3.php',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'requireNamespace' => [
|
2017-06-02 05:16:45 +02:00
|
|
|
'files' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
namespace Foo;
|
2017-06-02 05:16:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A{
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-06-02 05:16:45 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
class B {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {
|
2017-06-02 05:16:45 +02:00
|
|
|
(new Foo\A);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'files_to_check' => [
|
2017-06-06 04:12:19 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'requireFunction' => [
|
2017-06-02 05:16:45 +02:00
|
|
|
'files' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(): void {
|
2017-06-02 05:16:45 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-06-02 05:16:45 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
fooFoo();',
|
|
|
|
],
|
|
|
|
'files_to_check' => [
|
2017-06-06 04:12:19 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-01-12 18:33:26 +01:00
|
|
|
'namespacedRequireFunction' => [
|
|
|
|
'files' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
function fooFoo(): void {
|
|
|
|
|
|
|
|
}',
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
fooFoo();',
|
|
|
|
],
|
|
|
|
'files_to_check' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
|
|
|
],
|
|
|
|
],
|
2017-07-28 16:42:30 +02:00
|
|
|
'requireConstant' => [
|
|
|
|
'files' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
const FOO = 5;
|
|
|
|
define("BAR", "bat");',
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
echo FOO;
|
|
|
|
echo BAR;',
|
|
|
|
],
|
|
|
|
'files_to_check' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'requireNamespacedWithUse' => [
|
2017-06-02 05:16:45 +02:00
|
|
|
'files' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
namespace Foo;
|
2017-06-02 05:16:45 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A{
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-06-02 05:16:45 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
use Foo\A;
|
|
|
|
|
|
|
|
class B {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {
|
2017-06-02 05:16:45 +02:00
|
|
|
(new A);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'files_to_check' => [
|
2017-06-06 04:12:19 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
|
|
|
],
|
2017-06-05 16:21:03 +02:00
|
|
|
'noInfiniteRequireLoop' => [
|
|
|
|
'files' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
require_once("file2.php");
|
2017-06-06 16:11:34 +02:00
|
|
|
require_once("file3.php");
|
2017-06-05 16:21:03 +02:00
|
|
|
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doFoo(): void {
|
2017-06-05 16:21:03 +02:00
|
|
|
$this->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 16:11:34 +02:00
|
|
|
class C {}
|
|
|
|
|
|
|
|
new D();',
|
2017-06-05 22:46:04 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
2017-06-06 16:11:34 +02:00
|
|
|
require_once("file3.php");
|
2017-06-05 22:46:04 +02:00
|
|
|
|
|
|
|
class A{
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void { }
|
2017-06-05 22:46:04 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 16:11:34 +02:00
|
|
|
new C();',
|
|
|
|
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file3.php' => '<?php
|
|
|
|
require_once("file1.php");
|
|
|
|
|
|
|
|
class D{ }
|
|
|
|
|
2017-06-05 22:46:04 +02:00
|
|
|
new C();',
|
2017-06-05 16:21:03 +02:00
|
|
|
],
|
|
|
|
'files_to_check' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
|
2017-06-05 22:46:04 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
2017-06-06 16:11:34 +02:00
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file3.php',
|
2017-06-05 16:21:03 +02:00
|
|
|
],
|
|
|
|
],
|
2017-07-25 22:11:02 +02:00
|
|
|
'analyzeAllClasses' => [
|
|
|
|
'files' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
require_once("file2.php");
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doFoo(): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class C {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function barBar(): void { }
|
2017-07-25 22:11:02 +02:00
|
|
|
}',
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
require_once("file1.php");
|
|
|
|
class A{
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void { }
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
class D extends C {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function doBar(): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->barBar();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'files_to_check' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
2017-07-31 21:10:42 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'loopWithInterdependencies' => [
|
|
|
|
'files' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
require_once("file2.php");
|
|
|
|
class A {}
|
|
|
|
class D extends C {}
|
|
|
|
new B();',
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
require_once("file1.php");
|
|
|
|
class C {}
|
|
|
|
class B extends A {}
|
|
|
|
new D();',
|
|
|
|
],
|
|
|
|
'files_to_check' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
2017-07-25 22:11:02 +02:00
|
|
|
],
|
|
|
|
],
|
2018-03-17 20:02:25 +01:00
|
|
|
'variadicArgs' => [
|
|
|
|
'files' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
require_once("file2.php");
|
|
|
|
variadicArgs(5, 2, "hello");',
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
function variadicArgs() {
|
|
|
|
$args = func_get_args();
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'files_to_check' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php',
|
|
|
|
],
|
|
|
|
],
|
2018-03-31 01:15:24 +02:00
|
|
|
'returnNamespacedFunctionCallType' => [
|
|
|
|
'files' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A{
|
|
|
|
function doThing() : void {}
|
|
|
|
}',
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
namespace Bar;
|
|
|
|
|
|
|
|
use Foo\A;
|
|
|
|
|
|
|
|
/** @return A */
|
|
|
|
function getThing() {
|
|
|
|
return new A;
|
|
|
|
}',
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file3.php' => '<?php
|
|
|
|
require("file2.php");
|
|
|
|
|
|
|
|
namespace Bat;
|
|
|
|
|
|
|
|
class C {
|
|
|
|
function boop() : void {
|
|
|
|
\Bar\getThing()->doThing();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'files_to_check' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file3.php',
|
|
|
|
],
|
|
|
|
],
|
2017-07-25 22:11:02 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerTestInvalidIncludes()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'undefinedMethodInRequire' => [
|
|
|
|
'files' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
|
|
|
|
require("file1.php");
|
|
|
|
|
|
|
|
class B {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
(new A)->fooFo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
|
|
|
|
class A{
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'files_to_check' => [
|
|
|
|
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
|
|
|
|
],
|
|
|
|
'error_message' => 'UndefinedMethod',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-08 17:24:01 +01:00
|
|
|
}
|
2017-01-08 01:07:58 +01:00
|
|
|
}
|