2017-04-24 23:45:02 -04:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2019-07-05 16:24:00 -04:00
|
|
|
use function define;
|
|
|
|
use function defined;
|
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
use function getcwd;
|
|
|
|
use function ini_set;
|
2017-12-15 12:18:33 +01:00
|
|
|
use PHPUnit\Framework\TestCase as BaseTestCase;
|
2020-02-24 11:43:44 -08:00
|
|
|
use Psalm\Config;
|
2018-11-05 21:57:36 -05:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2019-03-23 14:27:54 -04:00
|
|
|
use Psalm\Internal\Provider\Providers;
|
2018-11-12 10:57:05 -05:00
|
|
|
use Psalm\Tests\Internal\Provider;
|
2018-07-14 00:44:50 +03:00
|
|
|
use RuntimeException;
|
2017-04-24 23:45:02 -04:00
|
|
|
|
2017-12-15 12:18:33 +01:00
|
|
|
class TestCase extends BaseTestCase
|
2017-04-24 23:45:02 -04:00
|
|
|
{
|
2017-07-25 17:04:58 -04:00
|
|
|
/** @var string */
|
|
|
|
protected static $src_dir_path;
|
|
|
|
|
2018-11-05 21:57:36 -05:00
|
|
|
/** @var ProjectAnalyzer */
|
2018-11-11 12:01:14 -05:00
|
|
|
protected $project_analyzer;
|
2017-04-24 23:45:02 -04:00
|
|
|
|
2017-07-25 16:11:02 -04:00
|
|
|
/** @var Provider\FakeFileProvider */
|
|
|
|
protected $file_provider;
|
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-05-16 18:36:36 -04:00
|
|
|
public static function setUpBeforeClass() : void
|
2017-04-24 23:45:02 -04:00
|
|
|
{
|
2018-01-09 09:21:54 -05:00
|
|
|
ini_set('memory_limit', '-1');
|
2018-04-24 21:02:07 -04:00
|
|
|
|
|
|
|
if (!defined('PSALM_VERSION')) {
|
|
|
|
define('PSALM_VERSION', '2.0.0');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined('PHP_PARSER_VERSION')) {
|
|
|
|
define('PHP_PARSER_VERSION', '4.0.0');
|
|
|
|
}
|
2018-04-24 07:19:25 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
parent::setUpBeforeClass();
|
2017-07-25 17:04:58 -04:00
|
|
|
self::$src_dir_path = getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
|
|
|
|
2020-02-24 11:43:44 -08:00
|
|
|
/**
|
|
|
|
* @return Config
|
|
|
|
*/
|
|
|
|
protected function getConfig() : Config
|
|
|
|
{
|
|
|
|
return new TestConfig();
|
|
|
|
}
|
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-05-16 18:36:36 -04:00
|
|
|
public function setUp() : void
|
2017-04-24 23:45:02 -04:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-11-05 21:57:36 -05:00
|
|
|
FileAnalyzer::clearCache();
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2018-11-12 10:57:05 -05:00
|
|
|
$this->file_provider = new \Psalm\Tests\Internal\Provider\FakeFileProvider();
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2020-02-24 11:43:44 -08:00
|
|
|
$config = $this->getConfig();
|
2018-09-28 16:18:45 -04:00
|
|
|
|
|
|
|
$providers = new Providers(
|
|
|
|
$this->file_provider,
|
|
|
|
new Provider\FakeParserCacheProvider()
|
|
|
|
);
|
2018-01-21 13:38:51 -05:00
|
|
|
|
2018-11-11 12:01:14 -05:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
2018-01-21 13:38:51 -05:00
|
|
|
$config,
|
2019-06-09 12:37:28 -04:00
|
|
|
$providers
|
2017-07-25 16:11:02 -04:00
|
|
|
);
|
2019-02-07 14:08:35 -05:00
|
|
|
|
|
|
|
$this->project_analyzer->setPhpVersion('7.3');
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2019-05-16 18:36:36 -04:00
|
|
|
public function tearDown() : void
|
|
|
|
{
|
|
|
|
FileAnalyzer::clearCache();
|
|
|
|
}
|
|
|
|
|
2017-07-25 16:11:02 -04:00
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
* @param string $contents
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addFile($file_path, $contents)
|
|
|
|
{
|
|
|
|
$this->file_provider->registerFile($file_path, $contents);
|
2019-01-12 10:52:23 -05:00
|
|
|
$this->project_analyzer->getCodebase()->scanner->addFileToShallowScan($file_path);
|
2017-07-25 16:11:02 -04:00
|
|
|
}
|
2018-01-21 10:22:04 -05:00
|
|
|
|
|
|
|
/**
|
2018-01-21 13:38:51 -05:00
|
|
|
* @param string $file_path
|
|
|
|
* @param \Psalm\Context $context
|
2018-01-21 10:22:04 -05:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-08-18 15:34:32 -04:00
|
|
|
public function analyzeFile($file_path, \Psalm\Context $context, bool $track_unused_suppressions = true)
|
2018-01-21 10:22:04 -05:00
|
|
|
{
|
2018-11-11 12:01:14 -05:00
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
2018-02-03 18:52:35 -05:00
|
|
|
$codebase->addFilesToAnalyze([$file_path => $file_path]);
|
|
|
|
|
2018-01-21 13:38:51 -05:00
|
|
|
$codebase->scanFiles();
|
|
|
|
|
2018-06-30 15:29:37 -04:00
|
|
|
$codebase->config->visitStubFiles($codebase);
|
|
|
|
|
2019-06-02 23:33:57 -04:00
|
|
|
if ($codebase->alter_code) {
|
|
|
|
$this->project_analyzer->interpretRefactors();
|
|
|
|
}
|
|
|
|
|
2019-08-18 16:59:56 -04:00
|
|
|
$this->project_analyzer->trackUnusedSuppressions();
|
2019-08-18 15:34:32 -04:00
|
|
|
|
2018-11-11 12:01:14 -05:00
|
|
|
$file_analyzer = new FileAnalyzer(
|
|
|
|
$this->project_analyzer,
|
2018-01-21 13:38:51 -05:00
|
|
|
$file_path,
|
|
|
|
$codebase->config->shortenFileName($file_path)
|
|
|
|
);
|
2018-11-11 12:01:14 -05:00
|
|
|
$file_analyzer->analyze($context);
|
2019-08-04 10:37:36 -04:00
|
|
|
|
|
|
|
if ($codebase->taint) {
|
|
|
|
while ($codebase->taint->hasNewSinksAndSources()) {
|
|
|
|
$codebase->taint->clearNewSinksAndSources();
|
|
|
|
|
|
|
|
$file_analyzer->analyze($context);
|
|
|
|
}
|
|
|
|
}
|
2019-08-18 14:27:50 -04:00
|
|
|
|
2019-08-18 15:34:32 -04:00
|
|
|
if ($track_unused_suppressions) {
|
2019-08-18 14:27:50 -04:00
|
|
|
\Psalm\IssueBuffer::processUnusedSuppressions($codebase->file_provider);
|
|
|
|
}
|
2018-01-21 10:22:04 -05:00
|
|
|
}
|
2018-07-22 19:29:04 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $withDataSet
|
2019-03-23 14:27:54 -04:00
|
|
|
*
|
2018-07-22 19:29:04 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getTestName($withDataSet = true)
|
2018-07-14 00:44:50 +03:00
|
|
|
{
|
|
|
|
$name = parent::getName($withDataSet);
|
2019-06-07 06:51:34 -04:00
|
|
|
/**
|
|
|
|
* @psalm-suppress TypeDoesNotContainNull PHPUnit 8.2 made it non-nullable again
|
|
|
|
*/
|
2018-07-14 00:44:50 +03:00
|
|
|
if (null === $name) {
|
|
|
|
throw new RuntimeException('anonymous test - shouldn\'t happen');
|
|
|
|
}
|
2019-03-23 14:27:54 -04:00
|
|
|
|
2018-07-14 00:44:50 +03:00
|
|
|
return $name;
|
|
|
|
}
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|