Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Php version consistency #6898

Merged
merged 4 commits into from Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/TemplateScanner.php
Expand Up @@ -25,7 +25,7 @@ public function scan(
): void {
$stmts = $codebase->statements_provider->getStatementsForFile(
$file_storage->file_path,
'7.4',
70400,
$progress
);

Expand Down
32 changes: 17 additions & 15 deletions src/Psalm/Codebase.php
Expand Up @@ -77,6 +77,7 @@
use function explode;
use function implode;
use function in_array;
use function intdiv;
use function is_numeric;
use function is_string;
use function krsort;
Expand All @@ -90,8 +91,6 @@
use function substr;
use function substr_count;

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

class Codebase
Expand Down Expand Up @@ -305,18 +304,6 @@ class Codebase
*/
public $allow_backwards_incompatible_changes = true;

/**
* @var int
* @deprecated Removed in Psalm 5, use Codebase::$analysis_php_version_id
*/
public $php_major_version = PHP_MAJOR_VERSION;

/**
* @var int
* @deprecated Removed in Psalm 5, use Codebase::$analysis_php_version_id
*/
public $php_minor_version = PHP_MINOR_VERSION;

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

Expand Down Expand Up @@ -534,7 +521,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 @@ -2010,4 +1997,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 @@ -1871,7 +1871,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 @@ -1881,7 +1881,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 @@ -1932,12 +1932,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 @@ -1608,8 +1608,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 @@ -1618,8 +1617,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 @@ -1633,7 +1631,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 @@ -1443,7 +1443,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 @@ -1457,8 +1457,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 @@ -92,7 +92,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 @@ -559,9 +559,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 @@ -575,7 +573,7 @@ private static function compareMethodSignatureParams(
if (!$is_contained_by) {
$config = 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 @@ -853,9 +851,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 @@ -865,7 +861,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
12 changes: 5 additions & 7 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Expand Up @@ -569,7 +569,7 @@ private function generatePHPVersionMessage(): string
{
$codebase = $this->codebase;

$version = $codebase->php_major_version . '.' . $codebase->php_minor_version;
$version = $codebase->getMajorAnalysisPhpVersion() . '.' . $codebase->getMinorAnalysisPhpVersion();

switch ($codebase->php_version_source) {
case 'cli':
Expand Down Expand Up @@ -1297,17 +1297,15 @@ public function setPhpVersion(string $version, string $source): 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
StatementsProvider::clearLexer();
StatementsProvider::clearParser();
}

$this->codebase->php_major_version = $php_major_version;
$this->codebase->php_minor_version = $php_minor_version;
$this->codebase->analysis_php_version_id = $php_major_version * 10000 + $php_minor_version * 100;
$this->codebase->analysis_php_version_id = $analysis_php_version_id;
$this->codebase->php_version_source = $source;
}

Expand Down
Expand Up @@ -510,9 +510,7 @@ private static function handleUnpackedArray(
if ($unpacked_atomic_type instanceof 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) {
IssueBuffer::maybeAdd(
new DuplicateArrayKey(
'String keys are not supported in unpacked arrays',
Expand Down Expand Up @@ -555,9 +553,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) {
IssueBuffer::maybeAdd(
new DuplicateArrayKey(
'String keys are not supported in unpacked arrays',
Expand Down
Expand Up @@ -486,7 +486,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 @@ -552,7 +552,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 All @@ -578,15 +578,15 @@ 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
),
$statements_analyzer->getSuppressedIssues()
);
}
if ($invalid_string_key) {
if ($codebase->php_major_version < 8) {
if ($codebase->analysis_php_version_id < 80000) {
IssueBuffer::maybeAdd(
new $issue_type(
'String keys not supported in unpacked arguments',
Expand Down
Expand Up @@ -125,7 +125,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 @@ -135,8 +135,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 @@ -570,7 +570,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 @@ -580,8 +580,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 @@ -504,7 +504,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 @@ -514,8 +514,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 @@ -261,7 +261,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