2019-05-02 18:15:38 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-05-02 18:15:38 +02:00
|
|
|
namespace Psalm\Tests\FileManipulation;
|
|
|
|
|
|
|
|
use Psalm\Context;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2021-07-02 01:10:21 +02:00
|
|
|
use Psalm\Internal\Provider\FakeFileProvider;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Provider\Providers;
|
2020-08-23 16:32:07 +02:00
|
|
|
use Psalm\Internal\RuntimeCaches;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Tests\TestCase;
|
2019-05-02 18:15:38 +02:00
|
|
|
use Psalm\Tests\TestConfig;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strpos;
|
2019-05-02 18:15:38 +02:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
abstract class FileManipulationTestCase extends TestCase
|
2019-05-02 18:15:38 +02:00
|
|
|
{
|
2022-12-16 19:58:47 +01:00
|
|
|
protected ProjectAnalyzer $project_analyzer;
|
2019-05-02 18:15:38 +02:00
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function setUp(): void
|
2019-05-02 18:15:38 +02:00
|
|
|
{
|
2020-08-23 16:32:07 +02:00
|
|
|
RuntimeCaches::clearAll();
|
2019-05-02 18:15:38 +02:00
|
|
|
|
2021-07-02 01:10:21 +02:00
|
|
|
$this->file_provider = new FakeFileProvider();
|
2019-05-02 18:15:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerValidCodeParse
|
|
|
|
* @param string[] $issues_to_fix
|
|
|
|
*/
|
2019-05-15 06:34:56 +02:00
|
|
|
public function testValidCode(
|
2022-01-13 19:49:37 +01:00
|
|
|
string $input_code,
|
|
|
|
string $output_code,
|
|
|
|
string $php_version,
|
2019-05-15 06:34:56 +02:00
|
|
|
array $issues_to_fix,
|
2022-01-13 19:49:37 +01:00
|
|
|
bool $safe_types,
|
2019-05-15 06:34:56 +02:00
|
|
|
bool $allow_backwards_incompatible_changes = true
|
2020-09-12 17:24:05 +02:00
|
|
|
): void {
|
2019-05-02 18:15:38 +02:00
|
|
|
$test_name = $this->getTestName();
|
|
|
|
if (strpos($test_name, 'SKIPPED-') !== false) {
|
|
|
|
$this->markTestSkipped('Skipped due to a bug.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = new TestConfig();
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
2019-05-02 18:15:38 +02:00
|
|
|
$config,
|
2021-12-03 20:11:20 +01:00
|
|
|
new Providers(
|
2019-05-02 18:15:38 +02:00
|
|
|
$this->file_provider,
|
2022-12-18 17:15:15 +01:00
|
|
|
new FakeParserCacheProvider(),
|
|
|
|
),
|
2019-05-02 18:15:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (empty($issues_to_fix)) {
|
|
|
|
$config->addPluginPath('examples/plugins/ClassUnqualifier.php');
|
|
|
|
$config->initializePlugins($this->project_analyzer);
|
|
|
|
}
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$file_path = self::$src_dir_path . 'somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
2022-12-18 17:15:15 +01:00
|
|
|
$input_code,
|
2019-05-02 18:15:38 +02:00
|
|
|
);
|
|
|
|
|
2021-11-27 01:06:33 +01:00
|
|
|
$this->project_analyzer->setPhpVersion($php_version, 'tests');
|
2019-05-02 18:15:38 +02:00
|
|
|
|
|
|
|
$keyed_issues_to_fix = [];
|
|
|
|
|
|
|
|
foreach ($issues_to_fix as $issue) {
|
|
|
|
$keyed_issues_to_fix[$issue] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->project_analyzer->setIssuesToFix($keyed_issues_to_fix);
|
|
|
|
$this->project_analyzer->alterCodeAfterCompletion(
|
|
|
|
false,
|
2022-12-18 17:15:15 +01:00
|
|
|
$safe_types,
|
2019-05-02 18:15:38 +02:00
|
|
|
);
|
2019-05-13 20:38:18 +02:00
|
|
|
$this->project_analyzer->getCodebase()->allow_backwards_incompatible_changes = $allow_backwards_incompatible_changes;
|
2022-05-21 17:39:51 +02:00
|
|
|
$this->project_analyzer->getConfig()->check_for_throws_docblock = true;
|
2019-05-02 18:15:38 +02:00
|
|
|
|
2020-11-22 00:25:18 +01:00
|
|
|
if (strpos(static::class, 'Unused') || strpos(static::class, 'Unnecessary')) {
|
|
|
|
$this->project_analyzer->getCodebase()->reportUnusedCode();
|
|
|
|
}
|
2019-05-02 18:15:38 +02:00
|
|
|
|
|
|
|
$this->analyzeFile($file_path, $context);
|
|
|
|
|
2019-12-02 21:24:01 +01:00
|
|
|
$this->project_analyzer->consolidateAnalyzedData();
|
2019-05-02 18:15:38 +02:00
|
|
|
|
|
|
|
$this->project_analyzer->getCodebase()->analyzer->updateFile($file_path, false);
|
|
|
|
$this->assertSame($output_code, $this->project_analyzer->getCodebase()->getFileContents($file_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return array<string,array{input:string,output:string,php_version:string,issues_to_fix:array<string>,safe_types:bool,allow_backwards_incompatible_changes?:bool}>
|
2019-05-02 18:15:38 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
abstract public function providerValidCodeParse(): array;
|
2019-05-02 18:15:38 +02:00
|
|
|
}
|