Skip to content

Commit

Permalink
use consistent way to compare php version
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Nov 14, 2021
1 parent 6d21288 commit 0e1f34b
Show file tree
Hide file tree
Showing 93 changed files with 276 additions and 336 deletions.
2 changes: 1 addition & 1 deletion examples/TemplateScanner.php
Expand Up @@ -24,7 +24,7 @@ public function scan(
): void {
$stmts = $codebase->statements_provider->getStatementsForFile(
$file_storage->file_path,
'7.4',
70400,
$progress
);

Expand Down
26 changes: 25 additions & 1 deletion src/Psalm/Codebase.php
Expand Up @@ -35,6 +35,7 @@
use function explode;
use function implode;
use function in_array;
use function intdiv;
use function is_string;
use function krsort;
use function ksort;
Expand All @@ -48,6 +49,7 @@

use const PHP_MAJOR_VERSION;
use const PHP_MINOR_VERSION;
use const PHP_VERSION_ID;

class Codebase
{
Expand Down Expand Up @@ -270,14 +272,21 @@ class Codebase

/**
* @var int
* @deprecated will be removed in Psalm 5. Please use Codebase::$analysis_php_version_id
*/
public $php_major_version = PHP_MAJOR_VERSION;

/**
* @var int
* @deprecated will be removed in Psalm 5. Please use Codebase::$analysis_php_version_id
*/
public $php_minor_version = PHP_MINOR_VERSION;

/**
* @var int
*/
public $analysis_php_version_id = PHP_VERSION_ID;

/**
* @var bool
*/
Expand Down Expand Up @@ -489,7 +498,7 @@ public function getStatementsForFile(string $file_path): array
{
return $this->statements_provider->getStatementsForFile(
$file_path,
$this->php_major_version . '.' . $this->php_minor_version,
$this->analysis_php_version_id,
$this->progress
);
}
Expand Down Expand Up @@ -1967,4 +1976,19 @@ public function addTaintSink(

$this->taint_flow_graph->addSink($sink);
}

public function getMinorAnalysisPhpVersion(): int
{
return self::transformPhpVersionId($this->analysis_php_version_id % 10000, 100);
}

public function getMajorAnalysisPhpVersion(): int
{
return self::transformPhpVersionId($this->analysis_php_version_id, 10000);
}

public static function transformPhpVersionId(int $php_version_id, int $div): int
{
return intdiv($php_version_id, $div);
}
}
8 changes: 4 additions & 4 deletions src/Psalm/Config.php
Expand Up @@ -1821,7 +1821,7 @@ public function visitPreloadedStubFiles(Codebase $codebase, ?Progress $progress

$core_generic_files = [];

if (\PHP_VERSION_ID < 80000 && $codebase->php_major_version >= 8) {
if (\PHP_VERSION_ID < 80000 && $codebase->analysis_php_version_id >= 80000) {
$stringable_path = dirname(__DIR__, 2) . '/stubs/Php80.phpstub';

if (!file_exists($stringable_path)) {
Expand All @@ -1831,7 +1831,7 @@ public function visitPreloadedStubFiles(Codebase $codebase, ?Progress $progress
$core_generic_files[] = $stringable_path;
}

if (\PHP_VERSION_ID < 80100 && $codebase->php_major_version >= 8 && $codebase->php_minor_version >= 1) {
if (\PHP_VERSION_ID < 80100 && $codebase->analysis_php_version_id >= 80100) {
$stringable_path = dirname(__DIR__, 2) . '/stubs/Php81.phpstub';

if (!file_exists($stringable_path)) {
Expand Down Expand Up @@ -1882,12 +1882,12 @@ public function visitStubFiles(Codebase $codebase, ?Progress $progress = null):
$dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'SPL.phpstub',
];

if (\PHP_VERSION_ID >= 80000 && $codebase->php_major_version >= 8) {
if (\PHP_VERSION_ID >= 80000 && $codebase->analysis_php_version_id >= 80000) {
$stringable_path = $dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'Php80.phpstub';
$this->internal_stubs[] = $stringable_path;
}

if (\PHP_VERSION_ID >= 80100 && $codebase->php_major_version >= 8 && $codebase->php_minor_version >= 1) {
if (\PHP_VERSION_ID >= 80100 && $codebase->analysis_php_version_id >= 80100) {
$stringable_path = $dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'Php81.phpstub';
$this->internal_stubs[] = $stringable_path;
}
Expand Down
8 changes: 3 additions & 5 deletions src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Expand Up @@ -1617,8 +1617,7 @@ private static function addOrUpdatePropertyType(
$codebase = $project_analyzer->getCodebase();

$allow_native_type = !$docblock_only
&& $codebase->php_major_version >= 7
&& ($codebase->php_major_version > 7 || $codebase->php_minor_version >= 4)
&& $codebase->analysis_php_version_id >= 70400
&& $codebase->allow_backwards_incompatible_changes;

$manipulator->setType(
Expand All @@ -1627,8 +1626,7 @@ private static function addOrUpdatePropertyType(
$source->getNamespace(),
$source->getAliasedClassesFlipped(),
$source->getFQCLN(),
$codebase->php_major_version,
$codebase->php_minor_version
$codebase->analysis_php_version_id
) : null,
$inferred_type->toNamespacedString(
$source->getNamespace(),
Expand All @@ -1642,7 +1640,7 @@ private static function addOrUpdatePropertyType(
$source->getFQCLN(),
true
),
$inferred_type->canBeFullyExpressedInPhp($codebase->php_major_version, $codebase->php_minor_version)
$inferred_type->canBeFullyExpressedInPhp($codebase->analysis_php_version_id)
);
}

Expand Down
Expand Up @@ -942,7 +942,7 @@ private static function addOrUpdateReturnType(
}

$allow_native_type = !$docblock_only
&& $codebase->php_major_version >= 7
&& $codebase->analysis_php_version_id >= 70000
&& (
$codebase->allow_backwards_incompatible_changes
|| $is_final
Expand All @@ -955,8 +955,7 @@ private static function addOrUpdateReturnType(
$source->getNamespace(),
$source->getAliasedClassesFlipped(),
$source->getFQCLN(),
$codebase->php_major_version,
$codebase->php_minor_version
$codebase->analysis_php_version_id
) : null,
$inferred_return_type->toNamespacedString(
$source->getNamespace(),
Expand All @@ -970,7 +969,7 @@ private static function addOrUpdateReturnType(
$source->getFQCLN(),
true
),
$inferred_return_type->canBeFullyExpressedInPhp($codebase->php_major_version, $codebase->php_minor_version),
$inferred_return_type->canBeFullyExpressedInPhp($codebase->analysis_php_version_id),
$function_like_storage->return_type_description ?? null
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Expand Up @@ -1416,7 +1416,7 @@ public function addOrUpdateParamType(
}

$allow_native_type = !$docblock_only
&& $codebase->php_major_version >= 7
&& $codebase->analysis_php_version_id >= 70000
&& (
$codebase->allow_backwards_incompatible_changes
|| $is_final
Expand All @@ -1430,8 +1430,7 @@ public function addOrUpdateParamType(
$this->source->getNamespace(),
$this->source->getAliasedClassesFlipped(),
$this->source->getFQCLN(),
$project_analyzer->getCodebase()->php_major_version,
$project_analyzer->getCodebase()->php_minor_version
$project_analyzer->getCodebase()->analysis_php_version_id
) : null,
$inferred_return_type->toNamespacedString(
$this->source->getNamespace(),
Expand Down
14 changes: 5 additions & 9 deletions src/Psalm/Internal/Analyzer/MethodComparator.php
Expand Up @@ -82,7 +82,7 @@ public static function compare(
$cased_implementer_method_id,
$prevent_method_signature_mismatch,
$prevent_abstract_override,
$codebase->php_major_version >= 8,
$codebase->analysis_php_version_id >= 80000,
$code_location,
$suppressed_issues
);
Expand Down Expand Up @@ -565,9 +565,7 @@ private static function compareMethodSignatureParams(
$implementer_classlike_storage->parent_class
);

$is_contained_by = (($codebase->php_major_version === 7
&& $codebase->php_minor_version === 4)
|| $codebase->php_major_version >= 8)
$is_contained_by = $codebase->analysis_php_version_id >= 70400
&& $guide_param_signature_type
? UnionTypeComparator::isContainedBy(
$codebase,
Expand All @@ -581,7 +579,7 @@ private static function compareMethodSignatureParams(
if (!$is_contained_by) {
$config = \Psalm\Config::getInstance();

if ($codebase->php_major_version >= 8
if ($codebase->analysis_php_version_id >= 80000
|| $guide_classlike_storage->is_trait === $implementer_classlike_storage->is_trait
|| !in_array($guide_classlike_storage->name, $implementer_classlike_storage->used_traits)
|| $implementer_method_storage->defining_fqcln !== $implementer_classlike_storage->name
Expand Down Expand Up @@ -867,9 +865,7 @@ private static function compareMethodSignatureReturnTypes(
$implementer_classlike_storage->parent_class
) : null;

$is_contained_by = (($codebase->php_major_version === 7
&& $codebase->php_minor_version === 4)
|| $codebase->php_major_version >= 8)
$is_contained_by = $codebase->analysis_php_version_id >= 70400
&& $implementer_signature_return_type
? UnionTypeComparator::isContainedBy(
$codebase,
Expand All @@ -879,7 +875,7 @@ private static function compareMethodSignatureReturnTypes(
: UnionTypeComparator::isContainedByInPhp($implementer_signature_return_type, $guide_signature_return_type);

if (!$is_contained_by) {
if ($codebase->php_major_version >= 8
if ($codebase->analysis_php_version_id >= 80000
|| $guide_classlike_storage->is_trait === $implementer_classlike_storage->is_trait
|| !in_array($guide_classlike_storage->name, $implementer_classlike_storage->used_traits)
|| $implementer_method_storage->defining_fqcln !== $implementer_classlike_storage->name
Expand Down
9 changes: 4 additions & 5 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Expand Up @@ -1273,16 +1273,15 @@ public function setPhpVersion(string $version): void
$php_major_version = (int) $php_major_version;
$php_minor_version = (int) $php_minor_version;

if ($this->codebase->php_major_version !== $php_major_version
|| $this->codebase->php_minor_version !== $php_minor_version
) {
$analysis_php_version_id = $php_major_version * 10000 + $php_minor_version * 100;

if ($this->codebase->analysis_php_version_id !== $analysis_php_version_id) {
// reset lexer and parser when php version changes
\Psalm\Internal\Provider\StatementsProvider::clearLexer();
\Psalm\Internal\Provider\StatementsProvider::clearParser();
}

$this->codebase->php_major_version = $php_major_version;
$this->codebase->php_minor_version = $php_minor_version;
$this->codebase->analysis_php_version_id = $analysis_php_version_id;
}

/**
Expand Down
Expand Up @@ -490,9 +490,7 @@ private static function handleUnpackedArray(
if ($unpacked_atomic_type instanceof Type\Atomic\TKeyedArray) {
foreach ($unpacked_atomic_type->properties as $key => $property_value) {
if (\is_string($key)) {
if ($codebase->php_major_version < 8 ||
($codebase->php_major_version === 8 && $codebase->php_minor_version < 1)
) {
if ($codebase->analysis_php_version_id <= 80000) {
if (IssueBuffer::accepts(
new DuplicateArrayKey(
'String keys are not supported in unpacked arrays',
Expand Down Expand Up @@ -537,9 +535,7 @@ private static function handleUnpackedArray(
$array_creation_info->can_create_objectlike = false;

if ($unpacked_atomic_type->type_params[0]->hasString()) {
if ($codebase->php_major_version < 8 ||
($codebase->php_major_version === 8 && $codebase->php_minor_version < 1)
) {
if ($codebase->analysis_php_version_id <= 80000) {
if (IssueBuffer::accepts(
new DuplicateArrayKey(
'String keys are not supported in unpacked arrays',
Expand Down
Expand Up @@ -467,7 +467,7 @@ private static function checkFunctionLikeTypeMatches(

if ($function_param->is_variadic) {
$arg_type = $unpacked_atomic_array->getGenericValueType();
} elseif ($codebase->php_major_version >= 8
} elseif ($codebase->analysis_php_version_id >= 80000
&& $allow_named_args
&& isset($unpacked_atomic_array->properties[$function_param->name])
) {
Expand Down Expand Up @@ -535,7 +535,7 @@ private static function checkFunctionLikeTypeMatches(

continue;
}
if (($codebase->php_major_version < 8 || !$allow_named_args) && !$key_type->isInt()) {
if (($codebase->analysis_php_version_id < 80000 || !$allow_named_args) && !$key_type->isInt()) {
$invalid_string_key = true;

continue;
Expand Down Expand Up @@ -563,7 +563,7 @@ private static function checkFunctionLikeTypeMatches(
'Method ' . $cased_method_id
. ' called with unpacked iterable ' . $arg_type->getId()
. ' with invalid key (must be '
. ($codebase->php_major_version < 8 ? 'int' : 'int|string') . ')',
. ($codebase->analysis_php_version_id < 80000 ? 'int' : 'int|string') . ')',
new CodeLocation($statements_analyzer->getSource(), $arg->value),
$cased_method_id
),
Expand All @@ -573,7 +573,7 @@ private static function checkFunctionLikeTypeMatches(
}
}
if ($invalid_string_key) {
if ($codebase->php_major_version < 8) {
if ($codebase->analysis_php_version_id < 80000) {
if (IssueBuffer::accepts(
new $issue_type(
'String keys not supported in unpacked arguments',
Expand Down
Expand Up @@ -77,7 +77,7 @@ public static function fetch(
$template_result->lower_bounds[$template_name] = [
'fn-' . $function_id => [
new TemplateBound(
Type::getInt(false, $codebase->php_major_version)
Type::getInt(false, $codebase->getMajorAnalysisPhpVersion())
)
]
];
Expand All @@ -87,8 +87,7 @@ public static function fetch(
new TemplateBound(
Type::getInt(
false,
10000 * $codebase->php_major_version
+ 100 * $codebase->php_minor_version
$codebase->analysis_php_version_id
)
)
]
Expand Down
Expand Up @@ -544,7 +544,7 @@ public static function replaceTemplateTypes(
$template_result->lower_bounds[$template_type->param_name] = [
'fn-' . strtolower((string) $method_id) => [
new TemplateBound(
Type::getInt(false, $codebase->php_major_version)
Type::getInt(false, $codebase->getMajorAnalysisPhpVersion())
)
]
];
Expand All @@ -554,8 +554,7 @@ public static function replaceTemplateTypes(
new TemplateBound(
Type::getInt(
false,
10000 * $codebase->php_major_version
+ 100 * $codebase->php_minor_version
$codebase->analysis_php_version_id
)
)
]
Expand Down
Expand Up @@ -495,7 +495,7 @@ private static function getMethodReturnType(
$template_result->lower_bounds[$template_type->param_name] = [
'fn-' . strtolower((string)$method_id) => [
new TemplateBound(
Type::getInt(false, $codebase->php_major_version)
Type::getInt(false, $codebase->getMajorAnalysisPhpVersion())
)
]
];
Expand All @@ -505,8 +505,7 @@ private static function getMethodReturnType(
new TemplateBound(
Type::getInt(
false,
10000 * $codebase->php_major_version
+ 100 * $codebase->php_minor_version
$codebase->analysis_php_version_id
)
)
]
Expand Down
Expand Up @@ -242,7 +242,7 @@ public static function analyze(
}

if ($stmt instanceof PhpParser\Node\Expr\Cast\Unset_
&& $statements_analyzer->getCodebase()->php_major_version < 8
&& $statements_analyzer->getCodebase()->analysis_php_version_id <= 70400
) {
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
return false;
Expand Down

0 comments on commit 0e1f34b

Please sign in to comment.