Skip to content

Commit

Permalink
Merge branch '5.x' into upstream-master
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Aug 20, 2023
2 parents fb95bc0 + 0922fa1 commit 82a1806
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
2 changes: 0 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,6 @@
<file src="src/Psalm/Type/Reconciler.php">
<PossiblyUndefinedIntArrayOffset>
<code>$const_name</code>
<code>$type[0]</code>
<code>$type[0][0]</code>
</PossiblyUndefinedIntArrayOffset>
</file>
<file src="src/Psalm/Type/TypeNode.php">
Expand Down
4 changes: 4 additions & 0 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,10 @@ public function visitStubFiles(Codebase $codebase, ?Progress $progress = null):
$stubsDir . 'SPL.phpstub',
];

if ($codebase->analysis_php_version_id >= 7_04_00) {
$this->internal_stubs[] = $stubsDir . 'Php74.phpstub';
}

if ($codebase->analysis_php_version_id >= 8_00_00) {
$this->internal_stubs[] = $stubsDir . 'CoreGenericAttributes.phpstub';
$this->internal_stubs[] = $stubsDir . 'Php80.phpstub';
Expand Down
16 changes: 14 additions & 2 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ class ProjectAnalyzer
UnnecessaryVarAnnotation::class,
];

private const PHP_VERSION_REGEX = '^(0|[1-9]\d*)\.(0|[1-9]\d*)(?:\..*)?$';

private const PHP_SUPPORTED_VERSIONS_REGEX = '^(5\.[456]|7\.[01234]|8\.[0123])(\..*)?$';

/**
* @param array<ReportOptions> $generated_report_options
*/
Expand Down Expand Up @@ -1179,8 +1183,16 @@ public function refactorCodeAfterCompletion(array $to_refactor): void
*/
public function setPhpVersion(string $version, string $source): void
{
if (!preg_match('/^(5\.[456]|7\.[01234]|8\.[012])(\..*)?$/', $version)) {
throw new UnexpectedValueException('Expecting a version number in the format x.y');
if (!preg_match('/' . self::PHP_VERSION_REGEX . '/', $version)) {
throw new UnexpectedValueException('Expecting a version number in the format x.y or x.y.z');
}

if (!preg_match('/' . self::PHP_SUPPORTED_VERSIONS_REGEX . '/', $version)) {
throw new UnexpectedValueException(
'Psalm supports PHP version ">=5.4". The specified version '
. $version
. " is either not supported or doesn't exist.",
);
}

[$php_major_version, $php_minor_version] = explode('.', $version);
Expand Down
11 changes: 10 additions & 1 deletion src/Psalm/Internal/CliUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Report;
use RuntimeException;
use UnexpectedValueException;

use function array_filter;
use function array_key_exists;
Expand Down Expand Up @@ -485,7 +486,15 @@ public static function initPhpVersion(array $options, Config $config, ProjectAna
}

if ($version !== null && $source !== null) {
$project_analyzer->setPhpVersion($version, $source);
try {
$project_analyzer->setPhpVersion($version, $source);
} catch (UnexpectedValueException $e) {
fwrite(
STDERR,
$e->getMessage() . PHP_EOL,
);
exit(2);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Psalm/Type/Reconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ private static function addNestedAssertions(array $new_types, array $existing_ty
{
foreach ($new_types as $nk => $type) {
if (strpos($nk, '[') || strpos($nk, '->')) {
$type = array_values($type);
if (!isset($type[0][0])) {
continue;
}
if ($type[0][0] instanceof IsEqualIsset
|| $type[0][0] instanceof IsIsset
|| $type[0][0] instanceof NonEmpty
Expand Down
11 changes: 11 additions & 0 deletions stubs/Php74.phpstub
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

/**
* @psalm-pure
*
* @psalm-taint-escape html
* @psalm-flow ($string) -> return
*
* @param null|string|array<array-key,string> $allowed_tags
*/
function strip_tags(string $string, null|string|array $allowed_tags = null) : string {}

0 comments on commit 82a1806

Please sign in to comment.