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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
2018-01-07 22:14:16 +01:00
|
|
|
* @param bool $safe_types
|
2017-09-16 18:45:11 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-07 22:11:51 +01:00
|
|
|
public function testValidCode($input_code, $output_code, $php_version, array $issues_to_fix, $safe_types)
|
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.');
|
|
|
|
}
|
|
|
|
|
2018-02-17 23:15:50 +01:00
|
|
|
$config = new TestConfig();
|
|
|
|
|
|
|
|
if (empty($issues_to_fix)) {
|
|
|
|
$config->addPluginPath('examples/ClassUnqualifier.php');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker(
|
|
|
|
$config,
|
|
|
|
$this->file_provider,
|
2018-02-19 06:27:39 +01:00
|
|
|
new Provider\FakeParserCacheProvider(),
|
|
|
|
new \Psalm\Provider\NoCache\NoFileStorageCacheProvider(),
|
|
|
|
new \Psalm\Provider\NoCache\NoClassLikeStorageCacheProvider()
|
2018-02-17 23:15:50 +01:00
|
|
|
);
|
|
|
|
|
2017-09-16 18:45:11 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->project_checker->setIssuesToFix($keyed_issues_to_fix);
|
2018-01-07 22:11:51 +01:00
|
|
|
$this->project_checker->alterCodeAfterCompletion(
|
|
|
|
(int) $php_major_version,
|
|
|
|
(int) $php_minor_version,
|
|
|
|
false,
|
|
|
|
$safe_types
|
|
|
|
);
|
2018-01-07 06:11:23 +01:00
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile($file_path, $context);
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$this->project_checker->getCodebase()->analyzer->updateFile($file_path, false);
|
2018-01-21 19:38:51 +01:00
|
|
|
$this->assertSame($output_code, $this->project_checker->getCodebase()->getFileContents($file_path));
|
2017-09-16 18:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
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'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 06:11:23 +01:00
|
|
|
],
|
|
|
|
'addMissingVoidReturnType71' => [
|
|
|
|
'<?php
|
|
|
|
function foo() { }',
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): void { }',
|
2018-01-07 06:11:23 +01:00
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 06:11:23 +01:00
|
|
|
],
|
|
|
|
'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'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 06:11:23 +01:00
|
|
|
],
|
|
|
|
'addMissingStringReturnType70' => [
|
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): string {
|
2018-01-07 06:11:23 +01:00
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['MissingReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2017-09-16 18:45:11 +02:00
|
|
|
],
|
2018-01-07 19:21:08 +01:00
|
|
|
'addMissingClosureStringReturnType56' => [
|
|
|
|
'<?php
|
|
|
|
$a = function() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
$a = /**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'5.6',
|
|
|
|
['MissingClosureReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 19:21:08 +01: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'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 06:11:23 +01:00
|
|
|
],
|
2018-01-18 23:16:50 +01:00
|
|
|
'addMissingNullableStringReturnType70' => [
|
2018-01-07 06:11:23 +01:00
|
|
|
'<?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'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
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
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): ?string {
|
2018-01-07 06:11:23 +01:00
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 06:11:23 +01:00
|
|
|
],
|
2018-01-07 19:06:56 +01:00
|
|
|
'addMissingStringReturnTypeWithComment71' => [
|
|
|
|
'<?php
|
|
|
|
function foo() /** : ?string */ {
|
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}',
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): ?string /** : ?string */ {
|
2018-01-07 19:06:56 +01:00
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 19:06:56 +01:00
|
|
|
],
|
2018-01-07 19:14:50 +01:00
|
|
|
'addMissingStringReturnTypeWithSingleLineComment71' => [
|
|
|
|
'<?php
|
|
|
|
function foo()// cool
|
|
|
|
{
|
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}',
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): ?string// cool
|
2018-01-07 19:14:50 +01:00
|
|
|
{
|
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 19:14:50 +01:00
|
|
|
],
|
2018-01-07 06:11:23 +01:00
|
|
|
'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'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 06:11:23 +01:00
|
|
|
],
|
|
|
|
'addMissingStringArrayReturnType70' => [
|
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
return ["hello"];
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*
|
|
|
|
* @psalm-return array{0:string}
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): array {
|
2018-01-07 06:11:23 +01:00
|
|
|
return ["hello"];
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['MissingReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
|
|
|
],
|
2018-03-17 21:53:11 +01:00
|
|
|
'addMissingObjectLikeReturnType70' => [
|
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
return rand(0, 1) ? ["a" => "hello"] : ["a" => "goodbye", "b" => "hello again"];
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*
|
|
|
|
* @psalm-return array{a:string, b?:string}
|
|
|
|
*/
|
|
|
|
function foo(): array {
|
|
|
|
return rand(0, 1) ? ["a" => "hello"] : ["a" => "goodbye", "b" => "hello again"];
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['MissingReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
2018-03-23 06:36:56 +01:00
|
|
|
'addMissingObjectLikeReturnTypeSeparateStatements70' => [
|
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return ["a" => "hello", "b" => "hello again"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return ["a" => "hello", "b" => "hello again"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ["a" => "goodbye"];
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*
|
|
|
|
* @psalm-return array{a:string, b?:string}
|
|
|
|
*/
|
|
|
|
function foo(): array {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return ["a" => "hello", "b" => "hello again"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return ["a" => "hello", "b" => "hello again"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ["a" => "goodbye"];
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['MissingReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
2018-01-07 22:11:51 +01:00
|
|
|
'addMissingStringArrayReturnTypeFromCall71' => [
|
|
|
|
'<?php
|
|
|
|
/** @return string[] */
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): array {
|
2018-01-07 22:11:51 +01:00
|
|
|
return ["hello"];
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar() {
|
|
|
|
return foo();
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/** @return string[] */
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): array {
|
2018-01-07 22:11:51 +01:00
|
|
|
return ["hello"];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*
|
|
|
|
* @psalm-return array<mixed, string>
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar(): array {
|
2018-01-07 22:11:51 +01:00
|
|
|
return foo();
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
'addMissingDocblockStringArrayReturnTypeFromCall71' => [
|
|
|
|
'<?php
|
|
|
|
/** @return string[] */
|
|
|
|
function foo() {
|
|
|
|
return ["hello"];
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar() {
|
|
|
|
return foo();
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/** @return string[] */
|
|
|
|
function foo() {
|
|
|
|
return ["hello"];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*
|
|
|
|
* @psalm-return array<mixed, string>
|
|
|
|
*/
|
|
|
|
function bar() {
|
|
|
|
return foo();
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
'addMissingNullableStringReturnType71' => [
|
|
|
|
'<?php
|
|
|
|
/** @return string[] */
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): array {
|
2018-01-07 22:11:51 +01:00
|
|
|
return ["hello"];
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar() {
|
|
|
|
foreach (foo() as $f) {
|
|
|
|
return $f;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/** @return string[] */
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): array {
|
2018-01-07 22:11:51 +01:00
|
|
|
return ["hello"];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
function bar() {
|
|
|
|
foreach (foo() as $f) {
|
|
|
|
return $f;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
2018-01-10 19:04:37 +01:00
|
|
|
'addMissingNullableStringReturnTypeWithMaybeReturn71' => [
|
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
if (rand(0, 1)) return new stdClass;
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return stdClass|null
|
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
if (rand(0, 1)) return new stdClass;
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
2018-01-07 22:11:51 +01:00
|
|
|
'addMissingUnsafeNullableStringReturnType71' => [
|
|
|
|
'<?php
|
|
|
|
/** @return string[] */
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): array {
|
2018-01-07 22:11:51 +01:00
|
|
|
return ["hello"];
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar() {
|
|
|
|
foreach (foo() as $f) {
|
|
|
|
return $f;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/** @return string[] */
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): array {
|
2018-01-07 22:11:51 +01:00
|
|
|
return ["hello"];
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar(): ?string {
|
2018-01-07 22:11:51 +01:00
|
|
|
foreach (foo() as $f) {
|
|
|
|
return $f;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
false,
|
2018-01-07 06:11:23 +01:00
|
|
|
],
|
2018-01-23 00:00:19 +01:00
|
|
|
'addSelfReturnType' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo(): self {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
false,
|
|
|
|
],
|
2018-01-23 18:03:18 +01:00
|
|
|
'addMissingNullableReturnTypeInDocblockOnly71' => [
|
|
|
|
'<?php
|
|
|
|
function foo() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
function bar() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
'addMissingVoidReturnTypeToOldArray71' => [
|
|
|
|
'<?php
|
|
|
|
function foo(array $a = array()) {}
|
|
|
|
function bar(array $a = array() ) {}',
|
|
|
|
'<?php
|
|
|
|
function foo(array $a = array()): void {}
|
|
|
|
function bar(array $a = array() ): void {}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
'addMissingVoidReturnTypeClosureUse71' => [
|
|
|
|
'<?php
|
|
|
|
$a = "foo";
|
|
|
|
$b = function() use ($a) {};',
|
|
|
|
'<?php
|
|
|
|
$a = "foo";
|
|
|
|
$b = function() use ($a): void {};',
|
|
|
|
'7.1',
|
|
|
|
['MissingClosureReturnType'],
|
|
|
|
false,
|
|
|
|
],
|
2018-01-23 00:00:19 +01:00
|
|
|
'dontAddMissingVoidReturnType56' => [
|
|
|
|
'<?php
|
|
|
|
/** @return void */
|
|
|
|
function foo() { }
|
|
|
|
|
|
|
|
function bar() {
|
|
|
|
return foo();
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/** @return void */
|
|
|
|
function foo() { }
|
|
|
|
|
|
|
|
function bar() {
|
|
|
|
return foo();
|
|
|
|
}',
|
|
|
|
'5.6',
|
|
|
|
['MissingReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
2018-03-23 21:34:45 +01:00
|
|
|
'dontAddMissingVoidReturnTypehintForSubclass71' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo() {}
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function foo() {}
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
'dontAddMissingVoidReturnTypehintForPrivateMethodInSubclass71' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
private function foo() {}
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private function foo(): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
private function foo(): void {}
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
'dontAddMissingClassReturnTypehintForSubclass71' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function foo() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function foo() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
'dontAddMissingClassReturnTypehintForSubSubclass71' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public function foo() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function foo() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
/**
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function foo() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['MissingReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
2018-01-07 06:11:23 +01:00
|
|
|
'fixInvalidIntReturnType56' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'5.6',
|
|
|
|
['InvalidReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 06:11:23 +01:00
|
|
|
],
|
|
|
|
'fixInvalidIntReturnType70' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): int {
|
2018-01-07 06:11:23 +01:00
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): string {
|
2018-01-07 06:11:23 +01:00
|
|
|
return "hello";
|
|
|
|
}',
|
2018-01-07 19:18:06 +01:00
|
|
|
'7.0',
|
|
|
|
['InvalidReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 19:18:06 +01:00
|
|
|
],
|
|
|
|
'fixInvalidIntReturnTypeJustInTypehint70' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): int {
|
2018-01-07 19:18:06 +01:00
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): string {
|
2018-01-07 19:18:06 +01:00
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['InvalidReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 19:18:06 +01:00
|
|
|
],
|
2018-01-08 00:06:31 +01:00
|
|
|
'fixInvalidStringReturnTypeThatIsNotPhpCompatible70' => [
|
2018-01-07 23:17:18 +01:00
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): string {
|
2018-01-07 23:17:18 +01:00
|
|
|
return rand(0, 1) ? "hello" : false;
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string|false
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo() {
|
2018-01-07 23:17:18 +01:00
|
|
|
return rand(0, 1) ? "hello" : false;
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['InvalidFalsableReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
2018-01-08 00:06:31 +01:00
|
|
|
'fixInvalidIntReturnTypeThatIsNotPhpCompatible70' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): string {
|
2018-01-08 00:06:31 +01:00
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo() {
|
2018-01-08 00:06:31 +01:00
|
|
|
return rand(0, 1) ? "hello" : null;
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['InvalidNullableReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
2018-01-07 19:18:06 +01:00
|
|
|
'fixInvalidIntReturnTypeJustInTypehintWithComment70' => [
|
|
|
|
'<?php
|
|
|
|
function foo() /** cool : beans */ : int /** cool : beans */ {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
function foo() /** cool : beans */ : string /** cool : beans */ {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['InvalidReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 19:18:06 +01:00
|
|
|
],
|
|
|
|
'fixInvalidIntReturnTypeJustInTypehintWithSingleLineComment70' => [
|
|
|
|
'<?php
|
|
|
|
function foo() // hello
|
|
|
|
: int {
|
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
function foo() // hello
|
|
|
|
: string {
|
|
|
|
return "hello";
|
|
|
|
}',
|
2018-01-07 06:11:23 +01:00
|
|
|
'7.0',
|
|
|
|
['InvalidReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2017-09-16 18:45:11 +02:00
|
|
|
],
|
2018-01-07 16:23:02 +01:00
|
|
|
'fixMismatchingDocblockReturnType70' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): string {
|
2018-01-07 16:23:02 +01:00
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): string {
|
2018-01-07 16:23:02 +01:00
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['MismatchingDocblockReturnType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 16:23:02 +01:00
|
|
|
],
|
|
|
|
'fixMismatchingDocblockParamType70' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param int $s
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(string $s): string {
|
2018-01-07 16:23:02 +01:00
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string $s
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(string $s): string {
|
2018-01-07 16:23:02 +01:00
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['MismatchingDocblockParamType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 16:23:02 +01:00
|
|
|
],
|
2018-01-07 18:53:25 +01:00
|
|
|
'fixNamespacedMismatchingDocblockParamsType70' => [
|
2018-01-07 18:38:01 +01:00
|
|
|
'<?php
|
|
|
|
namespace Foo\Bar {
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @param \B $b
|
2018-01-07 18:53:25 +01:00
|
|
|
* @param \C $c
|
2018-01-07 18:38:01 +01:00
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(B $b, C $c): string {
|
2018-01-07 18:38:01 +01:00
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B {}
|
2018-01-07 18:53:25 +01:00
|
|
|
class C {}
|
2018-01-07 18:38:01 +01:00
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
namespace Foo\Bar {
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @param B $b
|
2018-01-07 18:53:25 +01:00
|
|
|
* @param C $c
|
2018-01-07 18:38:01 +01:00
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(B $b, C $c): string {
|
2018-01-07 18:38:01 +01:00
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B {}
|
2018-01-07 18:53:25 +01:00
|
|
|
class C {}
|
2018-01-07 18:38:01 +01:00
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['MismatchingDocblockParamType'],
|
2018-01-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-07 18:38:01 +01:00
|
|
|
],
|
2018-01-10 04:33:36 +01:00
|
|
|
'preserveFormat' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
2018-01-29 19:42:04 +01:00
|
|
|
* Here is a paragraph
|
|
|
|
*
|
|
|
|
* And another one
|
|
|
|
*
|
2018-01-10 04:33:36 +01:00
|
|
|
* @other is
|
|
|
|
* a friend of mine
|
|
|
|
* + Members
|
|
|
|
* - `google`
|
|
|
|
* @return int
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): int {
|
2018-01-10 04:33:36 +01:00
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
2018-01-29 19:42:04 +01:00
|
|
|
* Here is a paragraph
|
|
|
|
*
|
|
|
|
* And another one
|
|
|
|
*
|
2018-01-10 04:33:36 +01:00
|
|
|
* @other is
|
|
|
|
* a friend of mine
|
|
|
|
* + Members
|
|
|
|
* - `google`
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): string {
|
2018-01-10 04:33:36 +01:00
|
|
|
return "hello";
|
|
|
|
}',
|
|
|
|
'7.0',
|
|
|
|
['InvalidReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
2018-01-21 22:24:20 +01:00
|
|
|
'possiblyUndefinedVariable' => [
|
|
|
|
'<?php
|
|
|
|
$flag = rand(0, 1);
|
|
|
|
$otherflag = rand(0, 1);
|
|
|
|
$yetanotherflag = rand(0, 1);
|
|
|
|
|
|
|
|
if ($flag) {
|
|
|
|
if ($otherflag) {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($flag) {
|
|
|
|
if ($yetanotherflag) {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
$flag = rand(0, 1);
|
|
|
|
$otherflag = rand(0, 1);
|
|
|
|
$yetanotherflag = rand(0, 1);
|
|
|
|
|
|
|
|
$a = null;
|
|
|
|
if ($flag) {
|
|
|
|
if ($otherflag) {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($flag) {
|
|
|
|
if ($yetanotherflag) {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
}',
|
|
|
|
'5.6',
|
2018-01-22 19:54:32 +01:00
|
|
|
['PossiblyUndefinedGlobalVariable'],
|
2018-01-21 22:24:20 +01:00
|
|
|
true,
|
|
|
|
],
|
2018-01-22 03:45:56 +01:00
|
|
|
'twoPossiblyUndefinedVariables' => [
|
2018-01-22 03:24:22 +01:00
|
|
|
'<?php
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 1;
|
|
|
|
$b = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
echo $b;',
|
|
|
|
'<?php
|
|
|
|
$a = null;
|
2018-01-22 04:01:02 +01:00
|
|
|
$b = null;
|
2018-01-22 03:24:22 +01:00
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 1;
|
|
|
|
$b = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
echo $b;',
|
|
|
|
'5.6',
|
2018-01-22 19:54:32 +01:00
|
|
|
['PossiblyUndefinedGlobalVariable'],
|
2018-01-22 03:24:22 +01:00
|
|
|
true,
|
|
|
|
],
|
2018-01-22 04:05:57 +01:00
|
|
|
'possiblyUndefinedVariableInElse' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
'<?php
|
|
|
|
$a = null;
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
'5.6',
|
2018-01-22 19:54:32 +01:00
|
|
|
['PossiblyUndefinedGlobalVariable'],
|
2018-01-22 04:05:57 +01:00
|
|
|
true,
|
|
|
|
],
|
2018-02-10 16:30:08 +01:00
|
|
|
'unsetPossiblyUndefinedVariable' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = "bar";
|
|
|
|
}
|
|
|
|
unset($a);',
|
|
|
|
'<?php
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = "bar";
|
|
|
|
}
|
|
|
|
unset($a);',
|
|
|
|
'5.6',
|
|
|
|
['PossiblyUndefinedGlobalVariable'],
|
|
|
|
true,
|
|
|
|
],
|
2018-03-07 19:11:36 +01:00
|
|
|
'addLessSpecificArrayReturnType71' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class C {}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace C {
|
|
|
|
use A\B;
|
|
|
|
|
|
|
|
class D {
|
|
|
|
public function getArrayOfC(): array {
|
|
|
|
return [new \A\B\C];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class C {}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace C {
|
|
|
|
use A\B;
|
|
|
|
|
|
|
|
class D {
|
|
|
|
/**
|
|
|
|
* @return \A\B\C[]
|
|
|
|
*
|
|
|
|
* @psalm-return array{0:\A\B\C}
|
|
|
|
*/
|
|
|
|
public function getArrayOfC(): array {
|
|
|
|
return [new \A\B\C];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['LessSpecificReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
'fixLessSpecificReturnType' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public function getB(): ?\A {
|
|
|
|
return new B;
|
|
|
|
}
|
|
|
|
public function getC(): ?\A {
|
|
|
|
return new C;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public function getB(): B {
|
|
|
|
return new B;
|
|
|
|
}
|
|
|
|
public function getC(): self {
|
|
|
|
return new C;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'7.1',
|
|
|
|
['LessSpecificReturnType'],
|
|
|
|
true,
|
|
|
|
],
|
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-07 22:11:51 +01:00
|
|
|
true,
|
2018-01-02 05:18:49 +01:00
|
|
|
],
|
2017-09-16 18:45:11 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|