2017-04-25 05:45:02 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2023-10-19 13:12:06 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-12-15 12:18:33 +01:00
|
|
|
use PHPUnit\Framework\TestCase as BaseTestCase;
|
2020-02-24 20:43:44 +01:00
|
|
|
use Psalm\Config;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Context;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2021-07-02 01:10:21 +02:00
|
|
|
use Psalm\Internal\Provider\FakeFileProvider;
|
2019-03-23 19:27:54 +01:00
|
|
|
use Psalm\Internal\Provider\Providers;
|
2020-08-23 16:32:07 +02: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-25 05:45:02 +02:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
use function array_filter;
|
|
|
|
use function count;
|
2021-06-08 04:55:21 +02: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 04:55:21 +02:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
use const ARRAY_FILTER_USE_KEY;
|
2021-06-08 04:55:21 +02:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2017-12-15 12:18:33 +01:00
|
|
|
class TestCase extends BaseTestCase
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
2022-12-16 19:58:47 +01:00
|
|
|
protected static string $src_dir_path;
|
2017-07-25 23:04:58 +02:00
|
|
|
|
2022-12-16 19:58:47 +01:00
|
|
|
protected ProjectAnalyzer $project_analyzer;
|
2017-04-25 05:45:02 +02:00
|
|
|
|
2022-12-16 19:58:47 +01:00
|
|
|
protected FakeFileProvider $file_provider;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2022-12-16 19:58:47 +01:00
|
|
|
protected Config $testConfig;
|
2021-05-26 22:04:22 +02:00
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public static function setUpBeforeClass(): void
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
2018-01-09 15:21:54 +01:00
|
|
|
ini_set('memory_limit', '-1');
|
2018-04-25 03:02:07 +02:00
|
|
|
|
|
|
|
if (!defined('PSALM_VERSION')) {
|
2021-05-17 15:27:08 +02:00
|
|
|
define('PSALM_VERSION', '4.0.0');
|
2018-04-25 03:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined('PHP_PARSER_VERSION')) {
|
|
|
|
define('PHP_PARSER_VERSION', '4.0.0');
|
|
|
|
}
|
2018-04-24 13:19:25 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
parent::setUpBeforeClass();
|
2023-10-09 22:40:21 +02:00
|
|
|
self::$src_dir_path = (string) getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
protected function makeConfig(): Config
|
2020-02-24 20:43:44 +01:00
|
|
|
{
|
|
|
|
return new TestConfig();
|
|
|
|
}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function setUp(): void
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2020-08-23 16:32:07 +02:00
|
|
|
RuntimeCaches::clearAll();
|
2020-08-10 15:58:43 +02:00
|
|
|
|
2021-07-02 01:10:21 +02:00
|
|
|
$this->file_provider = new FakeFileProvider();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2021-05-26 22:04:22 +02:00
|
|
|
$this->testConfig = $this->makeConfig();
|
2018-09-28 22:18:45 +02:00
|
|
|
|
|
|
|
$providers = new Providers(
|
|
|
|
$this->file_provider,
|
2022-12-18 17:15:15 +01:00
|
|
|
new FakeParserCacheProvider(),
|
2018-09-28 22:18:45 +02:00
|
|
|
);
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
2021-05-26 22:04:22 +02:00
|
|
|
$this->testConfig,
|
2022-12-18 17:15:15 +01:00
|
|
|
$providers,
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
2019-02-07 20:08:35 +01:00
|
|
|
|
2021-11-27 01:06:33 +01:00
|
|
|
$this->project_analyzer->setPhpVersion('7.4', 'tests');
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function tearDown(): void
|
2019-05-17 00:36:36 +02:00
|
|
|
{
|
2021-05-26 22:04:22 +02:00
|
|
|
unset($this->project_analyzer, $this->file_provider, $this->testConfig);
|
2020-08-23 16:32:07 +02:00
|
|
|
RuntimeCaches::clearAll();
|
2019-05-17 00:36:36 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 03:26:17 +01:00
|
|
|
public function addFile(string $file_path, string $contents): void
|
2017-07-25 22:11:02 +02:00
|
|
|
{
|
|
|
|
$this->file_provider->registerFile($file_path, $contents);
|
2019-01-12 16:52:23 +01:00
|
|
|
$this->project_analyzer->getCodebase()->scanner->addFileToShallowScan($file_path);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2018-01-21 16:22:04 +01:00
|
|
|
|
2022-12-15 03:26:17 +01:00
|
|
|
public function addStubFile(string $file_path, string $contents): void
|
2022-04-27 07:42:37 +02:00
|
|
|
{
|
|
|
|
$this->file_provider->registerFile($file_path, $contents);
|
|
|
|
$this->project_analyzer->getConfig()->addStubFile($file_path);
|
|
|
|
}
|
|
|
|
|
2022-12-15 03:26:17 +01:00
|
|
|
public function analyzeFile(string $file_path, Context $context, bool $track_unused_suppressions = true, bool $taint_flow_tracking = false): void
|
2018-01-21 16:22:04 +01:00
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
2020-10-30 00:41:10 +01:00
|
|
|
|
2020-11-17 19:23:20 +01:00
|
|
|
if ($taint_flow_tracking) {
|
|
|
|
$this->project_analyzer->trackTaintedInputs();
|
|
|
|
}
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$codebase->addFilesToAnalyze([$file_path => $file_path]);
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase->scanFiles();
|
|
|
|
|
2018-06-30 21:29:37 +02:00
|
|
|
$codebase->config->visitStubFiles($codebase);
|
|
|
|
|
2019-06-03 05:33:57 +02:00
|
|
|
if ($codebase->alter_code) {
|
|
|
|
$this->project_analyzer->interpretRefactors();
|
|
|
|
}
|
|
|
|
|
2019-08-18 22:59:56 +02:00
|
|
|
$this->project_analyzer->trackUnusedSuppressions();
|
2019-08-18 21:34:32 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$file_analyzer = new FileAnalyzer(
|
|
|
|
$this->project_analyzer,
|
2018-01-21 19:38:51 +01:00
|
|
|
$file_path,
|
2022-12-18 17:15:15 +01:00
|
|
|
$codebase->config->shortenFileName($file_path),
|
2018-01-21 19:38:51 +01:00
|
|
|
);
|
2018-11-11 18:01:14 +01:00
|
|
|
$file_analyzer->analyze($context);
|
2019-08-04 16:37:36 +02:00
|
|
|
|
2020-09-25 06:37:40 +02:00
|
|
|
if ($codebase->taint_flow_graph) {
|
|
|
|
$codebase->taint_flow_graph->connectSinksAndSources();
|
2019-08-04 16:37:36 +02:00
|
|
|
}
|
2019-08-18 20:27:50 +02:00
|
|
|
|
2019-08-18 21:34:32 +02:00
|
|
|
if ($track_unused_suppressions) {
|
2021-12-03 20:11:20 +01:00
|
|
|
IssueBuffer::processUnusedSuppressions($codebase->file_provider);
|
2019-08-18 20:27:50 +02:00
|
|
|
}
|
2018-01-21 16:22:04 +01:00
|
|
|
}
|
2018-07-23 01:29:04 +02:00
|
|
|
|
2022-12-15 03:26:17 +01:00
|
|
|
protected function getTestName(bool $withDataSet = true): string
|
2018-07-13 23:44:50 +02:00
|
|
|
{
|
2021-12-27 19:59:11 +01:00
|
|
|
return $this->getName($withDataSet);
|
2018-07-13 23:44:50 +02:00
|
|
|
}
|
2020-04-05 23:25:00 +02: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 01:06:33 +01:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
public static function assertArrayKeysAreZeroOrString(array $array, string $message = ''): void
|
|
|
|
{
|
2023-10-21 20:45:09 +02:00
|
|
|
$isZeroOrString = /** @param mixed $key */ static 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 01:06:33 +01: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 01:06:33 +01: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 01:06:33 +01: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-25 05:45:02 +02:00
|
|
|
}
|