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

Apply literal number separator rector #7299

Merged
merged 4 commits into from Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
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,
70400,
7_04_00,
$progress
);

Expand Down
7 changes: 7 additions & 0 deletions phpcs.xml
Expand Up @@ -190,4 +190,11 @@
</properties>
</rule>

<rule ref="SlevomatCodingStandard.Numbers.RequireNumericLiteralSeparator">
<properties>
<property name="enable" value="true"/>
<property name="minDigitsBeforeDecimalPoint" value="5"/>
</properties>
</rule>

</ruleset>
4 changes: 2 additions & 2 deletions src/Psalm/Codebase.php
Expand Up @@ -1999,12 +1999,12 @@ public function addTaintSink(

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

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

public static function transformPhpVersionId(int $php_version_id, int $div): int
Expand Down
10 changes: 5 additions & 5 deletions src/Psalm/Config.php
Expand Up @@ -522,7 +522,7 @@ class Config
/**
* @var int
*/
public $max_string_length = 1000;
public $max_string_length = 1_000;

/** @var ?IncludeCollector */
private $include_collector;
Expand Down Expand Up @@ -1865,7 +1865,7 @@ public function visitPreloadedStubFiles(Codebase $codebase, ?Progress $progress

$core_generic_files = [];

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

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

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

if (!file_exists($stringable_path)) {
Expand Down Expand Up @@ -1926,12 +1926,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->analysis_php_version_id >= 80000) {
if (PHP_VERSION_ID >= 8_00_00 && $codebase->analysis_php_version_id >= 8_00_00) {
$stringable_path = $dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'Php80.phpstub';
$this->internal_stubs[] = $stringable_path;
}

if (PHP_VERSION_ID >= 80100 && $codebase->analysis_php_version_id >= 80100) {
if (PHP_VERSION_ID >= 8_01_00 && $codebase->analysis_php_version_id >= 8_01_00) {
$stringable_path = $dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'Php81.phpstub';
$this->internal_stubs[] = $stringable_path;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Psalm/Internal/Algebra.php
Expand Up @@ -99,7 +99,7 @@ public static function simplifyCNF(array $clauses): array

//65536 seems to be a significant threshold, when put at 65537, the code https://psalm.dev/r/216f362ea6 goes
//from seconds in analysis to many minutes
if ($clause_count > 65536) {
if ($clause_count > 65_536) {
return [];
}

Expand Down Expand Up @@ -443,7 +443,7 @@ public static function groupImpossibilities(array $clauses): array

++$complexity;

if ($complexity > 20000) {
if ($complexity > 20_000) {
throw new ComplicatedExpressionException();
}
}
Expand All @@ -469,7 +469,7 @@ public static function combineOredClauses(
array $right_clauses,
int $conditional_object_id
): array {
if (count($left_clauses) > 60000 || count($right_clauses) > 60000) {
if (count($left_clauses) > 60_000 || count($right_clauses) > 60_000) {
return [];
}

Expand Down Expand Up @@ -601,7 +601,7 @@ function ($clause) {
);

if (!$clauses) {
$cond_id = mt_rand(0, 100000000);
$cond_id = mt_rand(0, 100_000_000);
return [new Clause([], $cond_id, $cond_id, true)];
}

Expand All @@ -616,14 +616,14 @@ function ($clause) {
$impossible_clauses = self::groupImpossibilities($clauses_with_impossibilities);

if (!$impossible_clauses) {
$cond_id = mt_rand(0, 100000000);
$cond_id = mt_rand(0, 100_000_000);
return [new Clause([], $cond_id, $cond_id, true)];
}

$negated = self::simplifyCNF($impossible_clauses);

if (!$negated) {
$cond_id = mt_rand(0, 100000000);
$cond_id = mt_rand(0, 100_000_000);
return [new Clause([], $cond_id, $cond_id, true)];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Expand Up @@ -1608,7 +1608,7 @@ private static function addOrUpdatePropertyType(
$codebase = $project_analyzer->getCodebase();

$allow_native_type = !$docblock_only
&& $codebase->analysis_php_version_id >= 70400
&& $codebase->analysis_php_version_id >= 7_04_00
&& $codebase->allow_backwards_incompatible_changes;

$manipulator->setType(
Expand Down
Expand Up @@ -940,7 +940,7 @@ private static function addOrUpdateReturnType(
}

$allow_native_type = !$docblock_only
&& $codebase->analysis_php_version_id >= 70000
&& $codebase->analysis_php_version_id >= 7_00_00
&& (
$codebase->allow_backwards_incompatible_changes
|| $is_final
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Expand Up @@ -1442,7 +1442,7 @@ public function addOrUpdateParamType(
}

$allow_native_type = !$docblock_only
&& $codebase->analysis_php_version_id >= 70000
&& $codebase->analysis_php_version_id >= 7_00_00
&& (
$codebase->allow_backwards_incompatible_changes
|| $is_final
Expand Down
10 changes: 5 additions & 5 deletions src/Psalm/Internal/Analyzer/MethodComparator.php
Expand Up @@ -95,7 +95,7 @@ public static function compare(
$cased_implementer_method_id,
$prevent_method_signature_mismatch,
$prevent_abstract_override,
$codebase->analysis_php_version_id >= 80000,
$codebase->analysis_php_version_id >= 8_00_00,
$code_location,
$suppressed_issues
);
Expand Down Expand Up @@ -562,7 +562,7 @@ private static function compareMethodSignatureParams(
$implementer_classlike_storage->parent_class
);

$is_contained_by = $codebase->analysis_php_version_id >= 70400
$is_contained_by = $codebase->analysis_php_version_id >= 7_04_00
&& $guide_param_signature_type
? UnionTypeComparator::isContainedBy(
$codebase,
Expand All @@ -576,7 +576,7 @@ private static function compareMethodSignatureParams(
if (!$is_contained_by) {
$config = Config::getInstance();

if ($codebase->analysis_php_version_id >= 80000
if ($codebase->analysis_php_version_id >= 8_00_00
|| $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 @@ -854,7 +854,7 @@ private static function compareMethodSignatureReturnTypes(
$implementer_classlike_storage->parent_class
) : null;

$is_contained_by = $codebase->analysis_php_version_id >= 70400
$is_contained_by = $codebase->analysis_php_version_id >= 7_04_00
&& $implementer_signature_return_type
? UnionTypeComparator::isContainedBy(
$codebase,
Expand All @@ -864,7 +864,7 @@ private static function compareMethodSignatureReturnTypes(
: UnionTypeComparator::isContainedByInPhp($implementer_signature_return_type, $guide_signature_return_type);

if (!$is_contained_by) {
if ($codebase->analysis_php_version_id >= 80000
if ($codebase->analysis_php_version_id >= 8_00_00
|| $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
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Expand Up @@ -1297,7 +1297,7 @@ public function setPhpVersion(string $version, string $source): void
$php_major_version = (int) $php_major_version;
$php_minor_version = (int) $php_minor_version;

$analysis_php_version_id = $php_major_version * 10000 + $php_minor_version * 100;
$analysis_php_version_id = $php_major_version * 10_000 + $php_minor_version * 100;

if ($this->codebase->analysis_php_version_id !== $analysis_php_version_id) {
// reset lexer and parser when php version changes
Expand Down
Expand Up @@ -341,7 +341,7 @@ function (array $carry, Clause $clause): array {

$reasonable_clause_count = count($if_scope->reasonable_clauses);

if ($reasonable_clause_count && $reasonable_clause_count < 20000 && $elseif_clauses) {
if ($reasonable_clause_count && $reasonable_clause_count < 20_000 && $elseif_clauses) {
$if_scope->reasonable_clauses = Algebra::combineOredClauses(
$if_scope->reasonable_clauses,
$elseif_clauses,
Expand Down
Expand Up @@ -510,7 +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->analysis_php_version_id <= 80000) {
if ($codebase->analysis_php_version_id <= 8_00_00) {
IssueBuffer::maybeAdd(
new DuplicateArrayKey(
'String keys are not supported in unpacked arrays',
Expand Down Expand Up @@ -553,7 +553,7 @@ private static function handleUnpackedArray(
$array_creation_info->can_create_objectlike = false;

if ($unpacked_atomic_type->type_params[0]->hasString()) {
if ($codebase->analysis_php_version_id <= 80000) {
if ($codebase->analysis_php_version_id <= 8_00_00) {
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->analysis_php_version_id >= 80000
} elseif ($codebase->analysis_php_version_id >= 8_00_00
&& $allow_named_args
&& isset($unpacked_atomic_array->properties[$function_param->name])
) {
Expand Down Expand Up @@ -552,7 +552,9 @@ private static function checkFunctionLikeTypeMatches(

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

continue;
Expand All @@ -578,15 +580,15 @@ private static function checkFunctionLikeTypeMatches(
'Method ' . $cased_method_id
. ' called with unpacked iterable ' . $arg_type->getId()
. ' with invalid key (must be '
. ($codebase->analysis_php_version_id < 80000 ? 'int' : 'int|string') . ')',
. ($codebase->analysis_php_version_id < 8_00_00 ? 'int' : 'int|string') . ')',
new CodeLocation($statements_analyzer->getSource(), $arg->value),
$cased_method_id
),
$statements_analyzer->getSuppressedIssues()
);
}
if ($invalid_string_key) {
if ($codebase->analysis_php_version_id < 80000) {
if ($codebase->analysis_php_version_id < 8_00_00) {
IssueBuffer::maybeAdd(
new $issue_type(
'String keys not supported in unpacked arguments',
Expand Down
Expand Up @@ -831,8 +831,8 @@ public static function applyAssertionsToContext(
);

$assert_clauses = FormulaGenerator::getFormula(
mt_rand(0, 1000000),
mt_rand(0, 1000000),
mt_rand(0, 1_000_000),
mt_rand(0, 1_000_000),
$conditional,
$context->self,
$statements_analyzer,
Expand Down
Expand Up @@ -264,7 +264,7 @@ public static function analyze(
}

if ($stmt instanceof PhpParser\Node\Expr\Cast\Unset_
&& $statements_analyzer->getCodebase()->analysis_php_version_id <= 70400
&& $statements_analyzer->getCodebase()->analysis_php_version_id <= 7_04_00
) {
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php
Expand Up @@ -473,17 +473,17 @@ private static function handleExpression(
$codebase = $statements_analyzer->getCodebase();
$analysis_php_version_id = $codebase->analysis_php_version_id;

if ($stmt instanceof PhpParser\Node\Expr\Match_ && $analysis_php_version_id >= 80000) {
if ($stmt instanceof PhpParser\Node\Expr\Match_ && $analysis_php_version_id >= 8_00_00) {
return MatchAnalyzer::analyze($statements_analyzer, $stmt, $context);
}

if ($stmt instanceof PhpParser\Node\Expr\Throw_ && $analysis_php_version_id >= 80000) {
if ($stmt instanceof PhpParser\Node\Expr\Throw_ && $analysis_php_version_id >= 8_00_00) {
return ThrowAnalyzer::analyze($statements_analyzer, $stmt, $context);
}

if (($stmt instanceof PhpParser\Node\Expr\NullsafePropertyFetch
|| $stmt instanceof PhpParser\Node\Expr\NullsafeMethodCall)
&& $analysis_php_version_id >= 80000
&& $analysis_php_version_id >= 8_00_00
) {
return NullsafeAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Cli/LanguageServer.php
Expand Up @@ -121,7 +121,7 @@ function (string $arg) use ($valid_long_options): void {
if (!array_key_exists('use-ini-defaults', $options)) {
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
ini_set('memory_limit', (string) (8 * 1024 * 1024 * 1024));
ini_set('memory_limit', (string) (8 * 1_024 * 1_024 * 1_024));
}

if (array_key_exists('help', $options)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Cli/Psalm.php
Expand Up @@ -475,7 +475,7 @@ private static function setMemoryLimit(array $options): void
ini_set('display_errors', 'stderr');
ini_set('display_startup_errors', '1');

$memoryLimit = (8 * 1024 * 1024 * 1024);
$memoryLimit = (8 * 1_024 * 1_024 * 1_024);

if (array_key_exists('memory-limit', $options)) {
$memoryLimit = $options['memory-limit'];
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Cli/Psalter.php
Expand Up @@ -424,8 +424,8 @@ private static function setMemoryLimit(): void
{
$memLimit = CliUtils::getMemoryLimitInBytes();
// Magic number is 4096M in bytes
if ($memLimit > 0 && $memLimit < 8 * 1024 * 1024 * 1024) {
ini_set('memory_limit', (string) (8 * 1024 * 1024 * 1024));
if ($memLimit > 0 && $memLimit < 8 * 1_024 * 1_024 * 1_024) {
ini_set('memory_limit', (string) (8 * 1_024 * 1_024 * 1_024));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Psalm/Internal/CliUtils.php
Expand Up @@ -456,13 +456,13 @@ public static function convertMemoryLimitToBytes(string $limit): int
$limit = (int)$matches[1];
switch (strtoupper($matches[2] ?? '')) {
case 'G':
$limit *= 1024 * 1024 * 1024;
$limit *= 1_024 * 1_024 * 1_024;
break;
case 'M':
$limit *= 1024 * 1024;
$limit *= 1_024 * 1_024;
break;
case 'K':
$limit *= 1024;
$limit *= 1_024;
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Fork/Pool.php
Expand Up @@ -266,7 +266,7 @@ public function __construct(

if ($bytes_written < $bytes_to_write) {
// wait a bit
usleep(500000);
usleep(500_000);
}
}

Expand Down Expand Up @@ -368,7 +368,7 @@ private function readResultsFromChildren(): array

// For each stream that was ready, read the content.
foreach ($needs_read as $file) {
$buffer = fread($file, 1024);
$buffer = fread($file, 1_024);
if ($buffer !== false) {
$content[(int)$file] .= $buffer;
}
Expand Down
Expand Up @@ -1077,7 +1077,7 @@ private function createStorageForFunctionLike(
if ($method_name_lc === strtolower($class_name)
&& !isset($classlike_storage->methods['__construct'])
&& strpos($fq_classlike_name, '\\') === false
&& $this->codebase->analysis_php_version_id <= 70400
&& $this->codebase->analysis_php_version_id <= 7_04_00
) {
$this->codebase->methods->setDeclaringMethodId(
$fq_classlike_name,
Expand Down