1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add int type aliases based on existing codes

This commit is contained in:
William Owen O. Ponce 2022-10-04 09:47:40 +08:00
parent 313ebf428b
commit 58fd83a01f
4 changed files with 147 additions and 0 deletions

View File

@ -62,6 +62,9 @@ use Psalm\Type\Atomic\TNumericString;
use Psalm\Type\Atomic\TObject;
use Psalm\Type\Atomic\TObjectWithProperties;
use Psalm\Type\Atomic\TPositiveInt;
use Psalm\Type\Atomic\TNonPositiveInt;
use Psalm\Type\Atomic\TNegativeInt;
use Psalm\Type\Atomic\TNonNegativeInt;
use Psalm\Type\Atomic\TResource;
use Psalm\Type\Atomic\TScalar;
use Psalm\Type\Atomic\TString;
@ -229,6 +232,15 @@ abstract class Atomic implements TypeNode
case 'positive-int':
return new TPositiveInt();
case 'non-positive-int':
return new TNonPositiveInt();
case 'negative-int':
return new TNegativeInt();
case 'non-negative-int':
return new TNonNegativeInt();
case 'numeric':
return $php_version !== null ? new TNamedObject($value) : new TNumeric();
@ -720,6 +732,18 @@ abstract class Atomic implements TypeNode
return true;
}
if ($this instanceof TNonPositiveInt) {
return true;
}
if ($this instanceof TNegativeInt) {
return true;
}
if ($this instanceof TNonNegativeInt) {
return true;
}
if ($this instanceof TLiteralClassString) {
return true;
}

View File

@ -0,0 +1,41 @@
<?php
namespace Psalm\Type\Atomic;
/**
* Denotes an int that is also negative (strictly < 0)
* @deprecated will be removed in Psalm 5
*/
class TNegativeInt extends TInt
{
public function getId(bool $nested = false): string
{
return 'negative-int';
}
public function __toString(): string
{
return 'negative-int';
}
/**
* @return false
*/
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}
/**
* @param array<lowercase-string, string> $aliased_classes
*
*/
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
): string {
return $use_phpdoc_format ? 'int' : 'negative-int';
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Psalm\Type\Atomic;
/**
* Denotes an int that is also non-negative (strictly > 0)
* @deprecated will be removed in Psalm 5
*/
class TNonNegativeInt extends TInt
{
public function getId(bool $nested = false): string
{
return 'non-negative-int';
}
public function __toString(): string
{
return 'non-negative-int';
}
/**
* @return false
*/
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}
/**
* @param array<lowercase-string, string> $aliased_classes
*
*/
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
): string {
return $use_phpdoc_format ? 'int' : 'non-negative-int';
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Psalm\Type\Atomic;
/**
* Denotes an int that is also non non-positive (strictly < 0)
* @deprecated will be removed in Psalm 5
*/
class TNonPositiveInt extends TInt
{
public function getId(bool $nested = false): string
{
return 'non-positive-int';
}
public function __toString(): string
{
return 'non-positive-int';
}
/**
* @return false
*/
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}
/**
* @param array<lowercase-string, string> $aliased_classes
*
*/
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
): string {
return $use_phpdoc_format ? 'int' : 'non-positive-int';
}
}