Skip to content

Commit

Permalink
feat: adjusts for project php version
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Jul 22, 2024
1 parent bc26906 commit 58a347f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results
Original file line number Diff line number Diff line change
@@ -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}}
{"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}}
37 changes: 36 additions & 1 deletion src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

namespace Pest\TypeCoverage;

use Pest\TestSuite;
use PHPStan\Analyser\Error as PHPStanError;

/**
* @internal
*/
final class Result
{
/**
* Either the project supports constant types or not.
*/
private static ?bool $supportsConstantTypes = null;

/**
* Creates a new result instance.
*
Expand Down Expand Up @@ -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];
}
}
Expand All @@ -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;
}
}

0 comments on commit 58a347f

Please sign in to comment.