From 016bff520438a17b8bfd72381421c9a3dfb90402 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Sat, 27 Nov 2021 02:06:33 +0200 Subject: [PATCH] Display target PHP version Historically it was often not quite clear to users what PHP version Psalm assumes, and why. This PR addresses this issue by printing the version and where we got it from right before scanning the files. --- src/Psalm/Codebase.php | 3 ++ src/Psalm/Config.php | 9 +++- .../Internal/Analyzer/ProjectAnalyzer.php | 37 +++++++++++++++- src/Psalm/Internal/Cli/Psalm.php | 18 +------- src/Psalm/Internal/Cli/Psalter.php | 12 +---- src/Psalm/Internal/CliUtils.php | 22 ++++++++++ tests/Config/ConfigTest.php | 2 +- tests/DocumentationTest.php | 4 +- tests/EndToEnd/PsalmEndToEndTest.php | 30 ++++++++++++- .../FileManipulationTestCase.php | 2 +- tests/FileReferenceTest.php | 2 +- tests/FileUpdates/AnalyzedMethodTest.php | 2 +- tests/FileUpdates/CachedStorageTest.php | 2 +- tests/FileUpdates/ErrorAfterUpdateTest.php | 2 +- tests/FileUpdates/ErrorFixTest.php | 2 +- tests/FileUpdates/TemporaryUpdateTest.php | 2 +- tests/ForbiddenCodeTest.php | 2 +- tests/LanguageServer/CompletionTest.php | 2 +- tests/LanguageServer/FileMapTest.php | 2 +- tests/LanguageServer/SymbolLookupTest.php | 2 +- tests/MethodSignatureTest.php | 12 ++--- tests/ProjectCheckerTest.php | 44 ++++++++++++++++--- tests/StubTest.php | 4 +- tests/TestCase.php | 12 ++--- tests/Traits/InvalidCodeAnalysisTestTrait.php | 2 +- tests/Traits/ValidCodeAnalysisTestTrait.php | 2 +- tests/TypeReconciliation/ValueTest.php | 2 +- tests/UnusedCodeTest.php | 4 +- tests/UnusedVariableTest.php | 2 +- tests/VariadicTest.php | 2 +- .../DummyProjectWithErrors/composer.json | 1 + 31 files changed, 172 insertions(+), 74 deletions(-) diff --git a/src/Psalm/Codebase.php b/src/Psalm/Codebase.php index ca16490678d..a0c8fc63ea9 100644 --- a/src/Psalm/Codebase.php +++ b/src/Psalm/Codebase.php @@ -278,6 +278,9 @@ class Codebase */ public $php_minor_version = PHP_MINOR_VERSION; + /** @var 'cli'|'config'|'composer'|'tests'|'runtime' */ + public $php_version_source = 'runtime'; + /** * @var bool */ diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index 5c90e119ab3..78c648e937b 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -2225,7 +2225,12 @@ public function addPreloadedStubFile(string $stub_file): void public function getPhpVersion(): ?string { - return $this->configured_php_version ?? $this->getPHPVersionFromComposerJson(); + return $this->getPhpVersionFromConfig() ?? $this->getPHPVersionFromComposerJson(); + } + + public function getPhpVersionFromConfig(): ?string + { + return $this->configured_php_version; } private function setBooleanAttribute(string $name, bool $value): void @@ -2237,7 +2242,7 @@ private function setBooleanAttribute(string $name, bool $value): void * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayAccess */ - private function getPHPVersionFromComposerJson(): ?string + public function getPHPVersionFromComposerJson(): ?string { $composer_json_path = Composer::getJsonFilePath($this->base_dir); diff --git a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php index d0d1766750e..7b8941d15b1 100644 --- a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php @@ -544,6 +544,33 @@ public function canReportIssues(string $file_path): bool return isset($this->project_files[$file_path]); } + private function generatePHPVersionMessage(): string + { + $codebase = $this->codebase; + + $version = $codebase->php_major_version . '.' . $codebase->php_minor_version; + + switch ($codebase->php_version_source) { + case 'cli': + $source = '(set by CLI argument)'; + break; + case 'config': + $source = '(set by config file)'; + break; + case 'composer': + $source = '(inferred from composer.json)'; + break; + case 'tests': + $source = '(set by tests)'; + break; + case 'runtime': + $source = '(inferred from current PHP version)'; + break; + } + + return "Target PHP version: $version $source\n"; + } + public function check(string $base_dir, bool $is_diff = false): void { $start_checks = (int)microtime(true); @@ -574,6 +601,7 @@ public function check(string $base_dir, bool $is_diff = false): void } } + $this->progress->write($this->generatePHPVersionMessage()); $this->progress->startScanningFiles(); $diff_no_files = false; @@ -992,6 +1020,7 @@ public function checkDir(string $dir_name): void $this->checkDirWithConfig($dir_name, $this->config, true); + $this->progress->write($this->generatePHPVersionMessage()); $this->progress->startScanningFiles(); $this->config->initializePlugins($this); @@ -1123,6 +1152,7 @@ public function checkFile(string $file_path): void $this->file_reference_provider->loadReferenceCache(); + $this->progress->write($this->generatePHPVersionMessage()); $this->progress->startScanningFiles(); $this->config->initializePlugins($this); @@ -1164,6 +1194,7 @@ public function checkPaths(array $paths_to_check): void $this->file_reference_provider->loadReferenceCache(); + $this->progress->write($this->generatePHPVersionMessage()); $this->progress->startScanningFiles(); $this->config->initializePlugins($this); @@ -1262,7 +1293,10 @@ public function refactorCodeAfterCompletion(array $to_refactor): void $this->show_issues = false; } - public function setPhpVersion(string $version): void + /** + * @param 'cli'|'config'|'composer'|'tests' $source + */ + public function setPhpVersion(string $version, string $source): void { if (!preg_match('/^(5\.[456]|7\.[01234]|8\.[01])(\..*)?$/', $version)) { throw new \UnexpectedValueException('Expecting a version number in the format x.y'); @@ -1283,6 +1317,7 @@ public function setPhpVersion(string $version): void $this->codebase->php_major_version = $php_major_version; $this->codebase->php_minor_version = $php_minor_version; + $this->codebase->php_version_source = $source; } /** diff --git a/src/Psalm/Internal/Cli/Psalm.php b/src/Psalm/Internal/Cli/Psalm.php index 61637edf112..8343c2c9ce8 100644 --- a/src/Psalm/Internal/Cli/Psalm.php +++ b/src/Psalm/Internal/Cli/Psalm.php @@ -332,8 +332,7 @@ function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\Class $progress ); - self::initPhpVersion($options, $config, $project_analyzer); - + CliUtils::initPhpVersion($options, $config, $project_analyzer); $start_time = microtime(true); @@ -1071,21 +1070,6 @@ function (array $edges) { } } - private static function initPhpVersion(array $options, Config $config, ProjectAnalyzer $project_analyzer): void - { - if (!isset($options['php-version'])) { - $options['php-version'] = $config->getPhpVersion(); - } - - if (isset($options['php-version'])) { - if (!is_string($options['php-version'])) { - die('Expecting a version number in the format x.y' . PHP_EOL); - } - - $project_analyzer->setPhpVersion($options['php-version']); - } - } - /** @return false|'always'|'auto' */ private static function shouldFindUnusedCode(array $options, Config $config) { diff --git a/src/Psalm/Internal/Cli/Psalter.php b/src/Psalm/Internal/Cli/Psalter.php index ff292737477..528a5ec8978 100644 --- a/src/Psalm/Internal/Cli/Psalter.php +++ b/src/Psalm/Internal/Cli/Psalter.php @@ -281,17 +281,7 @@ function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\Class $keyed_issues = []; } - if (!isset($options['php-version'])) { - $options['php-version'] = $config->getPhpVersion(); - } - - if (isset($options['php-version'])) { - if (!is_string($options['php-version'])) { - die('Expecting a version number in the format x.y' . PHP_EOL); - } - - $project_analyzer->setPhpVersion($options['php-version']); - } + CliUtils::initPhpVersion($options, $config, $project_analyzer); if (isset($options['codeowner'])) { $codeowner_files = self::loadCodeowners($providers); diff --git a/src/Psalm/Internal/CliUtils.php b/src/Psalm/Internal/CliUtils.php index 821c48ffb72..6091e1b6404 100644 --- a/src/Psalm/Internal/CliUtils.php +++ b/src/Psalm/Internal/CliUtils.php @@ -6,6 +6,7 @@ use Phar; use Psalm\Config; use Psalm\Internal\Composer; +use Psalm\Internal\Analyzer\ProjectAnalyzer; use function assert; use function count; @@ -622,4 +623,25 @@ public static function convertMemoryLimitToBytes(string $limit): int return (int)$limit; } + + public static function initPhpVersion(array $options, Config $config, ProjectAnalyzer $project_analyzer): void + { + $source = null; + + if (isset($options['php-version'])) { + if (!is_string($options['php-version'])) { + die('Expecting a version number in the format x.y' . PHP_EOL); + } + $version = $options['php-version']; + $source = 'cli'; + } elseif ($version = $config->getPhpVersionFromConfig()) { + $source = 'config'; + } elseif ($version = $config->getPHPVersionFromComposerJson()) { + $source = 'composer'; + } + + if ($version !== null && $source !== null) { + $project_analyzer->setPhpVersion($version, $source); + } + } } diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index ef353744a12..42ace10a0f7 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -67,7 +67,7 @@ private function getProjectAnalyzerWithConfig(Config $config): \Psalm\Internal\A ) ); - $p->setPhpVersion('7.3'); + $p->setPhpVersion('7.3', 'tests'); return $p; } diff --git a/tests/DocumentationTest.php b/tests/DocumentationTest.php index 6ba345ca42b..0f13819025c 100644 --- a/tests/DocumentationTest.php +++ b/tests/DocumentationTest.php @@ -136,7 +136,7 @@ public function setUp() : void ) ); - $this->project_analyzer->setPhpVersion('8.0'); + $this->project_analyzer->setPhpVersion('8.0', 'tests'); } public function testAllIssuesCoveredInConfigSchema(): void @@ -204,7 +204,7 @@ public function testInvalidCode($code, $error_message, $error_levels = [], $chec $this->markTestSkipped(); } - $this->project_analyzer->setPhpVersion($php_version); + $this->project_analyzer->setPhpVersion($php_version, 'tests'); if ($check_references) { $this->project_analyzer->getCodebase()->reportUnusedCode(); diff --git a/tests/EndToEnd/PsalmEndToEndTest.php b/tests/EndToEnd/PsalmEndToEndTest.php index a9749e8e7b8..3d8449a2ff2 100644 --- a/tests/EndToEnd/PsalmEndToEndTest.php +++ b/tests/EndToEnd/PsalmEndToEndTest.php @@ -115,6 +115,10 @@ public function testPsalm(): void { $this->runPsalmInit(1); $result = $this->runPsalm([], self::$tmpDir, true); + $this->assertStringContainsString( + 'Target PHP version: 7.1 (inferred from composer.json)', + $result['STDERR'] + ); $this->assertStringContainsString('UnusedParam', $result['STDOUT']); $this->assertStringContainsString('InvalidReturnType', $result['STDOUT']); $this->assertStringContainsString('InvalidReturnStatement', $result['STDOUT']); @@ -122,6 +126,26 @@ public function testPsalm(): void $this->assertSame(2, $result['CODE']); } + public function testPsalmWithPHPVersionOverride(): void + { + $this->runPsalmInit(1); + $result = $this->runPsalm(['--php-version=8.0'], self::$tmpDir, true); + $this->assertStringContainsString( + 'Target PHP version: 8.0 (set by CLI argument)', + $result['STDERR'] + ); + } + + public function testPsalmWithPHPVersionFromConfig(): void + { + $this->runPsalmInit(1, '7.4'); + $result = $this->runPsalm([], self::$tmpDir, true); + $this->assertStringContainsString( + 'Target PHP version: 7.4 (set by config file)', + $result['STDERR'] + ); + } + public function testPsalmDiff(): void { if (PHP_VERSION_ID < 70400) { @@ -212,7 +236,7 @@ public function testLegacyConfigWithoutresolveFromConfigFile(): void /** * @return array{STDOUT: string, STDERR: string, CODE: int|null} */ - private function runPsalmInit(?int $level = null): array + private function runPsalmInit(?int $level = null, ?string $php_version = null): array { $args = ['--init']; @@ -226,7 +250,9 @@ private function runPsalmInit(?int $level = null): array $psalm_config_contents = file_get_contents(self::$tmpDir . '/psalm.xml'); $psalm_config_contents = \str_replace( 'errorLevel="1"', - 'errorLevel="1" cacheDirectory="' . self::$tmpDir . '/cache"', + 'errorLevel="1" ' + . 'cacheDirectory="' . self::$tmpDir . '/cache" ' + . ($php_version ? ('phpVersion="' . $php_version . '"') : ''), $psalm_config_contents ); file_put_contents(self::$tmpDir . '/psalm.xml', $psalm_config_contents); diff --git a/tests/FileManipulation/FileManipulationTestCase.php b/tests/FileManipulation/FileManipulationTestCase.php index 3088e23cb4e..ad8bfe97b9c 100644 --- a/tests/FileManipulation/FileManipulationTestCase.php +++ b/tests/FileManipulation/FileManipulationTestCase.php @@ -68,7 +68,7 @@ public function testValidCode( $input_code ); - $this->project_analyzer->setPhpVersion($php_version); + $this->project_analyzer->setPhpVersion($php_version, 'tests'); $keyed_issues_to_fix = []; diff --git a/tests/FileReferenceTest.php b/tests/FileReferenceTest.php index 8bf40381953..23ab9ca9d42 100644 --- a/tests/FileReferenceTest.php +++ b/tests/FileReferenceTest.php @@ -29,7 +29,7 @@ public function setUp() : void ); $this->project_analyzer->getCodebase()->collectLocations(); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); } /** diff --git a/tests/FileUpdates/AnalyzedMethodTest.php b/tests/FileUpdates/AnalyzedMethodTest.php index 59fc3726d91..ac19d581226 100644 --- a/tests/FileUpdates/AnalyzedMethodTest.php +++ b/tests/FileUpdates/AnalyzedMethodTest.php @@ -36,7 +36,7 @@ public function setUp() : void $config, $providers ); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); } /** diff --git a/tests/FileUpdates/CachedStorageTest.php b/tests/FileUpdates/CachedStorageTest.php index 21e410636cf..c711268c945 100644 --- a/tests/FileUpdates/CachedStorageTest.php +++ b/tests/FileUpdates/CachedStorageTest.php @@ -36,7 +36,7 @@ public function setUp() : void $config, $providers ); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); } public function testValidInclude(): void diff --git a/tests/FileUpdates/ErrorAfterUpdateTest.php b/tests/FileUpdates/ErrorAfterUpdateTest.php index 593ca3ca8ff..36d15c06576 100644 --- a/tests/FileUpdates/ErrorAfterUpdateTest.php +++ b/tests/FileUpdates/ErrorAfterUpdateTest.php @@ -37,7 +37,7 @@ public function setUp() : void $config, $providers ); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); } /** diff --git a/tests/FileUpdates/ErrorFixTest.php b/tests/FileUpdates/ErrorFixTest.php index fd57630f9f5..97c6cd6f5b9 100644 --- a/tests/FileUpdates/ErrorFixTest.php +++ b/tests/FileUpdates/ErrorFixTest.php @@ -37,7 +37,7 @@ public function setUp() : void $config, $providers ); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); } /** diff --git a/tests/FileUpdates/TemporaryUpdateTest.php b/tests/FileUpdates/TemporaryUpdateTest.php index 0f089636b95..06a0aa576b2 100644 --- a/tests/FileUpdates/TemporaryUpdateTest.php +++ b/tests/FileUpdates/TemporaryUpdateTest.php @@ -39,7 +39,7 @@ public function setUp() : void $config, $providers ); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); } /** diff --git a/tests/ForbiddenCodeTest.php b/tests/ForbiddenCodeTest.php index c41fdfe5448..60a182e2609 100644 --- a/tests/ForbiddenCodeTest.php +++ b/tests/ForbiddenCodeTest.php @@ -361,7 +361,7 @@ private function getProjectAnalyzerWithConfig(Config $config): \Psalm\Internal\A ) ); - $p->setPhpVersion('7.4'); + $p->setPhpVersion('7.4', 'tests'); return $p; } diff --git a/tests/LanguageServer/CompletionTest.php b/tests/LanguageServer/CompletionTest.php index 6698224a2fa..e8357216fdb 100644 --- a/tests/LanguageServer/CompletionTest.php +++ b/tests/LanguageServer/CompletionTest.php @@ -35,7 +35,7 @@ public function setUp() : void $config, $providers ); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); $this->project_analyzer->getCodebase()->store_node_types = true; } diff --git a/tests/LanguageServer/FileMapTest.php b/tests/LanguageServer/FileMapTest.php index 3c03a40c731..be390848b4a 100644 --- a/tests/LanguageServer/FileMapTest.php +++ b/tests/LanguageServer/FileMapTest.php @@ -31,7 +31,7 @@ public function setUp() : void $config, $providers ); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); $this->project_analyzer->getCodebase()->store_node_types = true; } diff --git a/tests/LanguageServer/SymbolLookupTest.php b/tests/LanguageServer/SymbolLookupTest.php index 063229e0558..4d32be4608c 100644 --- a/tests/LanguageServer/SymbolLookupTest.php +++ b/tests/LanguageServer/SymbolLookupTest.php @@ -34,7 +34,7 @@ public function setUp() : void $providers ); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); $this->project_analyzer->getCodebase()->store_node_types = true; } diff --git a/tests/MethodSignatureTest.php b/tests/MethodSignatureTest.php index 4d89bdc89a6..0d302855e0b 100644 --- a/tests/MethodSignatureTest.php +++ b/tests/MethodSignatureTest.php @@ -103,7 +103,7 @@ public function testMismatchingCovariantReturnIn73(): void $this->expectExceptionMessage('MethodSignatureMismatch'); $this->expectException(\Psalm\Exception\CodeException::class); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); $this->addFile( 'somefile.php', @@ -127,7 +127,7 @@ class D extends C {}' public function testMismatchingCovariantReturnIn74(): void { - $this->project_analyzer->setPhpVersion('7.4'); + $this->project_analyzer->setPhpVersion('7.4', 'tests'); $this->addFile( 'somefile.php', @@ -154,7 +154,7 @@ public function testMismatchingCovariantReturnIn73WithSelf(): void $this->expectExceptionMessage('MethodSignatureMismatch'); $this->expectException(\Psalm\Exception\CodeException::class); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); $this->addFile( 'somefile.php', @@ -176,7 +176,7 @@ function foo(): self { public function testMismatchingCovariantReturnIn74WithSelf(): void { - $this->project_analyzer->setPhpVersion('7.4'); + $this->project_analyzer->setPhpVersion('7.4', 'tests'); $this->addFile( 'somefile.php', @@ -201,7 +201,7 @@ public function testMismatchingCovariantParamIn73(): void $this->expectExceptionMessage('MethodSignatureMismatch'); $this->expectException(\Psalm\Exception\CodeException::class); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); $this->addFile( 'somefile.php', @@ -222,7 +222,7 @@ class D extends C {}' public function testMismatchingCovariantParamIn74(): void { - $this->project_analyzer->setPhpVersion('7.4'); + $this->project_analyzer->setPhpVersion('7.4', 'tests'); $this->addFile( 'somefile.php', diff --git a/tests/ProjectCheckerTest.php b/tests/ProjectCheckerTest.php index 1945e923323..794b81611ac 100644 --- a/tests/ProjectCheckerTest.php +++ b/tests/ProjectCheckerTest.php @@ -2,6 +2,7 @@ namespace Psalm\Tests; use Psalm\Config; +use Psalm\Internal\Analyzer\ProjectAnalyzer; use Psalm\Internal\IncludeCollector; use Psalm\Internal\Provider\FakeFileProvider; use Psalm\Internal\RuntimeCaches; @@ -27,7 +28,7 @@ class ProjectCheckerTest extends TestCase /** @var TestConfig */ protected static $config; - /** @var \Psalm\Internal\Analyzer\ProjectAnalyzer */ + /** @var ProjectAnalyzer */ protected $project_analyzer; public static function setUpBeforeClass() : void @@ -49,7 +50,7 @@ public function setUp() : void $this->file_provider = new FakeFileProvider(); } - private function getProjectAnalyzerWithConfig(Config $config): \Psalm\Internal\Analyzer\ProjectAnalyzer + private function getProjectAnalyzerWithConfig(Config $config): ProjectAnalyzer { $config->setIncludeCollector(new IncludeCollector()); return new \Psalm\Internal\Analyzer\ProjectAnalyzer( @@ -79,6 +80,7 @@ public function testCheck(): void ' ) ); + $this->project_analyzer->setPhpVersion('8.1', 'tests'); $this->project_analyzer->progress = new EchoProgress(); @@ -86,7 +88,13 @@ public function testCheck(): void $this->project_analyzer->check('tests/fixtures/DummyProject'); $output = ob_get_clean(); - $this->assertSame('Scanning files...' . "\n" . 'Analyzing files...' . "\n\n", $output); + $this->assertSame( + 'Target PHP version: 8.1 (set by tests)' . "\n" + . 'Scanning files...' . "\n" + . 'Analyzing files...' . "\n" + . "\n", + $output + ); $this->assertSame(0, \Psalm\IssueBuffer::getErrorCount()); @@ -261,13 +269,21 @@ public function testCheckDir(): void ) ); + $this->project_analyzer->setPhpVersion('8.1', 'tests'); + $this->project_analyzer->progress = new EchoProgress(); ob_start(); $this->project_analyzer->checkDir('tests/fixtures/DummyProject'); $output = ob_get_clean(); - $this->assertSame('Scanning files...' . "\n" . 'Analyzing files...' . "\n\n", $output); + $this->assertSame( + 'Target PHP version: 8.1 (set by tests)' . "\n" + . 'Scanning files...' . "\n" + . 'Analyzing files...' . "\n" + . "\n", + $output + ); $this->assertSame(0, \Psalm\IssueBuffer::getErrorCount()); @@ -293,6 +309,8 @@ public function testCheckPaths(): void ) ); + $this->project_analyzer->setPhpVersion('8.1', 'tests'); + $this->project_analyzer->progress = new EchoProgress(); ob_start(); @@ -304,7 +322,13 @@ public function testCheckPaths(): void ]); $output = ob_get_clean(); - $this->assertSame('Scanning files...' . "\n" . 'Analyzing files...' . "\n\n", $output); + $this->assertSame( + 'Target PHP version: 8.1 (set by tests)' . "\n" + . 'Scanning files...' . "\n" + . 'Analyzing files...' . "\n" + . "\n", + $output + ); $this->assertSame(0, \Psalm\IssueBuffer::getErrorCount()); @@ -330,6 +354,8 @@ public function testCheckFile(): void ) ); + $this->project_analyzer->setPhpVersion('8.1', 'tests'); + $this->project_analyzer->progress = new EchoProgress(); ob_start(); @@ -341,7 +367,13 @@ public function testCheckFile(): void ]); $output = ob_get_clean(); - $this->assertSame('Scanning files...' . "\n" . 'Analyzing files...' . "\n\n", $output); + $this->assertSame( + 'Target PHP version: 8.1 (set by tests)' . "\n" + . 'Scanning files...' . "\n" + . 'Analyzing files...' . "\n" + . "\n", + $output + ); $this->assertSame(0, \Psalm\IssueBuffer::getErrorCount()); diff --git a/tests/StubTest.php b/tests/StubTest.php index 4b6b43a6fdd..5d9773c0f65 100644 --- a/tests/StubTest.php +++ b/tests/StubTest.php @@ -50,7 +50,7 @@ private function getProjectAnalyzerWithConfig(Config $config): \Psalm\Internal\A new Provider\FakeParserCacheProvider() ) ); - $project_analyzer->setPhpVersion('7.4'); + $project_analyzer->setPhpVersion('7.4', 'tests'); $config->setIncludeCollector(new IncludeCollector()); $config->visitComposerAutoloadFiles($project_analyzer, null); @@ -852,7 +852,7 @@ public function testVersionDependentStubs(string $php_version, string $code): vo ' ) ); - $this->project_analyzer->setPhpVersion($php_version); + $this->project_analyzer->setPhpVersion($php_version, 'tests'); $file_path = getcwd() . '/src/somefile.php'; diff --git a/tests/TestCase.php b/tests/TestCase.php index 1b2042caced..395c8ad65d5 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -82,7 +82,7 @@ public function setUp() : void $providers ); - $this->project_analyzer->setPhpVersion('7.4'); + $this->project_analyzer->setPhpVersion('7.4', 'tests'); } public function tearDown() : void @@ -180,13 +180,13 @@ public static function assertRegExp(string $pattern, string $string, string $mes parent::assertRegExp($pattern, $string, $message); } } - + 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); } - + public static function assertArrayKeysAreZeroOrString(array $array, string $message = ''): void { $isZeroOrString = /** @param mixed $key */ function ($key): bool { @@ -195,19 +195,19 @@ public static function assertArrayKeysAreZeroOrString(array $array, string $mess $validKeys = array_filter($array, $isZeroOrString, ARRAY_FILTER_USE_KEY); self::assertTrue(count($array) === count($validKeys), $message); } - + public static function assertArrayValuesAreArrays(array $array, string $message = ''): void { $validValues = array_filter($array, 'is_array'); self::assertTrue(count($array) === count($validValues), $message); } - + public static function assertArrayValuesAreStrings(array $array, string $message = ''): void { $validValues = array_filter($array, 'is_string'); self::assertTrue(count($array) === count($validValues), $message); } - + public static function assertStringIsParsableType(string $type, string $message = ''): void { if ($type === '') { diff --git a/tests/Traits/InvalidCodeAnalysisTestTrait.php b/tests/Traits/InvalidCodeAnalysisTestTrait.php index 9efd6876294..937c60ba751 100644 --- a/tests/Traits/InvalidCodeAnalysisTestTrait.php +++ b/tests/Traits/InvalidCodeAnalysisTestTrait.php @@ -66,7 +66,7 @@ public function testInvalidCode( Config::getInstance()->setCustomErrorLevel($issue_name, $error_level); } - $this->project_analyzer->setPhpVersion($php_version); + $this->project_analyzer->setPhpVersion($php_version, 'tests'); $file_path = self::$src_dir_path . 'somefile.php'; diff --git a/tests/Traits/ValidCodeAnalysisTestTrait.php b/tests/Traits/ValidCodeAnalysisTestTrait.php index 2769e58bbcd..df70554e87a 100644 --- a/tests/Traits/ValidCodeAnalysisTestTrait.php +++ b/tests/Traits/ValidCodeAnalysisTestTrait.php @@ -68,7 +68,7 @@ public function testValidCode( $context = new Context(); - $this->project_analyzer->setPhpVersion($php_version); + $this->project_analyzer->setPhpVersion($php_version, 'tests'); $codebase = $this->project_analyzer->getCodebase(); $codebase->config->visitPreloadedStubFiles($codebase); diff --git a/tests/TypeReconciliation/ValueTest.php b/tests/TypeReconciliation/ValueTest.php index 63cc2be669a..21a6ee5ad59 100644 --- a/tests/TypeReconciliation/ValueTest.php +++ b/tests/TypeReconciliation/ValueTest.php @@ -23,7 +23,7 @@ public function setUp() : void ) ); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); } /** diff --git a/tests/UnusedCodeTest.php b/tests/UnusedCodeTest.php index 3b928ce66e7..5b7330454b6 100644 --- a/tests/UnusedCodeTest.php +++ b/tests/UnusedCodeTest.php @@ -32,7 +32,7 @@ public function setUp() : void ); $this->project_analyzer->getCodebase()->reportUnusedCode(); - $this->project_analyzer->setPhpVersion('7.3'); + $this->project_analyzer->setPhpVersion('7.3', 'tests'); } /** @@ -56,7 +56,7 @@ public function testValidCode($code, array $error_levels = []): void $code ); - $this->project_analyzer->setPhpVersion('8.0'); + $this->project_analyzer->setPhpVersion('8.0', 'tests'); foreach ($error_levels as $error_level) { $this->project_analyzer->getCodebase()->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS); diff --git a/tests/UnusedVariableTest.php b/tests/UnusedVariableTest.php index f9235c698b3..0bd087accff 100644 --- a/tests/UnusedVariableTest.php +++ b/tests/UnusedVariableTest.php @@ -31,7 +31,7 @@ public function setUp() : void ) ); - $this->project_analyzer->setPhpVersion('7.4'); + $this->project_analyzer->setPhpVersion('7.4', 'tests'); $this->project_analyzer->getCodebase()->reportUnusedVariables(); } diff --git a/tests/VariadicTest.php b/tests/VariadicTest.php index c33dcb44283..93d0cb85194 100644 --- a/tests/VariadicTest.php +++ b/tests/VariadicTest.php @@ -136,7 +136,7 @@ private function getProjectAnalyzerWithConfig(Config $config): \Psalm\Internal\A new Provider\FakeParserCacheProvider() ) ); - $project_analyzer->setPhpVersion('7.3'); + $project_analyzer->setPhpVersion('7.3', 'tests'); $config->setIncludeCollector(new IncludeCollector()); $config->visitComposerAutoloadFiles($project_analyzer, null); diff --git a/tests/fixtures/DummyProjectWithErrors/composer.json b/tests/fixtures/DummyProjectWithErrors/composer.json index 94b372ebc77..af6d9be31ad 100644 --- a/tests/fixtures/DummyProjectWithErrors/composer.json +++ b/tests/fixtures/DummyProjectWithErrors/composer.json @@ -3,6 +3,7 @@ "description": "A sample project to be used when testing Psalm", "type": "project", "require": { + "php": ">= 7.1", "vimeo/psalm": "^4.3" }, "autoload": {