1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 02:47:02 +01:00
psalm/tests/FileUpdates/ErrorAfterUpdateTest.php

952 lines
36 KiB
PHP
Raw Normal View History

2018-10-10 22:24:10 +02:00
<?php
namespace Psalm\Tests\FileUpdates;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\Providers;
use Psalm\Tests\Internal\Provider;
2019-03-23 19:27:54 +01:00
use Psalm\Tests\TestConfig;
2018-10-10 22:24:10 +02:00
2021-06-08 04:55:21 +02:00
use function array_keys;
use function array_pop;
use function getcwd;
use function preg_quote;
use const DIRECTORY_SEPARATOR;
2018-10-10 22:24:10 +02:00
class ErrorAfterUpdateTest extends \Psalm\Tests\TestCase
{
2019-05-17 00:36:36 +02:00
public function setUp() : void
2018-10-10 22:24:10 +02:00
{
parent::setUp();
$this->file_provider = new \Psalm\Tests\Internal\Provider\FakeFileProvider();
2018-10-10 22:24:10 +02:00
$config = new TestConfig();
$providers = new Providers(
$this->file_provider,
new \Psalm\Tests\Internal\Provider\ParserInstanceCacheProvider(),
2018-10-10 22:24:10 +02:00
null,
null,
new Provider\FakeFileReferenceCacheProvider(),
new \Psalm\Tests\Internal\Provider\ProjectCacheProvider()
2018-10-10 22:24:10 +02:00
);
2018-11-11 18:01:14 +01:00
$this->project_analyzer = new ProjectAnalyzer(
2018-10-10 22:24:10 +02:00
$config,
$providers
2018-10-10 22:24:10 +02:00
);
2019-02-07 21:27:43 +01:00
$this->project_analyzer->setPhpVersion('7.3');
2018-10-10 22:24:10 +02:00
}
/**
* @dataProvider providerTestInvalidUpdates
*
* @param array<int, array<string, string>> $file_stages
* @param array<string, string> $error_levels
*
*/
public function testErrorAfterUpdate(
array $file_stages,
string $error_message,
array $error_levels = []
): void {
2018-11-11 18:01:14 +01:00
$this->project_analyzer->getCodebase()->diff_methods = true;
$this->project_analyzer->getCodebase()->reportUnusedCode();
2018-10-10 22:24:10 +02:00
2018-11-11 18:01:14 +01:00
$codebase = $this->project_analyzer->getCodebase();
2018-10-10 22:24:10 +02:00
$config = $codebase->config;
foreach ($error_levels as $error_type => $error_level) {
$config->setCustomErrorLevel($error_type, $error_level);
}
if (!$file_stages) {
throw new \UnexpectedValueException('$file_stages should not be empty');
}
2018-10-10 22:24:10 +02:00
$end_files = array_pop($file_stages);
foreach ($file_stages as $files) {
foreach ($files as $file_path => $contents) {
$this->file_provider->registerFile($file_path, $contents);
}
2018-11-11 18:01:14 +01:00
$codebase->reloadFiles($this->project_analyzer, array_keys($files));
2018-10-10 22:24:10 +02:00
2020-04-01 18:56:32 +02:00
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false, true);
2018-10-10 22:24:10 +02:00
}
foreach ($end_files as $file_path => $contents) {
$this->file_provider->registerFile($file_path, $contents);
}
2019-02-23 22:22:39 +01:00
$this->expectException(\Psalm\Exception\CodeException::class);
$this->expectExceptionMessageRegExp('/\b' . preg_quote($error_message, '/') . '\b/');
2019-01-02 17:18:22 +01:00
2018-11-11 18:01:14 +01:00
$codebase->reloadFiles($this->project_analyzer, array_keys($end_files));
2018-10-10 22:24:10 +02:00
foreach ($end_files as $file_path => $_) {
$codebase->addFilesToAnalyze([$file_path => $file_path]);
}
2020-04-01 18:56:32 +02:00
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false, true);
2018-10-10 22:24:10 +02:00
}
/**
2019-02-23 22:22:39 +01:00
* @return array<string,array{file_stages:array<int,array<string,string>>,error_message:string}>
2018-10-10 22:24:10 +02:00
*/
public function providerTestInvalidUpdates(): array
2018-10-10 22:24:10 +02:00
{
return [
'invalidateParentCaller' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
public function foo() : void {}
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B extends A { }',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'C.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class C {
public function bar() : void {
(new B)->foo();
}
}
(new C())->bar();',
2018-10-10 22:24:10 +02:00
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A { }',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B extends A { }',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'C.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class C {
public function bar() : void {
(new B)->foo();
}
}
(new C())->bar();',
2018-10-10 22:24:10 +02:00
],
],
'error_message' => 'UndefinedMethod',
],
'invalidateAfterPropertyTypeChange' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
/** @var string */
public $foo = "bar";
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B {
public function foo() : string {
return (new A)->foo;
}
}
2021-06-10 18:57:13 +02:00
echo (new B)->foo();',
2018-10-10 22:24:10 +02:00
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
/** @var int */
public $foo = 5;
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B {
public function foo() : string {
return (new A)->foo;
}
}
2021-06-10 18:57:13 +02:00
echo (new B)->foo();',
2018-10-10 22:24:10 +02:00
],
],
2019-03-23 19:27:54 +01:00
'error_message' => 'InvalidReturnStatement',
2018-10-10 22:24:10 +02:00
],
'invalidateAfterConstantChange' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
public const FOO = "bar";
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B {
public function foo() : string {
return A::FOO;
}
}
2021-06-10 18:57:13 +02:00
echo (new B)->foo();',
2018-10-10 22:24:10 +02:00
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
public const FOO = 5;
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B {
public function foo() : string {
return A::FOO;
}
}
2021-06-10 18:57:13 +02:00
echo (new B)->foo();',
2018-10-10 22:24:10 +02:00
],
],
2019-03-23 19:27:54 +01:00
'error_message' => 'InvalidReturnStatement',
2018-10-10 22:24:10 +02:00
],
'invalidateAfterSkippedAnalysis' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
public function getB() : B {
return new B;
}
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B {
public function getString() : string {
return "foo";
}
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'C.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class C {
public function existingMethod() : string {
2018-10-10 22:24:10 +02:00
return (new A)->getB()->getString();
}
}
2021-06-10 18:57:13 +02:00
echo (new C)->existingMethod();',
2018-10-10 22:24:10 +02:00
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
public function getB() : B {
return new B;
}
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B {
public function getString() : string {
return "foo";
}
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'C.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class C {
public function existingMethod() : string {
2018-10-10 22:24:10 +02:00
return (new A)->getB()->getString();
}
public function newMethod() : void {}
}
2021-06-10 18:57:13 +02:00
echo (new C)->existingMethod();
// newly-added call, removed in the next code block
(new C)->newMethod();',
2018-10-10 22:24:10 +02:00
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
public function getB() : B {
return new B;
}
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B {
public function getString() : ?string {
return "foo";
}
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'C.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class C {
public function existingMethod() : string {
2018-10-10 22:24:10 +02:00
return (new A)->getB()->getString();
}
}
2021-06-10 18:57:13 +02:00
echo (new C)->existingMethod();',
2018-10-10 22:24:10 +02:00
],
],
2019-03-23 19:27:54 +01:00
'error_message' => 'NullableReturnStatement',
2018-10-10 22:24:10 +02:00
],
'invalidateMissingConstructorAfterPropertyChange' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
/** @var string */
public $foo = "bar";
}
echo (new A)->foo;',
2018-10-10 22:24:10 +02:00
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
/** @var string */
public $foo;
}
echo (new A)->foo;',
2018-10-10 22:24:10 +02:00
],
],
2019-03-23 19:27:54 +01:00
'error_message' => 'MissingConstructor',
2018-10-10 22:24:10 +02:00
],
'invalidateEmptyConstructorAfterPropertyChange' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
/** @var string */
public $foo = "bar";
public function __construct() {}
}
echo (new A)->foo;',
2018-10-10 22:24:10 +02:00
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
/** @var string */
public $foo;
public function __construct() {}
}
echo (new A)->foo;',
2018-10-10 22:24:10 +02:00
],
],
2019-03-23 19:27:54 +01:00
'error_message' => 'PropertyNotSetInConstructor',
2018-10-10 22:24:10 +02:00
],
'invalidateEmptyTraitConstructorAfterPropertyChange' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
use T;
/** @var string */
public $foo = "bar";
}
echo (new A)->foo;',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'T.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
trait T {
public function __construct() {}
}',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
use T;
/** @var string */
public $foo;
}
echo (new A)->foo;',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'T.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
trait T {
public function __construct() {}
}',
],
],
2019-03-23 19:27:54 +01:00
'error_message' => 'PropertyNotSetInConstructor',
2018-10-10 22:24:10 +02:00
],
'invalidateEmptyTraitConstructorAfterTraitPropertyChange' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
use T;
}
echo (new A)->foo;',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'T.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
trait T {
/** @var string */
public $foo = "bar";
public function __construct() {}
}',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
use T;
/** @var string */
public $foo;
}
echo (new A)->foo;',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'T.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
trait T {
/** @var string */
public $foo;
public function __construct() {}
}',
],
],
2019-03-23 19:27:54 +01:00
'error_message' => 'PropertyNotSetInConstructor',
2018-10-10 22:24:10 +02:00
],
'invalidateSetInPrivateMethodConstructorCheck' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
/** @var string */
public $foo;
public function __construct() {
$this->setFoo();
}
private function setFoo() : void {
$this->foo = "bar";
}
}
echo (new A)->foo;',
2018-10-10 22:24:10 +02:00
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class A {
/** @var string */
public $foo;
public function __construct() {
$this->setFoo();
}
private function setFoo() : void {
}
}
echo (new A)->foo;',
2018-10-10 22:24:10 +02:00
],
],
2019-03-23 19:27:54 +01:00
'error_message' => 'PropertyNotSetInConstructor',
2018-10-10 22:24:10 +02:00
],
'invalidateMissingConstructorAfterParentPropertyChange' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
abstract class A {
/** @var string */
public $foo = "bar";
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B extends A {}
echo (new B)->foo;',
2018-10-10 22:24:10 +02:00
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
abstract class A {
/** @var string */
public $foo;
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B extends A {}
echo (new B)->foo;',
2018-10-10 22:24:10 +02:00
],
],
2019-03-23 19:27:54 +01:00
'error_message' => 'MissingConstructor',
2018-10-10 22:24:10 +02:00
],
'invalidateNotSetInConstructorAfterParentPropertyChange' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
abstract class A {
/** @var string */
public $foo = "bar";
public function __construct() {}
}
class C extends A {}
new C();',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B extends A {
public function __construct() {}
}
echo (new B)->foo;',
2018-10-10 22:24:10 +02:00
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
abstract class A {
/** @var string */
public $foo;
public function __construct() {}
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
2018-10-10 22:24:10 +02:00
namespace Foo;
class B extends A {
public function __construct() {}
}
echo (new B)->foo;',
2018-10-10 22:24:10 +02:00
],
],
2019-03-23 19:27:54 +01:00
'error_message' => 'PropertyNotSetInConstructor',
2018-10-10 22:24:10 +02:00
],
2019-01-02 17:18:22 +01:00
'duplicateClass' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2019-01-02 17:18:22 +01:00
namespace Foo;
class A {}
new A();',
2019-01-02 17:18:22 +01:00
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2019-01-02 17:18:22 +01:00
namespace Foo;
class A {}
class A {}',
],
],
2019-03-23 19:27:54 +01:00
'error_message' => 'DuplicateClass',
2019-01-02 17:18:22 +01:00
],
'duplicateMethod' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2019-01-02 17:18:22 +01:00
namespace Foo;
class A {
public function foo() : void {}
}
(new A)->foo();',
2019-01-02 17:18:22 +01:00
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
2019-01-02 17:18:22 +01:00
namespace Foo;
class A {
public function foo() : void {}
public function foo() : void {}
}',
],
],
2019-03-23 19:27:54 +01:00
'error_message' => 'DuplicateMethod',
2019-01-02 17:18:22 +01:00
],
'unusedClassReferencedInFile' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {}
2020-09-21 02:58:34 +02:00
$a = new A();
print_r($a);',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {}',
],
],
'error_message' => 'UnusedClass',
],
'unusedMethodReferencedInFile' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public function foo() : void {}
}
(new A)->foo();',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public function foo() : void {}
}
2020-09-21 02:58:34 +02:00
$a = new A();
print_r($a);',
],
],
'error_message' => 'PossiblyUnusedMethod',
],
'unusedStaticMethodReferencedInFile' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public static function foo() : void {}
public static function bar() : void {}
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
\Foo\A::foo();
\Foo\A::bar();',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public static function foo() : void {}
public static function bar() : void {}
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'B.php' => '<?php
\Foo\A::bar();',
],
],
'error_message' => 'PossiblyUnusedMethod',
],
'unusedParamReferencedInFile' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public function foo(string $s) : void {}
}
class B extends A {
public function foo(string $s) : void {
echo $s;
}
}
(new B)->foo("hello");',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public function foo(string $s) : void {}
}
class B extends A {
}
(new B)->foo("hello");',
],
],
'error_message' => 'PossiblyUnusedParam',
],
'unusedMethodReferencedInMethod' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public function foo() : void {}
}
class B {
public function bar() : void {
(new A)->foo();
}
}
(new B)->bar();',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public function foo() : void {}
}
class B {
public function bar() : void {
new A();
}
}
(new B)->bar();',
],
],
'error_message' => 'PossiblyUnusedMethod',
],
'unusedPropertyReferencedInFile' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
/** @var string */
public $foo = "hello";
}
echo (new A)->foo;',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
/** @var string */
public $foo = "hello";
}
2020-09-21 02:58:34 +02:00
print_r(new A());',
],
],
'error_message' => 'PossiblyUnusedProperty',
],
'unusedPropertyReferencedInMethod' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
/** @var string */
public $foo = "hello";
}
class B {
public function bar() : void {
echo (new A)->foo;
}
}
(new B)->bar();',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
/** @var string */
public $foo = "hello";
}
class B {
public function bar() : void {
new A();
}
}
(new B)->bar();',
],
],
'error_message' => 'PossiblyUnusedProperty',
],
'uninitialisedChildProperty' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
abstract class A {
public function __construct() {
$this->setFoo();
}
abstract protected function setFoo() : void;
}',
getcwd() . DIRECTORY_SEPARATOR . 'AChild.php' => '<?php
namespace Foo;
class AChild extends A {
/** @var string */
public $foo;
protected function setFoo() : void {
$this->reallySetFoo();
}
private function reallySetFoo() : void {
$this->foo = "bar";
}
}',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
abstract class A {
public function __construct() {
$this->setFoo();
}
abstract protected function setFoo() : void;
}',
getcwd() . DIRECTORY_SEPARATOR . 'AChild.php' => '<?php
namespace Foo;
class AChild extends A {
/** @var string */
public $foo;
protected function setFoo() : void {
$this->reallySetFoo();
}
private function reallySetFoo() : void {
//$this->foo = "bar";
}
}',
],
],
'error_message' => 'PropertyNotSetInConstructor',
],
'invalidateChildMethodWhenSignatureChanges' => [
'file_stages' => [
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public function foo(string $s) : void {
echo $s;
}
}
class AChild extends A {
public function foo(string $s) : void {
echo $s;
}
}',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public function foo(string $s = "") : void {
echo $s;
}
}
class AChild extends A {
public function foo(string $s) : void {
echo $s;
}
}',
],
],
'error_message' => 'MethodSignatureMismatch',
],
2018-10-10 22:24:10 +02:00
];
}
}