2017-02-08 06:28:26 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Checker\FileChecker;
|
2018-01-29 05:41:11 +01:00
|
|
|
use Psalm\Config;
|
2017-07-25 23:04:58 +02:00
|
|
|
use Psalm\Context;
|
2017-02-08 06:28:26 +01:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class UnusedCodeTest extends TestCase
|
2017-02-08 06:28:26 +01:00
|
|
|
{
|
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
FileChecker::clearCache();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
$this->file_provider = new Provider\FakeFileProvider();
|
|
|
|
|
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker(
|
2018-01-21 16:22:04 +01:00
|
|
|
new TestConfig(),
|
2018-09-28 22:18:45 +02:00
|
|
|
new \Psalm\Provider\Providers(
|
|
|
|
$this->file_provider,
|
|
|
|
new Provider\FakeParserCacheProvider()
|
|
|
|
)
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
|
|
|
|
2018-02-17 23:45:30 +01:00
|
|
|
$this->project_checker->getCodebase()->reportUnusedCode();
|
2017-02-08 06:28:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-25 23:04:58 +02:00
|
|
|
* @dataProvider providerFileCheckerValidCodeParse
|
|
|
|
*
|
|
|
|
* @param string $code
|
|
|
|
* @param array<string> $error_levels
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-29 18:13:44 +01:00
|
|
|
public function testValidCode($code, array $error_levels = [])
|
2017-07-25 23:04:58 +02:00
|
|
|
{
|
2018-07-13 23:44:50 +02:00
|
|
|
$test_name = $this->getTestName();
|
2017-07-25 23:04:58 +02:00
|
|
|
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-01-21 16:22:04 +01:00
|
|
|
$file_path = self::$src_dir_path . 'somefile.php';
|
2017-07-25 23:04:58 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
2018-01-21 16:22:04 +01:00
|
|
|
$file_path,
|
2017-07-25 23:04:58 +02:00
|
|
|
$code
|
|
|
|
);
|
|
|
|
|
2018-01-29 18:13:44 +01:00
|
|
|
foreach ($error_levels as $error_level) {
|
|
|
|
$this->project_checker->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS);
|
|
|
|
}
|
|
|
|
|
2018-01-29 05:41:11 +01:00
|
|
|
$context = new Context();
|
|
|
|
$context->collect_references = true;
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, $context);
|
2018-02-17 23:45:30 +01:00
|
|
|
|
|
|
|
$this->project_checker->checkClassReferences();
|
2017-07-25 23:04:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerFileCheckerInvalidCodeParse
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-04-25 05:45:02 +02:00
|
|
|
* @param string $code
|
|
|
|
* @param string $error_message
|
2018-01-29 05:41:11 +01:00
|
|
|
* @param array<string> $error_levels
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-08 06:28:26 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-29 05:41:11 +01:00
|
|
|
public function testInvalidCode($code, $error_message, $error_levels = [])
|
2017-02-08 06:28:26 +01:00
|
|
|
{
|
2018-07-13 23:44:50 +02:00
|
|
|
if (strpos($this->getTestName(), 'SKIPPED-') !== false) {
|
2017-07-25 23:04:58 +02:00
|
|
|
$this->markTestSkipped();
|
|
|
|
}
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->expectException('\Psalm\Exception\CodeException');
|
2018-01-11 23:35:28 +01:00
|
|
|
$this->expectExceptionMessageRegexp('/\b' . preg_quote($error_message, '/') . '\b/');
|
2017-02-08 06:28:26 +01:00
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$file_path = self::$src_dir_path . 'somefile.php';
|
|
|
|
|
2018-01-29 05:41:11 +01:00
|
|
|
foreach ($error_levels as $error_level) {
|
|
|
|
$this->project_checker->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS);
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
2018-01-21 16:22:04 +01:00
|
|
|
$file_path,
|
2017-07-25 22:11:02 +02:00
|
|
|
$code
|
|
|
|
);
|
2017-02-08 06:28:26 +01:00
|
|
|
|
2018-01-29 05:41:11 +01:00
|
|
|
$context = new Context();
|
|
|
|
$context->collect_references = true;
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, $context);
|
2018-02-17 23:45:30 +01:00
|
|
|
|
|
|
|
$this->project_checker->checkClassReferences();
|
2017-02-08 06:28:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-02-08 06:28:26 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2017-02-08 06:28:26 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
2017-11-11 00:08:17 +01:00
|
|
|
'magicCall' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private $value = "default";
|
|
|
|
|
|
|
|
/** @param string[] $args */
|
|
|
|
public function __call(string $name, array $args) {
|
|
|
|
if (count($args) == 1) {
|
|
|
|
$this->modify($name, $args[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
private function modify(string $name, string $value): void {
|
2017-11-11 00:08:17 +01:00
|
|
|
call_user_func(array($this, "modify_" . $name), $value);
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
public function modifyFoo(string $value): void {
|
2017-11-11 00:08:17 +01:00
|
|
|
$this->value = $value;
|
|
|
|
}
|
2018-01-11 05:29:18 +01:00
|
|
|
|
|
|
|
public function getFoo() : string {
|
|
|
|
return $this->value;
|
|
|
|
}
|
2017-11-11 00:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$m = new A();
|
|
|
|
$m->foo("value");
|
2018-01-11 05:29:18 +01:00
|
|
|
$m->modifyFoo("value2");
|
|
|
|
echo $m->getFoo();',
|
2017-11-11 00:08:17 +01:00
|
|
|
],
|
2018-06-10 05:10:42 +02:00
|
|
|
'usedTraitMethodWithExplicitCall' => [
|
2017-12-29 17:26:28 +01:00
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {
|
2017-12-29 17:26:28 +01:00
|
|
|
echo "parent method";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {
|
2017-12-29 17:26:28 +01:00
|
|
|
echo "trait method";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
|
|
|
|
(new A)->foo();
|
|
|
|
(new B)->foo();',
|
|
|
|
],
|
2017-12-30 01:38:01 +01:00
|
|
|
'usedInterfaceMethod' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void;
|
2017-12-30 01:38:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class A implements I {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {}
|
2017-12-30 01:38:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
(new A)->foo();',
|
|
|
|
],
|
2018-01-29 05:41:11 +01:00
|
|
|
'constructorIsUsed' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __construct() {
|
|
|
|
$this->foo();
|
|
|
|
}
|
|
|
|
private function foo() : void {}
|
|
|
|
}
|
|
|
|
$a = new A();
|
|
|
|
echo (bool) $a;',
|
|
|
|
],
|
2018-02-17 18:02:31 +01:00
|
|
|
'everythingUsed' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
public function foo();
|
|
|
|
}
|
|
|
|
class B implements I {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var I
|
|
|
|
*/
|
|
|
|
private $i;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int[] $as
|
|
|
|
*/
|
|
|
|
public function __construct(array $as) {
|
|
|
|
|
|
|
|
foreach ($as as $a) {
|
|
|
|
$this->a($a, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->i = new B();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function a(int $a, int $b): self
|
|
|
|
{
|
|
|
|
$this->v($a, $b);
|
|
|
|
|
|
|
|
$this->i->foo();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function v(int $a, int $b): void
|
|
|
|
{
|
|
|
|
if ($a + $b > 0) {
|
|
|
|
throw new \RuntimeException("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new A([1, 2, 3]);',
|
|
|
|
],
|
2018-02-27 17:39:26 +01:00
|
|
|
'unusedParamWithUnderscore' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $_) : void {}
|
|
|
|
|
|
|
|
foo(4);',
|
|
|
|
],
|
|
|
|
'unusedParamWithUnusedPrefix' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $unusedArg) : void {}
|
|
|
|
|
|
|
|
foo(4);',
|
|
|
|
],
|
|
|
|
'possiblyUnusedParamWithUnderscore' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function foo(int $_ = null) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
A::foo();',
|
|
|
|
],
|
|
|
|
'possiblyUnusedParamWithUnusedPrefix' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function foo(int $unusedArg = null) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
A::foo();',
|
|
|
|
],
|
2018-03-07 17:16:56 +01:00
|
|
|
'usedClass' => [
|
|
|
|
'<?php
|
|
|
|
class A { }
|
|
|
|
new A();',
|
|
|
|
],
|
2018-06-10 05:10:42 +02:00
|
|
|
'usedTraitMethodWithImplicitCall' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
trait T {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
function takesA(A $a) : void {
|
|
|
|
$a->foo();
|
|
|
|
}
|
|
|
|
takesA(new B);'
|
|
|
|
],
|
2018-08-07 19:17:23 +02:00
|
|
|
'usedMethodInTryCatch' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
protected function getC() : C {
|
|
|
|
return new C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class C {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function bar() : void {
|
|
|
|
$c = $this->getC();
|
|
|
|
|
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
try {
|
|
|
|
$c->foo();
|
|
|
|
} catch (Exception $e) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new B)->bar();',
|
|
|
|
],
|
2018-07-12 18:12:28 +02:00
|
|
|
'suppressPrivateUnusedMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @psalm-suppress UnusedMethod
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
new A();',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-08 06:28:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-02-08 06:28:26 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2017-02-08 06:28:26 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'unusedClass' => [
|
|
|
|
'<?php
|
|
|
|
class A { }',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UnusedClass',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'publicUnusedMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function foo() {}
|
|
|
|
}
|
2017-05-04 20:25:58 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
new A();',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyUnusedMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-01-22 05:48:58 +01:00
|
|
|
'possiblyUnusedParam' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function foo(int $i) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new A)->foo(4);',
|
|
|
|
'error_message' => 'PossiblyUnusedParam',
|
|
|
|
],
|
|
|
|
'unusedParam' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $i) {}
|
|
|
|
|
|
|
|
foo(4);',
|
|
|
|
'error_message' => 'UnusedParam',
|
|
|
|
],
|
|
|
|
'possiblyUnusedProperty' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();',
|
|
|
|
'error_message' => 'PossiblyUnusedProperty',
|
2018-01-29 05:41:11 +01:00
|
|
|
'error_levels' => ['UnusedVariable'],
|
2018-01-22 05:48:58 +01:00
|
|
|
],
|
|
|
|
'unusedProperty' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private $foo = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();',
|
|
|
|
'error_message' => 'UnusedProperty',
|
2018-01-29 05:41:11 +01:00
|
|
|
'error_levels' => ['UnusedVariable'],
|
2018-01-22 05:48:58 +01:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'privateUnusedMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
private function foo() {}
|
|
|
|
}
|
2017-05-04 20:25:58 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
new A();',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UnusedMethod',
|
|
|
|
],
|
2017-12-30 16:54:01 +01:00
|
|
|
'unevaluatedCode' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): void {
|
2017-12-30 16:54:01 +01:00
|
|
|
return;
|
|
|
|
$a = "foo";
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnevaluatedCode',
|
|
|
|
],
|
2018-06-10 05:10:42 +02:00
|
|
|
'unusedTraitMethodInParent' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
trait T {
|
|
|
|
public function foo() : void {}
|
|
|
|
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
function takesA(A $a) : void {
|
|
|
|
$a->foo();
|
|
|
|
}
|
|
|
|
takesA(new B);',
|
|
|
|
'error_message' => 'PossiblyUnusedMethod',
|
|
|
|
],
|
2018-06-19 22:14:51 +02:00
|
|
|
'unusedRecursivelyUsedMethod' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
public function foo() : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new C)->bar();',
|
|
|
|
'error_message' => 'PossiblyUnusedMethod',
|
|
|
|
],
|
|
|
|
'unusedRecursivelyUsedStaticMethod' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
public static function foo() : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
self::foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new C)->bar();',
|
|
|
|
'error_message' => 'PossiblyUnusedMethod',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-08 06:28:26 +01:00
|
|
|
}
|
|
|
|
}
|