Skip to content

Commit

Permalink
Add missing randomizer method, improve stub parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Feb 15, 2024
1 parent b238bc7 commit 65f3a78
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions dictionaries/CallMap_83_delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
return [
'added' => [
'json_validate' => ['bool', 'json'=>'string', 'depth='=>'positive-int', 'flags='=>'int'],
'Random\\Randomizer::getBytesFromString' => ['non-empty-string', 'string' => 'non-empty-string', 'length' => 'int<1, max>'],
],

'changed' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,14 @@ public static function parse(
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];
}
} elseif (isset($parsed_docblock->tags['php-from'])) {
$since = trim(reset($parsed_docblock->tags['php-from']));
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];

Check failure on line 399 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:399:55: PossiblyUndefinedIntArrayOffset: Possibly undefined array offset '1' is risky given expected type 'int<0, max>'. Consider using isset beforehand. (see https://psalm.dev/215)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,9 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal
}

if ($docblock_info) {
if ($docblock_info->since_php_major_version && !$this->aliases->namespace) {
if ($docblock_info->since_php_major_version) {
$analysis_major_php_version = $this->codebase->getMajorAnalysisPhpVersion();
$analysis_minor_php_version = $this->codebase->getMajorAnalysisPhpVersion();
$analysis_minor_php_version = $this->codebase->getMinorAnalysisPhpVersion();
if ($docblock_info->since_php_major_version > $analysis_major_php_version) {
return false;
}
Expand Down
11 changes: 10 additions & 1 deletion stubs/extensions/random.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace Random
/**
* @template TValue
* @param array<TValue> $array
* @return list<TValue>
* @return ($array is non-empty-array<TValue> ? non-empty-list<TValue> : list<TValue>)
*/
public function shuffleArray(array $array): array {}

Expand All @@ -103,6 +103,15 @@ namespace Random
*/
public function pickArrayKeys(array $array, int $num): array {}

/**
* @since 8.3
*
* @param non-empty-string $string
* @param positive-int $length
* @return non-empty-string
*/
public function getBytesFromString(string $string, int $length): string {}

public function __serialize(): array {}

public function __unserialize(array $data): void {}
Expand Down
34 changes: 34 additions & 0 deletions tests/CoreStubsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,44 @@ function takesList(array $list): void {}
$globBrace = glob('abc', GLOB_BRACE);
PHP,
];
yield 'randomizer' => [
'code' => <<<'PHP'
<?php
$r = new Random\Randomizer;
/** @var array<int, int> */
$arr = [];
$res = $r->shuffleArray($arr);
/** @psalm-check-type-exact $res = list<int> */;
/** @var non-empty-array<int, int> */
$arr = [];
$res = $r->shuffleArray($arr);
/** @psalm-check-type-exact $res = non-empty-list<int> */;
$str = '123';
$res = $r->getBytesFromString($str, 3);
/** @psalm-check-type-exact $res = non-empty-string */;
PHP,
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.3',
];
}

public function providerInvalidCodeParse(): iterable
{
yield 'randomizer 8.2' => [
'code' => <<<'PHP'
<?php
$r = new Random\Randomizer;
$str = '123';
$res = $r->getBytesFromString($str, 3);
PHP,
'error_message' => 'InvalidAssignment',
'error_levels' => [],
'php_version' => '8.2',
];
yield 'json_decode invalid depth' => [
'code' => '<?php
json_decode("true", depth: -1);
Expand Down

0 comments on commit 65f3a78

Please sign in to comment.