Skip to content

Commit

Permalink
Merge pull request #9107 from eopnetto/update-psalm-reserved-words
Browse files Browse the repository at this point in the history
Add missing `int-range` aliases into the list of psalm reserved words
  • Loading branch information
orklah committed Jan 14, 2023
2 parents 11942d7 + 423b1ac commit 3e66c6b
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Psalm/Internal/Type/TypeTokenizer.php
Expand Up @@ -60,6 +60,9 @@ class TypeTokenizer
'lowercase-string' => true,
'non-empty-lowercase-string' => true,
'positive-int' => true,
'non-negative-int' => true,
'negative-int' => true,
'non-positive-int' => true,
'literal-int' => true,
'boolean' => true,
'integer' => true,
Expand Down
145 changes: 144 additions & 1 deletion tests/IntRangeTest.php
Expand Up @@ -45,6 +45,36 @@ function scope(int $a){
return $a;
}',
],
'nonNegativeIntRange' => [
'code' => '<?php
/**
* @param int<0,12> $a
* @return non-negative-int
*/
function scope(int $a){
return $a;
}',
],
'negativeIntRange' => [
'code' => '<?php
/**
* @param int<-12,-1> $a
* @return negative-int
*/
function scope(int $a){
return $a;
}',
],
'nonPositiveIntRange' => [
'code' => '<?php
/**
* @param int<-12,0> $a
* @return non-positive-int
*/
function scope(int $a){
return $a;
}',
],
'intRangeToInt' => [
'code' => '<?php
/**
Expand Down Expand Up @@ -697,6 +727,42 @@ function doAnalysis(): void
'$length===' => 'int<8, max>',
],
],
'nonNegativeIntToRangeWithInferior' => [
'code' => '<?php
/** @var non-negative-int $length */
$length = 0;
if ($length < 8) {
throw new \RuntimeException();
}',
'assertions' => [
'$length===' => 'int<8, max>',
],
],
'negativeIntToRangeWithSuperior' => [
'code' => '<?php
/** @var negative-int $length */
$length = -8;
if ($length > -8) {
throw new \RuntimeException();
}',
'assertions' => [
'$length===' => 'int<min, -8>',
],
],
'nonPositiveIntToRangeWithSuperior' => [
'code' => '<?php
/** @var non-positive-int $length */
$length = -8;
if ($length > -8) {
throw new \RuntimeException();
}',
'assertions' => [
'$length===' => 'int<min, -8>',
],
],
'positiveIntToRangeWithSuperiorOrEqual' => [
'code' => '<?php
/** @var positive-int $length */
Expand All @@ -709,6 +775,42 @@ function doAnalysis(): void
'$length===' => 'int<1, 7>',
],
],
'nonNegativeIntToRangeWithSuperiorOrEqual' => [
'code' => '<?php
/** @var non-negative-int $length */
$length = 0;
if ($length >= 8) {
throw new \RuntimeException();
}',
'assertions' => [
'$length===' => 'int<0, 7>',
],
],
'negativeIntToRangeWithInferiorOrEqual' => [
'code' => '<?php
/** @var negative-int $length */
$length = -1;
if ($length <= -8) {
throw new \RuntimeException();
}',
'assertions' => [
'$length===' => 'int<-7, -1>',
],
],
'nonPositiveIntToRangeWithInferiorOrEqual' => [
'code' => '<?php
/** @var non-positive-int $length */
$length = -1;
if ($length <= -8) {
throw new \RuntimeException();
}',
'assertions' => [
'$length===' => 'int<-7, 0>',
],
],
'literalEquality' => [
'code' => '<?php
Expand Down Expand Up @@ -737,6 +839,47 @@ function doAnalysis(): void
'$_arr===' => 'non-empty-array<int<0, max>, int<0, max>>',
],
],
'nonNegativeIntCombinedWithIntRange' => [
'code' => '<?php
/** @var non-negative-int */
$int = 1;
/** @var array<int<0, max>, int<0, max>> */
$_arr = [];
$_arr[0] = 0;
$_arr[1] = $int;
$_arr[$int] = 2;',
'assertions' => [
'$_arr===' => 'non-empty-array<int<0, max>, int<0, max>>',
],
],
'negativeIntCombinedWithIntRange' => [
'code' => '<?php
/** @var negative-int */
$int = -1;
/** @var array<int<min, -1>, int<min, -1>> */
$_arr = [];
$_arr[-1] = $int;
$_arr[$int] = -2;',
'assertions' => [
'$_arr===' => 'non-empty-array<int<min, -1>, int<min, -1>>',
],
],
'nonPositiveIntCombinedWithIntRange' => [
'code' => '<?php
/** @var non-positive-int */
$int = -1;
/** @var array<int<min, 0>, int<min, 0>> */
$_arr = [];
$_arr[0] = 0;
$_arr[-1] = $int;
$_arr[$int] = -2;',
'assertions' => [
'$_arr===' => 'non-empty-array<int<min, 0>, int<min, 0>>',
],
],
'noErrorPushingBigShapeIntoConstant' => [
'code' => '<?php
class DocComment
Expand Down Expand Up @@ -837,7 +980,7 @@ class DocComment
'code' => '<?php
/**
* @param array-key $key
* @param positive-int $expected
* @param positive-int|non-negative-int|negative-int|non-positive-int $expected
*/
function matches($key, int $expected): bool {
if ($key !== $expected) {
Expand Down

0 comments on commit 3e66c6b

Please sign in to comment.