1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/FileManipulationTest.php

723 lines
22 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
use Psalm\Checker\FileChecker;
use Psalm\Context;
class FileManipulationTest extends TestCase
{
/** @var \Psalm\Checker\ProjectChecker */
protected $project_checker;
/** @var TestConfig|null */
private static $config;
/**
* @return void
*/
public function setUp()
{
FileChecker::clearCache();
\Psalm\FileManipulation\FunctionDocblockManipulator::clearCache();
$this->file_provider = new Provider\FakeFileProvider();
if (!self::$config) {
self::$config = new TestConfig();
self::$config->addPluginPath('examples/ClassUnqualifier.php');
}
$this->project_checker = new \Psalm\Checker\ProjectChecker(
self::$config,
$this->file_provider,
new Provider\FakeParserCacheProvider()
);
}
/**
* @dataProvider providerFileCheckerValidCodeParse
*
* @param string $input_code
* @param string $output_code
* @param string $php_version
* @param string[] $issues_to_fix
2018-01-07 22:14:16 +01:00
* @param bool $safe_types
*
* @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)
{
$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
);
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
);
$this->analyzeFile($file_path, $context);
$this->project_checker->getCodebase()->updateFile($file_path, false);
$this->assertSame($output_code, $this->project_checker->getCodebase()->getFileContents($file_path));
}
/**
* @return array
*/
public function providerFileCheckerValidCodeParse()
{
return [
'addMissingVoidReturnType56' => [
'<?php
function foo() { }',
'<?php
/**
* @return void
*/
function foo() { }',
'5.6',
['MissingReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
'addMissingVoidReturnType70' => [
'<?php
function foo() { }',
'<?php
/**
* @return void
*/
function foo() { }',
'7.0',
['MissingReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
'addMissingVoidReturnType71' => [
'<?php
function foo() { }',
'<?php
2018-01-11 21:50:45 +01:00
function foo(): void { }',
'7.1',
['MissingReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
'addMissingStringReturnType56' => [
'<?php
function foo() {
return "hello";
}',
'<?php
/**
* @return string
*/
function foo() {
return "hello";
}',
'5.6',
['MissingReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
'addMissingStringReturnType70' => [
'<?php
function foo() {
return "hello";
}',
'<?php
2018-01-11 21:50:45 +01:00
function foo(): string {
return "hello";
}',
'7.0',
['MissingReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
'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,
],
'addMissingNullableStringReturnType56' => [
'<?php
function foo() {
return rand(0, 1) ? "hello" : null;
}',
'<?php
/**
* @return string|null
*/
function foo() {
return rand(0, 1) ? "hello" : null;
}',
'5.6',
['MissingReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
'addMissingNullableStringReturnType70' => [
'<?php
function foo() {
return rand(0, 1) ? "hello" : null;
}',
'<?php
/**
* @return string|null
*/
function foo() {
return rand(0, 1) ? "hello" : null;
}',
'7.0',
['MissingReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
'addMissingStringReturnType71' => [
'<?php
function foo() {
return rand(0, 1) ? "hello" : null;
}',
'<?php
2018-01-11 21:50:45 +01:00
function foo(): ?string {
return rand(0, 1) ? "hello" : null;
}',
'7.1',
['MissingReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
'addMissingStringReturnTypeWithComment71' => [
'<?php
function foo() /** : ?string */ {
return rand(0, 1) ? "hello" : null;
}',
'<?php
2018-01-11 21:50:45 +01:00
function foo(): ?string /** : ?string */ {
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
'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
],
'addMissingStringArrayReturnType56' => [
'<?php
function foo() {
return ["hello"];
}',
'<?php
/**
* @return string[]
*
* @psalm-return array{0:string}
*/
function foo() {
return ["hello"];
}',
'5.6',
['MissingReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
'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 {
return ["hello"];
}',
'7.0',
['MissingReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
'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,
],
'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,
],
'fixInvalidIntReturnType70' => [
'<?php
/**
* @return int
*/
2018-01-11 21:50:45 +01:00
function foo(): int {
return "hello";
}',
'<?php
/**
* @return string
*/
2018-01-11 21:50:45 +01:00
function foo(): string {
return "hello";
}',
'7.0',
['InvalidReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
'fixInvalidIntReturnTypeJustInTypehint70' => [
'<?php
2018-01-11 21:50:45 +01:00
function foo(): int {
return "hello";
}',
'<?php
2018-01-11 21:50:45 +01:00
function foo(): string {
return "hello";
}',
'7.0',
['InvalidReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
'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,
],
'fixInvalidIntReturnTypeThatIsNotPhpCompatible70' => [
'<?php
2018-01-11 21:50:45 +01:00
function foo(): string {
return rand(0, 1) ? "hello" : null;
}',
'<?php
/**
* @return string|null
*/
2018-01-11 21:50:45 +01:00
function foo() {
return rand(0, 1) ? "hello" : null;
}',
'7.0',
['InvalidNullableReturnType'],
true,
],
'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,
],
'fixInvalidIntReturnTypeJustInTypehintWithSingleLineComment70' => [
'<?php
function foo() // hello
: int {
return "hello";
}',
'<?php
function foo() // hello
: string {
return "hello";
}',
'7.0',
['InvalidReturnType'],
2018-01-07 22:11:51 +01:00
true,
],
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' => [
'<?php
namespace Foo\Bar {
class A {
/**
* @param \B $b
2018-01-07 18:53:25 +01:00
* @param \C $c
*/
2018-01-11 21:50:45 +01:00
function foo(B $b, C $c): string {
return "hello";
}
}
class B {}
2018-01-07 18:53:25 +01:00
class C {}
}',
'<?php
namespace Foo\Bar {
class A {
/**
* @param B $b
2018-01-07 18:53:25 +01:00
* @param C $c
*/
2018-01-11 21:50:45 +01:00
function foo(B $b, C $c): string {
return "hello";
}
}
class B {}
2018-01-07 18:53:25 +01:00
class C {}
}',
'7.0',
['MismatchingDocblockParamType'],
2018-01-07 22:11:51 +01:00
true,
],
'preserveFormat' => [
'<?php
/**
* @other is
* a friend of mine
* + Members
* - `google`
* @return int
*/
2018-01-11 21:50:45 +01:00
function foo(): int {
return "hello";
}',
'<?php
/**
* @other is
* a friend of mine
* + Members
* - `google`
*
* @return string
*/
2018-01-11 21:50:45 +01:00
function foo(): string {
return "hello";
}',
'7.0',
['InvalidReturnType'],
true,
],
'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',
['PossiblyUndefinedVariable'],
true,
],
2018-01-22 03:45:56 +01:00
'twoPossiblyUndefinedVariables' => [
'<?php
if (rand(0, 1)) {
$a = 1;
$b = 2;
}
echo $a;
echo $b;',
'<?php
$b = null;
$a = null;
if (rand(0, 1)) {
$a = 1;
$b = 2;
}
echo $a;
echo $b;',
'5.6',
['PossiblyUndefinedVariable'],
true,
],
'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();
}',
PHP_VERSION,
[],
2018-01-07 22:11:51 +01:00
true,
],
];
}
}