2017-04-24 23:45:02 -04:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
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;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Context;
|
2018-11-05 21:57:36 -05:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2021-07-02 02:10:21 +03:00
|
|
|
use Psalm\Internal\Provider\FakeFileProvider;
|
2019-03-23 14:27:54 -04:00
|
|
|
use Psalm\Internal\Provider\Providers;
|
2020-08-23 17:32:07 +03:00
|
|
|
use Psalm\Internal\RuntimeCaches;
|
2021-08-08 10:39:54 +02:00
|
|
|
use Psalm\Internal\Type\TypeParser;
|
|
|
|
use Psalm\Internal\Type\TypeTokenizer;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
|
2021-08-08 10:39:54 +02:00
|
|
|
use Psalm\Type\Union;
|
|
|
|
use Throwable;
|
2017-04-24 23:45:02 -04:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
use function array_filter;
|
|
|
|
use function count;
|
2021-06-08 05:55:21 +03:00
|
|
|
use function define;
|
|
|
|
use function defined;
|
|
|
|
use function getcwd;
|
|
|
|
use function ini_set;
|
2021-08-08 10:39:54 +02:00
|
|
|
use function is_string;
|
2021-06-08 05:55:21 +03:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
use const ARRAY_FILTER_USE_KEY;
|
2021-06-08 05:55:21 +03:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
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
|
|
|
|
2021-07-02 02:10:21 +03:00
|
|
|
/** @var FakeFileProvider */
|
2017-07-25 16:11:02 -04:00
|
|
|
protected $file_provider;
|
|
|
|
|
2021-05-26 22:04:22 +02:00
|
|
|
/** @var Config */
|
|
|
|
protected $testConfig;
|
|
|
|
|
2021-12-05 18:51:26 +01: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')) {
|
2021-05-17 15:27:08 +02:00
|
|
|
define('PSALM_VERSION', '4.0.0');
|
2018-04-24 21:02:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
protected function makeConfig(): Config
|
2020-02-24 11:43:44 -08:00
|
|
|
{
|
|
|
|
return new TestConfig();
|
|
|
|
}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function setUp(): void
|
2017-04-24 23:45:02 -04:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2020-08-23 17:32:07 +03:00
|
|
|
RuntimeCaches::clearAll();
|
2020-08-10 09:58:43 -04:00
|
|
|
|
2021-07-02 02:10:21 +03:00
|
|
|
$this->file_provider = new FakeFileProvider();
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2021-05-26 22:04:22 +02:00
|
|
|
$this->testConfig = $this->makeConfig();
|
2018-09-28 16:18:45 -04:00
|
|
|
|
|
|
|
$providers = new Providers(
|
|
|
|
$this->file_provider,
|
2021-12-04 21:55:53 +01:00
|
|
|
new FakeParserCacheProvider()
|
2018-09-28 16:18:45 -04:00
|
|
|
);
|
2018-01-21 13:38:51 -05:00
|
|
|
|
2018-11-11 12:01:14 -05:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
2021-05-26 22:04:22 +02:00
|
|
|
$this->testConfig,
|
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
|
|
|
|
2021-11-27 02:06:33 +02:00
|
|
|
$this->project_analyzer->setPhpVersion('7.4', 'tests');
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
2017-07-25 16:11:02 -04:00
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function tearDown(): void
|
2019-05-16 18:36:36 -04:00
|
|
|
{
|
2021-05-26 22:04:22 +02:00
|
|
|
unset($this->project_analyzer, $this->file_provider, $this->testConfig);
|
2020-08-23 17:32:07 +03:00
|
|
|
RuntimeCaches::clearAll();
|
2019-05-16 18:36:36 -04:00
|
|
|
}
|
|
|
|
|
2017-07-25 16:11:02 -04:00
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
* @param string $contents
|
|
|
|
*
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function addFile($file_path, $contents): void
|
2017-07-25 16:11:02 -04:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
2022-04-27 01:42:37 -04:00
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
* @param string $contents
|
|
|
|
*/
|
|
|
|
public function addStubFile($file_path, $contents): void
|
|
|
|
{
|
|
|
|
$this->file_provider->registerFile($file_path, $contents);
|
|
|
|
$this->project_analyzer->getConfig()->addStubFile($file_path);
|
|
|
|
}
|
|
|
|
|
2018-01-21 10:22:04 -05:00
|
|
|
/**
|
2018-01-21 13:38:51 -05:00
|
|
|
* @param string $file_path
|
2018-01-21 10:22:04 -05:00
|
|
|
*
|
|
|
|
*/
|
2021-12-03 20:11:20 +01:00
|
|
|
public function analyzeFile($file_path, Context $context, bool $track_unused_suppressions = true, bool $taint_flow_tracking = false): void
|
2018-01-21 10:22:04 -05:00
|
|
|
{
|
2018-11-11 12:01:14 -05:00
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
2020-10-29 19:41:10 -04:00
|
|
|
|
2020-11-17 19:23:20 +01:00
|
|
|
if ($taint_flow_tracking) {
|
|
|
|
$this->project_analyzer->trackTaintedInputs();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-09-25 00:37:40 -04:00
|
|
|
if ($codebase->taint_flow_graph) {
|
|
|
|
$codebase->taint_flow_graph->connectSinksAndSources();
|
2019-08-04 10:37:36 -04:00
|
|
|
}
|
2019-08-18 14:27:50 -04:00
|
|
|
|
2019-08-18 15:34:32 -04:00
|
|
|
if ($track_unused_suppressions) {
|
2021-12-03 20:11:20 +01:00
|
|
|
IssueBuffer::processUnusedSuppressions($codebase->file_provider);
|
2019-08-18 14:27:50 -04:00
|
|
|
}
|
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
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
protected function getTestName($withDataSet = true): string
|
2018-07-14 00:44:50 +03:00
|
|
|
{
|
2021-12-27 19:59:11 +01:00
|
|
|
return $this->getName($withDataSet);
|
2018-07-14 00:44:50 +03:00
|
|
|
}
|
2020-04-06 00:25:00 +03:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
public static function assertArrayKeysAreStrings(array $array, string $message = ''): void
|
|
|
|
{
|
|
|
|
$validKeys = array_filter($array, 'is_string', ARRAY_FILTER_USE_KEY);
|
|
|
|
self::assertTrue(count($array) === count($validKeys), $message);
|
|
|
|
}
|
2021-11-27 02:06:33 +02:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
public static function assertArrayKeysAreZeroOrString(array $array, string $message = ''): void
|
|
|
|
{
|
2022-01-06 00:45:11 +02:00
|
|
|
$isZeroOrString = /** @param mixed $key */ fn($key): bool => $key === 0 || is_string($key);
|
2021-08-08 10:39:54 +02:00
|
|
|
$validKeys = array_filter($array, $isZeroOrString, ARRAY_FILTER_USE_KEY);
|
|
|
|
self::assertTrue(count($array) === count($validKeys), $message);
|
|
|
|
}
|
2021-11-27 02:06:33 +02:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
public static function assertArrayValuesAreArrays(array $array, string $message = ''): void
|
|
|
|
{
|
|
|
|
$validValues = array_filter($array, 'is_array');
|
|
|
|
self::assertTrue(count($array) === count($validValues), $message);
|
|
|
|
}
|
2021-11-27 02:06:33 +02:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
public static function assertArrayValuesAreStrings(array $array, string $message = ''): void
|
|
|
|
{
|
|
|
|
$validValues = array_filter($array, 'is_string');
|
|
|
|
self::assertTrue(count($array) === count($validValues), $message);
|
|
|
|
}
|
2021-11-27 02:06:33 +02:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
public static function assertStringIsParsableType(string $type, string $message = ''): void
|
|
|
|
{
|
|
|
|
if ($type === '') {
|
|
|
|
// Ignore empty types for now, as these are quite common for pecl libraries
|
|
|
|
self::assertTrue(true);
|
|
|
|
} else {
|
|
|
|
$union = null;
|
|
|
|
try {
|
|
|
|
$tokens = TypeTokenizer::tokenize($type);
|
|
|
|
$union = TypeParser::parseTokens($tokens);
|
|
|
|
} catch (Throwable $_e) {
|
|
|
|
}
|
|
|
|
self::assertInstanceOf(Union::class, $union, $message);
|
|
|
|
}
|
|
|
|
}
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|