2017-02-08 06:28:26 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class UnusedCodeTest extends TestCase
|
2017-02-08 06:28:26 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-02-08 06:28:26 +01:00
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected static $project_dir;
|
|
|
|
|
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
parent::setUpBeforeClass();
|
2017-02-08 06:28:26 +01:00
|
|
|
self::$project_dir = getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
FileChecker::clearCache();
|
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
|
|
|
$this->project_checker->setConfig(Config::loadFromXML(
|
|
|
|
'psalm.xml',
|
2017-05-04 20:25:58 +02:00
|
|
|
dirname(__DIR__),
|
2017-02-08 06:28:26 +01:00
|
|
|
'<?xml version="1.0"?>
|
|
|
|
<psalm
|
|
|
|
throwExceptionOnError="true"
|
|
|
|
useDocblockTypes="true"
|
|
|
|
totallyTyped="true"
|
|
|
|
>
|
|
|
|
<projectFiles>
|
|
|
|
<directory name="src" />
|
|
|
|
</projectFiles>
|
|
|
|
</psalm>'
|
|
|
|
));
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->project_checker->collect_references = true;
|
2017-02-08 06:28:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @dataProvider providerTestUnusedCodeWithClassReferences
|
|
|
|
* @param string $code
|
|
|
|
* @param string $error_message
|
2017-02-08 06:28:26 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function testUnusedCodeWithClassReferences($code, $error_message)
|
2017-02-08 06:28:26 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->expectException('\Psalm\Exception\CodeException');
|
|
|
|
$this->expectExceptionMessage($error_message);
|
2017-02-08 06:28:26 +01:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$stmts = self::$parser->parse($code);
|
2017-02-08 06:28:26 +01:00
|
|
|
|
|
|
|
$file_checker = new FileChecker(self::$project_dir . 'somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2017-04-25 05:45:02 +02: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 [
|
|
|
|
'unset' => [
|
|
|
|
'<?php
|
|
|
|
/** @return void */
|
|
|
|
function foo() {
|
|
|
|
$a = 0;
|
2017-05-04 20:25:58 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$arr = ["hello"];
|
2017-05-04 20:25:58 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
unset($arr[$a]);
|
|
|
|
}'
|
|
|
|
]
|
|
|
|
];
|
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 [
|
|
|
|
'function' => [
|
|
|
|
'<?php
|
|
|
|
/** @return int */
|
|
|
|
function foo() {
|
|
|
|
$a = 5;
|
|
|
|
$b = [];
|
|
|
|
return $a;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable'
|
|
|
|
],
|
|
|
|
'ifInFunction' => [
|
|
|
|
'<?php
|
|
|
|
/** @return int */
|
|
|
|
function foo() {
|
|
|
|
$a = 5;
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$b = "hello";
|
|
|
|
} else {
|
|
|
|
$b = "goodbye";
|
|
|
|
}
|
|
|
|
return $a;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable'
|
|
|
|
]
|
|
|
|
];
|
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 providerTestUnusedCodeWithClassReferences()
|
2017-02-08 06:28:26 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'unusedClass' => [
|
|
|
|
'<?php
|
|
|
|
class A { }',
|
|
|
|
'error_message' => 'UnusedClass'
|
|
|
|
],
|
|
|
|
'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();',
|
|
|
|
'error_message' => 'PossiblyUnusedMethod'
|
|
|
|
],
|
|
|
|
'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();',
|
|
|
|
'error_message' => 'UnusedMethod'
|
|
|
|
]
|
|
|
|
];
|
2017-02-08 06:28:26 +01:00
|
|
|
}
|
|
|
|
}
|