From 58a347fcb0953f998543ab444ff72f0c75f3dc42 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 22 Jul 2024 01:39:16 +0100 Subject: [PATCH] feat: adjusts for project php version --- .phpunit.cache/test-results | 2 +- src/Result.php | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/.phpunit.cache/test-results b/.phpunit.cache/test-results index 19d77fa..99b6808 100644 --- a/.phpunit.cache/test-results +++ b/.phpunit.cache/test-results @@ -1 +1 @@ -{"version":"pest_3.0.0-dev-0008","defects":{"P\\Tests\\Plugin::__pest_evaluable_it_can_output_to_json":7,"P\\Tests\\Plugin::__pest_evaluable_output":7},"times":{"P\\Tests\\Plugin::__pest_evaluable_output":6.946,"P\\Tests\\Plugin::__pest_evaluable_it_can_output_to_json":1.905,"P\\Tests\\Support\\ConfigurationSourceDetector::__pest_evaluable_it_detects_the_source_of_the_application":0.001}} \ No newline at end of file +{"version":"pest_3.0.0-dev-0008","defects":{"P\\Tests\\Plugin::__pest_evaluable_it_can_output_to_json":7,"P\\Tests\\Plugin::__pest_evaluable_output":7},"times":{"P\\Tests\\Plugin::__pest_evaluable_output":6.479,"P\\Tests\\Plugin::__pest_evaluable_it_can_output_to_json":1.683,"P\\Tests\\Support\\ConfigurationSourceDetector::__pest_evaluable_it_detects_the_source_of_the_application":0.001}} \ No newline at end of file diff --git a/src/Result.php b/src/Result.php index acb7267..3cb67a7 100644 --- a/src/Result.php +++ b/src/Result.php @@ -4,6 +4,7 @@ namespace Pest\TypeCoverage; +use Pest\TestSuite; use PHPStan\Analyser\Error as PHPStanError; /** @@ -11,6 +12,11 @@ */ final class Result { + /** + * Either the project supports constant types or not. + */ + private static ?bool $supportsConstantTypes = null; + /** * Creates a new result instance. * @@ -72,7 +78,7 @@ public static function fromPHPStanErrors(string $file, array $phpstanErrors, arr $returnTypeCoverage = (int) explode(' ', explode('only ', $message)[1])[2]; } - if (str_contains($error->getMessage(), 'constant types')) { + if (self::supportsConstantTypes() && str_contains($error->getMessage(), 'constant types')) { $constantsCoverage = (int) explode(' ', explode('only ', $message)[1])[2]; } } @@ -88,4 +94,33 @@ public static function fromPHPStanErrors(string $file, array $phpstanErrors, arr (int) round(($propertyCoverage + $paramCoverage + $returnTypeCoverage + $constantsCoverage) / 4, mode: PHP_ROUND_HALF_DOWN), ); } + + /** + * Either the project supports constant types or not. + */ + public static function supportsConstantTypes(): bool + { + if (self::$supportsConstantTypes !== null) { + return self::$supportsConstantTypes; + } + + $rootPath = TestSuite::getInstance()->rootPath; + $fallback = version_compare(PHP_VERSION, '8.3.0', '>='); + + $composerJson = json_decode(file_get_contents($rootPath.'/composer.json'), true); + + if (! is_array ($composerJson) || ! array_key_exists('require', $composerJson)) { + return $fallback; + } + + if (! array_key_exists('php', $composerJson['require'])) { + return $fallback; + } + + $phpVersion = ltrim($composerJson['require']['php'], '^>=~'); + + $isOwnPackage = $composerJson['name'] ?? '' === 'pestphp/pest-plugin-type-coverage'; + + return version_compare($phpVersion, '8.3.0', '>=') || $isOwnPackage; + } }