2017-09-16 18:45:11 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class FileManipulationTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
FileChecker::clearCache();
|
|
|
|
\Psalm\FileManipulation\FunctionDocblockManipulator::clearCache();
|
|
|
|
|
|
|
|
$this->file_provider = new Provider\FakeFileProvider();
|
|
|
|
|
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker(
|
|
|
|
$this->file_provider,
|
2017-10-15 17:57:44 +02:00
|
|
|
new Provider\FakeParserCacheProvider()
|
2017-09-16 18:45:11 +02:00
|
|
|
);
|
|
|
|
|
2018-01-02 05:18:49 +01:00
|
|
|
if (!self::$config) {
|
|
|
|
self::$config = new TestConfig();
|
|
|
|
self::$config->addPluginPath('examples/ClassUnqualifier.php');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->project_checker->setConfig(self::$config);
|
2017-09-16 18:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerFileCheckerValidCodeParse
|
|
|
|
*
|
|
|
|
* @param string $input_code
|
|
|
|
* @param string $output_code
|
2018-01-07 06:11:23 +01:00
|
|
|
* @param string $php_version
|
|
|
|
* @param string[] $issues_to_fix
|
2017-09-16 18:45:11 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-07 06:11:23 +01:00
|
|
|
public function testValidCode($input_code, $output_code, $php_version, array $issues_to_fix)
|
2017-09-16 18:45:11 +02:00
|
|
|
{
|
|
|
|
$test_name = $this->getName();
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
2018-01-07 06:11:23 +01:00
|
|
|
list($php_major_version, $php_minor_version) = explode('.', $php_version);
|
|
|
|
|
|
|
|
$keyed_issues_to_fix = [];
|
|
|
|
|
|
|
|
foreach ($issues_to_fix as $issue) {
|
|
|
|
$keyed_issues_to_fix[$issue] = true;
|
|
|
|
}
|
|
|
|
|
2017-09-16 18:45:11 +02:00
|
|
|
$file_checker = new FileChecker($file_path, $this->project_checker);
|
2018-01-07 06:11:23 +01:00
|
|
|
|
|
|
|
$this->project_checker->setIssuesToFix($keyed_issues_to_fix);
|
|
|
|
$this->project_checker->alterCodeAfterCompletion((int) $php_major_version, (int) $php_minor_version);
|
|
|
|
|
2017-09-16 18:45:11 +02:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2018-01-07 17:48:33 +01:00
|
|
|
$this->project_checker->updateFile($file_path, false);
|
2017-09-16 18:45:11 +02:00
|
|
|
$this->assertSame($output_code, $this->project_checker->getFileContents($file_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerValidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
2018-01-07 06:11:23 +01:00
|
|
|
'addMissingVoidReturnType56' => [
|
2017-09-16 18:45:11 +02:00
|
|
|
'<?php
|
|
|
|
function foo() { }',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function foo() { }',
|
2018-01-07 06:11:23 +01:00
|
|
|
'5.6',
|
|
|
|
['MissingReturnType'],
|
2017-09-16 18:45:11 +02:00
|
|
|
],
|
2018-01-07 06:11:23 +01:00
|
|
|
'addMissingVoidReturnType70' => [
|
|
|
|
'<?php
|
|
|
|
function foo() { }',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function foo() { }',
|
|
|
|
'7.0',
|
|
|
|
['MissingReturnType'],
|
|
|
|
],
|
|
|
|
'addMissingVoidReturnType71' => [
|
|
|
|
'<?php
|
|
|
|
function foo() { }',
|
|
|
|
'<?php
|
|
|
|
function foo() : void { }',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
],
|
|
|
|
'addMissingStringReturnType56' => [
|
2017-09-16 18:45:11 +02:00
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
2018-01-07 06:11:23 +01:00
|
|
|
'5.6',
|
|
|
|
['MissingReturnType'],
|
|
|
|
],
|
|
|
|
'addMissingStringReturnType70' => [
|
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
function foo() : string {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['MissingReturnType'],
|
2017-09-16 18:45:11 +02:00
|
|
|
],
|
2018-01-07 06:11:23 +01:00
|
|
|
'addMissingNullableStringReturnType56' => [
|
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}',
|
2017-09-16 18:45:11 +02:00
|
|
|
'<?php
|
|
|
|
/**
|
2018-01-07 06:11:23 +01:00
|
|
|
* @return string|null
|
2017-09-16 18:45:11 +02:00
|
|
|
*/
|
|
|
|
function foo() {
|
2018-01-07 06:11:23 +01:00
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}',
|
|
|
|
'5.6',
|
|
|
|
['MissingReturnType'],
|
|
|
|
],
|
|
|
|
'addMissingStringReturnType70' => [
|
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
return rand(0, 1) ? "hello" : null;
|
2017-09-16 18:45:11 +02:00
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
2018-01-07 06:11:23 +01:00
|
|
|
* @return string|null
|
2017-09-16 18:45:11 +02:00
|
|
|
*/
|
|
|
|
function foo() {
|
2018-01-07 06:11:23 +01:00
|
|
|
return rand(0, 1) ? "hello" : null;
|
2017-09-16 18:45:11 +02:00
|
|
|
}',
|
2018-01-07 06:11:23 +01:00
|
|
|
'7.0',
|
|
|
|
['MissingReturnType'],
|
2017-09-16 18:45:11 +02:00
|
|
|
],
|
2018-01-07 06:11:23 +01:00
|
|
|
'addMissingStringReturnType71' => [
|
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
function foo() : ?string {
|
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
],
|
|
|
|
'addMissingStringArrayReturnType56' => [
|
2017-09-16 18:45:11 +02:00
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
return ["hello"];
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
2018-01-07 06:11:23 +01:00
|
|
|
* @return string[]
|
|
|
|
*
|
2017-12-19 00:47:17 +01:00
|
|
|
* @psalm-return array{0:string}
|
2017-09-16 18:45:11 +02:00
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
return ["hello"];
|
|
|
|
}',
|
2018-01-07 06:11:23 +01:00
|
|
|
'5.6',
|
|
|
|
['MissingReturnType'],
|
|
|
|
],
|
|
|
|
'addMissingStringArrayReturnType70' => [
|
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
return ["hello"];
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*
|
|
|
|
* @psalm-return array{0:string}
|
|
|
|
*/
|
|
|
|
function foo() : array {
|
|
|
|
return ["hello"];
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['MissingReturnType'],
|
|
|
|
],
|
|
|
|
'fixInvalidIntReturnType56' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'5.6',
|
|
|
|
['InvalidReturnType'],
|
|
|
|
],
|
|
|
|
'fixInvalidIntReturnType70' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function foo() : int {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function foo() : string {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['InvalidReturnType'],
|
2017-09-16 18:45:11 +02:00
|
|
|
],
|
2018-01-07 16:23:02 +01:00
|
|
|
'fixMismatchingDocblockReturnType70' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function foo() : string {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function foo() : string {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['MismatchingDocblockReturnType'],
|
|
|
|
],
|
|
|
|
'fixMismatchingDocblockParamType70' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param int $s
|
|
|
|
*/
|
|
|
|
function foo(string $s) : string {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string $s
|
|
|
|
*/
|
|
|
|
function foo(string $s) : string {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['MismatchingDocblockParamType'],
|
|
|
|
],
|
2018-01-07 18:38:01 +01:00
|
|
|
'fixNamespacedMismatchingDocblockParamType70' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo\Bar {
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @param \B $b
|
|
|
|
*/
|
|
|
|
function foo(B $b) : string {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B {}
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
namespace Foo\Bar {
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @param B $b
|
|
|
|
*/
|
|
|
|
function foo(B $b) : string {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B {}
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['MismatchingDocblockParamType'],
|
|
|
|
],
|
2018-01-02 05:18:49 +01:00
|
|
|
'useUnqualifierPlugin' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B\C {
|
|
|
|
class D {}
|
|
|
|
}
|
|
|
|
namespace Foo\Bar {
|
|
|
|
use A\B\C\D;
|
|
|
|
|
|
|
|
new \A\B\C\D();
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
namespace A\B\C {
|
|
|
|
class D {}
|
|
|
|
}
|
|
|
|
namespace Foo\Bar {
|
|
|
|
use A\B\C\D;
|
|
|
|
|
|
|
|
new D();
|
|
|
|
}',
|
2018-01-07 06:11:23 +01:00
|
|
|
PHP_VERSION,
|
|
|
|
[],
|
2018-01-02 05:18:49 +01:00
|
|
|
],
|
2017-09-16 18:45:11 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|