2018-06-30 21:29:37 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2019-03-23 19:27:54 +01:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
2018-11-12 16:57:05 +01:00
|
|
|
use Psalm\Tests\Internal\Provider;
|
2018-06-30 21:29:37 +02:00
|
|
|
|
|
|
|
class StubTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @var TestConfig */
|
|
|
|
protected static $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$config = new TestConfig();
|
|
|
|
|
|
|
|
if (!defined('PSALM_VERSION')) {
|
|
|
|
define('PSALM_VERSION', '2.0.0');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined('PHP_PARSER_VERSION')) {
|
|
|
|
define('PHP_PARSER_VERSION', '4.0.0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
FileAnalyzer::clearCache();
|
2018-06-30 21:29:37 +02:00
|
|
|
$this->file_provider = new Provider\FakeFileProvider();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Config $config
|
|
|
|
*
|
2018-11-06 03:57:36 +01:00
|
|
|
* @return \Psalm\Internal\Analyzer\ProjectAnalyzer
|
2018-06-30 21:29:37 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
private function getProjectAnalyzerWithConfig(Config $config)
|
2018-06-30 21:29:37 +02:00
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer = new \Psalm\Internal\Analyzer\ProjectAnalyzer(
|
2018-06-30 21:29:37 +02:00
|
|
|
$config,
|
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-06-30 21:29:37 +02:00
|
|
|
);
|
2019-02-07 21:27:43 +01:00
|
|
|
$project_analyzer->setPhpVersion('7.3');
|
2018-07-03 17:17:52 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$config->visitComposerAutoloadFiles($project_analyzer, false);
|
2018-07-03 17:17:52 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
return $project_analyzer;
|
2018-06-30 21:29:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\ConfigException
|
|
|
|
* @expectedExceptionMessage Cannot resolve stubfile path
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNonexistentStubFile()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
Config::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
|
|
|
<file name="stubs/invalidfile.php" />
|
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-03-25 05:07:32 +01:00
|
|
|
public function testStubFileClass()
|
2018-06-30 21:29:37 +02:00
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/systemclass.php" />
|
2018-06-30 21:29:37 +02:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
2019-03-25 05:07:32 +01:00
|
|
|
namespace A\B\C;
|
2018-06-30 21:29:37 +02:00
|
|
|
|
2019-03-25 05:07:32 +01:00
|
|
|
$a = new \SystemClass();
|
2018-06-30 21:29:37 +02:00
|
|
|
$b = $a->foo(5, "hello");
|
2019-03-25 05:07:32 +01:00
|
|
|
$c = \SystemClass::bar(5, "hello");
|
|
|
|
echo \SystemClass::HELLO;'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStubFileConstant()
|
|
|
|
{
|
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/systemclass.php" />
|
2019-03-25 05:07:32 +01:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
namespace A\B\C;
|
|
|
|
|
|
|
|
$d = ROOT_CONST_CONSTANT;
|
|
|
|
$e = \ROOT_CONST_CONSTANT;
|
|
|
|
$f = ROOT_DEFINE_CONSTANT;
|
|
|
|
$g = \ROOT_DEFINE_CONSTANT;'
|
2018-06-30 21:29:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
2019-02-16 17:16:52 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPhpStormMetaParsingFile()
|
|
|
|
{
|
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
2019-02-17 00:50:25 +01:00
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/phpstorm.meta.php" />
|
2019-02-17 00:50:25 +01:00
|
|
|
</stubs>
|
2019-02-16 17:16:52 +01:00
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
namespace Ns {
|
|
|
|
class MyClass {
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @psalm-suppress InvalidReturnType
|
|
|
|
*/
|
|
|
|
public function create(string $s) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $s
|
|
|
|
* @return mixed
|
|
|
|
* @psalm-suppress InvalidReturnType
|
|
|
|
*/
|
|
|
|
public function foo($s) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @psalm-suppress InvalidReturnType
|
|
|
|
*/
|
|
|
|
public function bar(array $a) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
namespace {
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @psalm-suppress InvalidReturnType
|
|
|
|
*/
|
|
|
|
function create(string $s) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $s
|
|
|
|
* @return mixed
|
|
|
|
* @psalm-suppress InvalidReturnType
|
|
|
|
*/
|
|
|
|
function foo($s) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @psalm-suppress InvalidReturnType
|
|
|
|
*/
|
|
|
|
function bar(array $a) {}
|
|
|
|
|
|
|
|
$a1 = (new \Ns\MyClass)->create("object");
|
|
|
|
$a2 = (new \Ns\MyClass)->create("exception");
|
|
|
|
|
|
|
|
$b1 = \create("object");
|
|
|
|
$b2 = \create("exception");
|
|
|
|
|
2019-02-16 18:16:34 +01:00
|
|
|
$e2 = \create(\LogicException::class);
|
|
|
|
|
2019-02-16 17:16:52 +01:00
|
|
|
$c1 = (new \Ns\MyClass)->foo(5);
|
|
|
|
$c2 = (new \Ns\MyClass)->bar(["hello"]);
|
|
|
|
|
|
|
|
$d1 = \foo(5);
|
|
|
|
$d2 = \bar(["hello"]);
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
$this->analyzeFile($file_path, $context);
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:29:37 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNamespacedStubClass()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/namespaced_class.php" />
|
2018-06-30 21:29:37 +02:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
$a = new Foo\SystemClass();
|
|
|
|
echo Foo\SystemClass::HELLO;
|
|
|
|
|
|
|
|
$b = $a->foo(5, "hello");
|
2018-08-08 22:13:37 +02:00
|
|
|
$c = Foo\SystemClass::bar(5, "hello");
|
|
|
|
|
|
|
|
echo Foo\BAR;'
|
2018-06-30 21:29:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-12-01 01:21:14 +01:00
|
|
|
public function testStubRegularFunction()
|
2018-06-30 21:29:37 +02:00
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/custom_functions.php" />
|
2018-06-30 21:29:37 +02:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
echo barBar("hello");'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
2018-12-01 01:21:14 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStubVariadicFunction()
|
|
|
|
{
|
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/custom_functions.php" />
|
2018-12-01 01:21:14 +01:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
variadic("bat", "bam");'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidScalarArgument
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStubVariadicFunctionWrongArgType()
|
|
|
|
{
|
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/custom_functions.php" />
|
2018-12-01 01:21:14 +01:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
variadic("bat", 5);'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage TooManyArguments
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testUserVariadicWithFalseVariadic()
|
|
|
|
{
|
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string ...$bar
|
|
|
|
*/
|
|
|
|
function variadic() : void {}
|
|
|
|
variadic("hello");'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:29:37 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPolyfilledFunction()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm
|
2019-05-10 00:23:14 +02:00
|
|
|
autoloader="tests/fixtures/stubs/polyfill.php"
|
2018-06-30 21:29:37 +02:00
|
|
|
>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
2018-07-03 17:17:52 +02:00
|
|
|
$a = random_bytes(16);
|
|
|
|
$b = new_random_bytes(16);'
|
2018-06-30 21:29:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
2018-11-29 06:05:56 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testClassAlias()
|
|
|
|
{
|
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm
|
2019-05-10 00:23:14 +02:00
|
|
|
autoloader="tests/fixtures/stubs/class_alias.php"
|
2018-11-29 06:05:56 +01:00
|
|
|
>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
2018-12-21 07:21:00 +01:00
|
|
|
namespace ClassAliasStubTest;
|
|
|
|
|
2018-11-29 06:05:56 +01:00
|
|
|
function foo(A $a) : void {}
|
|
|
|
|
|
|
|
foo(new B());
|
2018-11-29 06:46:34 +01:00
|
|
|
foo(new C());
|
2018-11-29 06:05:56 +01:00
|
|
|
|
|
|
|
function bar(B $b) : void {}
|
|
|
|
|
|
|
|
bar(new A());
|
|
|
|
|
|
|
|
$a = new B();
|
|
|
|
|
|
|
|
echo $a->foo;
|
|
|
|
|
2018-12-05 16:36:42 +01:00
|
|
|
echo $a->bar("hello");
|
|
|
|
|
|
|
|
function f(): A {
|
|
|
|
return new A;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAliased(): B {
|
|
|
|
return f();
|
2018-12-21 07:21:00 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 15:29:23 +01:00
|
|
|
$d = new D();
|
|
|
|
|
2018-12-21 17:39:28 +01:00
|
|
|
D::bat();
|
|
|
|
$d::bat();
|
|
|
|
|
2018-12-21 15:29:23 +01:00
|
|
|
class E implements IAlias {}'
|
2018-11-29 06:05:56 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:29:37 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStubFunctionWithFunctionExists()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/custom_functions.php" />
|
2018-06-30 21:29:37 +02:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
function_exists("fooBar");
|
|
|
|
echo barBar("hello");'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNamespacedStubFunctionWithFunctionExists()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/custom_functions.php" />
|
2018-06-30 21:29:37 +02:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
namespace A;
|
|
|
|
function_exists("fooBar");
|
|
|
|
echo barBar("hello");'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2019-02-27 22:00:44 +01:00
|
|
|
* @expectedExceptionMessage UndefinedFunction - /src/somefile.php:2:22 - Function barBar does not exist
|
2018-06-30 21:29:37 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNoStubFunction()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
echo barBar("hello");'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNamespacedStubFunction()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/namespaced_functions.php" />
|
2018-06-30 21:29:37 +02:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
echo Foo\barBar("hello");'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testConditionalNamespacedStubFunction()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/conditional_namespaced_functions.php" />
|
2018-06-30 21:29:37 +02:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
echo Foo\barBar("hello");'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStubFileWithExistingClassDefinition()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/DomainException.php" />
|
2018-06-30 21:29:37 +02:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
2019-03-17 17:46:48 +01:00
|
|
|
$a = new DomainException(5);'
|
2018-06-30 21:29:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStubFileWithPartialClassDefinitionWithMoreMethods()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/partial_class.php" />
|
2018-06-30 21:29:37 +02:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class PartiallyStubbedClass {
|
|
|
|
/**
|
|
|
|
* @param string $a
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function foo(string $a) {
|
|
|
|
return new self;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bar(int $i) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
(new PartiallyStubbedClass())->foo(A::class);
|
|
|
|
(new PartiallyStubbedClass())->bar(5);'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
2019-04-21 17:30:42 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStubFileWithExtendedStubbedClass()
|
|
|
|
{
|
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/partial_class.php" />
|
2019-04-21 17:30:42 +02:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class Bar extends PartiallyStubbedClass {}
|
|
|
|
|
|
|
|
new Bar();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:29:37 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage TypeCoercion
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStubFileWithPartialClassDefinitionWithCoercion()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/partial_class.php" />
|
2018-06-30 21:29:37 +02:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class PartiallyStubbedClass {
|
|
|
|
/**
|
|
|
|
* @param string $a
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function foo(string $a) {
|
|
|
|
return new self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new PartiallyStubbedClass())->foo("dasda");'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidReturnStatement
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStubFileWithPartialClassDefinitionGeneralReturnType()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
|
2018-06-30 21:29:37 +02:00
|
|
|
TestConfig::loadFromXML(
|
|
|
|
dirname(__DIR__),
|
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
|
|
|
|
<stubs>
|
2019-05-10 00:23:14 +02:00
|
|
|
<file name="tests/fixtures/stubs/partial_class.php" />
|
2018-06-30 21:29:37 +02:00
|
|
|
</stubs>
|
|
|
|
</psalm>'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_path = getcwd() . '/src/somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class PartiallyStubbedClass {
|
|
|
|
/**
|
|
|
|
* @param string $a
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function foo(string $a) {
|
|
|
|
return new \stdClass;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, new Context());
|
|
|
|
}
|
|
|
|
}
|