Skip to content

Commit

Permalink
improve syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Nov 13, 2021
1 parent 6752c54 commit d251fd8
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 24 deletions.
30 changes: 30 additions & 0 deletions src/Psalm/Codebase.php
Original file line number Diff line number Diff line change
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 @@ -46,6 +47,8 @@
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 @@ -267,6 +270,18 @@ class Codebase
*/
public $allow_backwards_incompatible_changes = true;

/**
* @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
*/
Expand Down Expand Up @@ -1961,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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ public static function fetch(
]
];
} elseif ($template_name === 'TPhpMajorVersion') {
$analysis_php_version_id = (string) $codebase->analysis_php_version_id;
$template_result->lower_bounds[$template_name] = [
'fn-' . $function_id => [
new TemplateBound(
Type::getInt(false, (int) $analysis_php_version_id[0])
Type::getInt(false, $codebase->getMajorAnalysisPhpVersion())
)
]
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,10 @@ public static function replaceTemplateTypes(
]
];
} elseif ($template_type->param_name === 'TPhpMajorVersion') {
$analysis_php_version_id = (string) $codebase->analysis_php_version_id;
$template_result->lower_bounds[$template_type->param_name] = [
'fn-' . strtolower((string) $method_id) => [
new TemplateBound(
Type::getInt(false, (int) $analysis_php_version_id[0])
Type::getInt(false, $codebase->getMajorAnalysisPhpVersion())
)
]
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,10 @@ private static function getMethodReturnType(
]
];
} elseif ($template_type->param_name === 'TPhpMajorVersion') {
$analysis_php_version_id = (string) $codebase->analysis_php_version_id;
$template_result->lower_bounds[$template_type->param_name] = [
'fn-' . strtolower((string)$method_id) => [
new TemplateBound(
Type::getInt(false, (int) $analysis_php_version_id[0])
Type::getInt(false, $codebase->getMajorAnalysisPhpVersion())
)
]
];
Expand Down
5 changes: 3 additions & 2 deletions src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ private static function handleExpression(
return Expression\YieldFromAnalyzer::analyze($statements_analyzer, $stmt, $context);
}

$analysis_php_version_id = $statements_analyzer->getCodebase()->analysis_php_version_id;
$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) {
return Expression\MatchAnalyzer::analyze($statements_analyzer, $stmt, $context);
Expand All @@ -463,7 +464,7 @@ private static function handleExpression(
if (IssueBuffer::accepts(
new UnrecognizedExpression(
'Psalm does not understand ' . get_class($stmt) . ' for PHP ' .
$analysis_php_version_id,
$codebase->getMajorAnalysisPhpVersion() . '.' . $codebase->getMinorAnalysisPhpVersion(),
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
Expand Down
7 changes: 5 additions & 2 deletions src/Psalm/Internal/Codebase/ClassLikes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Psalm\Config;
use Psalm\Exception\UnpopulatedClasslikeException;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\FileManipulation\ClassDocblockManipulator;
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
use Psalm\Internal\Provider\ClassLikeStorageProvider;
Expand Down Expand Up @@ -784,8 +785,10 @@ public function getTraitNode(string $fq_trait_name): PhpParser\Node\Stmt\Trait_
throw new \UnexpectedValueException('Storage should exist for ' . $fq_trait_name);
}

/** Should we have a hardcoded php version here?? */
$file_statements = $this->statements_provider->getStatementsForFile($storage->location->file_path, 70400);
$file_statements = $this->statements_provider->getStatementsForFile(
$storage->location->file_path,
ProjectAnalyzer::getInstance()->getCodebase()->analysis_php_version_id
);

$trait_finder = new \Psalm\Internal\PhpVisitor\TraitFinder($fq_trait_name);

Expand Down
5 changes: 2 additions & 3 deletions src/Psalm/Internal/Codebase/InternalCallMapHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,8 @@ public static function getCallablesFromCallMap(string $function_id): ?array
public static function getCallMap(): array
{
$codebase = ProjectAnalyzer::getInstance()->getCodebase();
$php_version_id = (string) $codebase->analysis_php_version_id;
$analyzer_major_version = (int) $php_version_id[0];
$analyzer_minor_version = (int) $php_version_id[2];
$analyzer_major_version = $codebase->getMajorAnalysisPhpVersion();
$analyzer_minor_version = $codebase->getMinorAnalysisPhpVersion();

$analyzer_version = $analyzer_major_version . '.' . $analyzer_minor_version;
$current_version = self::PHP_MAJOR_VERSION . '.' . self::PHP_MINOR_VERSION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,14 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal

if ($docblock_info) {
if ($docblock_info->since_php_major_version && !$this->aliases->namespace) {
$analysis_php_version_id = (string) $this->codebase->analysis_php_version_id;
if ($docblock_info->since_php_major_version > (int) $analysis_php_version_id[0]) {
$analysis_major_php_version = $this->codebase->getMajorAnalysisPhpVersion();
$analysis_minor_php_version = $this->codebase->getMajorAnalysisPhpVersion();
if ($docblock_info->since_php_major_version > $analysis_major_php_version) {
return false;
}

if ($docblock_info->since_php_major_version === (int) $analysis_php_version_id[0]
&& $docblock_info->since_php_minor_version > (int) $analysis_php_version_id[2]
if ($docblock_info->since_php_major_version === $analysis_major_php_version
&& $docblock_info->since_php_minor_version > $analysis_minor_php_version
) {
return false;
}
Expand Down Expand Up @@ -1029,13 +1030,15 @@ private function createStorageForFunctionLike(
} catch (IncorrectDocblockException|DocblockParseException $e) {
}
if ($docblock_info) {
$analysis_php_version_id = (string) $this->codebase->analysis_php_version_id;
if ($docblock_info->since_php_major_version && !$this->aliases->namespace) {
if ($docblock_info->since_php_major_version > (int) $analysis_php_version_id[0]) {
$analysis_major_php_version = $this->codebase->getMajorAnalysisPhpVersion();
$analysis_minor_php_version = $this->codebase->getMajorAnalysisPhpVersion();
if ($docblock_info->since_php_major_version > $analysis_major_php_version) {
return false;
}
if ($docblock_info->since_php_major_version === (int) $analysis_php_version_id[0]
&& $docblock_info->since_php_minor_version > (int) $analysis_php_version_id[2]

if ($docblock_info->since_php_major_version === $analysis_major_php_version
&& $docblock_info->since_php_minor_version > $analysis_minor_php_version
) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Psalm/Internal/Provider/StatementsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Psalm\Internal\Provider;

use PhpParser;
use Psalm\Codebase;
use Psalm\Progress\Progress;
use Psalm\Progress\VoidProgress;

Expand Down Expand Up @@ -419,9 +420,8 @@ public static function parseStatements(
];

if (!self::$lexer) {
$analysis_php_version_id_string = (string) $analysis_php_version_id;
$major_version = $analysis_php_version_id_string[0];
$minor_version = $analysis_php_version_id_string[2];
$major_version = Codebase::transformPhpVersionId($analysis_php_version_id, 10000);
$minor_version = Codebase::transformPhpVersionId($analysis_php_version_id % 10000, 100);
self::$lexer = new PhpParser\Lexer\Emulative([
'usedAttributes' => $attributes,
'phpVersion' => $major_version . '.' . $minor_version,
Expand Down

0 comments on commit d251fd8

Please sign in to comment.