Skip to content

Commit

Permalink
@SInCE annotations should only infer PHP version in .phpstub files or…
Browse files Browse the repository at this point in the history
… for @SInCE 8.0.0 PHP

Fix #10761
  • Loading branch information
kkmuffme committed Mar 2, 2024
1 parent 616b903 commit 06298ff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Expand Up @@ -23,6 +23,7 @@
use function explode;
use function implode;
use function in_array;
use function pathinfo;
use function preg_last_error_msg;
use function preg_match;
use function preg_replace;
Expand All @@ -37,6 +38,8 @@
use function substr_count;
use function trim;

use const PATHINFO_EXTENSION;

/**
* @internal
*/
Expand Down Expand Up @@ -417,11 +420,14 @@ public static function parse(

if (isset($parsed_docblock->tags['since'])) {
$since = trim(reset($parsed_docblock->tags['since']));
if (preg_match('/^[4578]\.\d(\.\d+)?$/', $since)) {
$since_parts = explode('.', $since);

$info->since_php_major_version = (int)$since_parts[0];
$info->since_php_minor_version = (int)$since_parts[1];
// only for phpstub files or @since 8.0.0 PHP
// since @since is commonly used with the project version, not the PHP version
// https://docs.phpdoc.org/3.0/guide/references/phpdoc/tags/since.html
// https://github.com/vimeo/psalm/issues/10761
if (preg_match('/^([4578])\.(\d)(\.\d+)?(\s+PHP)?$/i', $since, $since_match)
&& (!empty($since_match[4]) || pathinfo($code_location->file_name, PATHINFO_EXTENSION) === 'phpstub')) {
$info->since_php_major_version = (int)$since_match[1];

Check failure on line 429 in src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockParser.php

View workflow job for this annotation

GitHub Actions / build

PossiblyUndefinedIntArrayOffset

src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockParser.php:429:55: PossiblyUndefinedIntArrayOffset: Possibly undefined array offset '1' is risky given expected type 'array-key'. Consider using isset beforehand. (see https://psalm.dev/215)
$info->since_php_minor_version = (int)$since_match[2];

Check failure on line 430 in src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockParser.php

View workflow job for this annotation

GitHub Actions / build

PossiblyUndefinedIntArrayOffset

src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockParser.php:430:55: PossiblyUndefinedIntArrayOffset: Possibly undefined array offset '2' is risky given expected type 'array-key'. Consider using isset beforehand. (see https://psalm.dev/215)
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/AnnotationTest.php
Expand Up @@ -1384,6 +1384,16 @@ class Bar {}
class Foo {}',
'assertions' => [],
],
'sinceTagNonPhpVersion' => [
'code' => '<?php
class Foo {
/**
* @since 8.9.9
*/
public function bar() : void {
}
};',
],
];
}

Expand Down

0 comments on commit 06298ff

Please sign in to comment.