From 58fd83a01f05d5ff23a8c2917cf5b385f96153e1 Mon Sep 17 00:00:00 2001 From: "William Owen O. Ponce" Date: Tue, 4 Oct 2022 09:47:40 +0800 Subject: [PATCH 1/5] Add int type aliases based on existing codes --- src/Psalm/Type/Atomic.php | 24 +++++++++++++ src/Psalm/Type/Atomic/TNegativeInt .php | 41 +++++++++++++++++++++++ src/Psalm/Type/Atomic/TNonNegativeInt.php | 41 +++++++++++++++++++++++ src/Psalm/Type/Atomic/TNonPositiveInt.php | 41 +++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 src/Psalm/Type/Atomic/TNegativeInt .php create mode 100644 src/Psalm/Type/Atomic/TNonNegativeInt.php create mode 100644 src/Psalm/Type/Atomic/TNonPositiveInt.php diff --git a/src/Psalm/Type/Atomic.php b/src/Psalm/Type/Atomic.php index b0d456b65dd..47b54162c4b 100644 --- a/src/Psalm/Type/Atomic.php +++ b/src/Psalm/Type/Atomic.php @@ -62,6 +62,9 @@ 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 @@ public static function create( 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 @@ public function isTruthy(): bool 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; } diff --git a/src/Psalm/Type/Atomic/TNegativeInt .php b/src/Psalm/Type/Atomic/TNegativeInt .php new file mode 100644 index 00000000000..1325492cdac --- /dev/null +++ b/src/Psalm/Type/Atomic/TNegativeInt .php @@ -0,0 +1,41 @@ + $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'; + } +} diff --git a/src/Psalm/Type/Atomic/TNonNegativeInt.php b/src/Psalm/Type/Atomic/TNonNegativeInt.php new file mode 100644 index 00000000000..10c16d638a6 --- /dev/null +++ b/src/Psalm/Type/Atomic/TNonNegativeInt.php @@ -0,0 +1,41 @@ + 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 $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'; + } +} diff --git a/src/Psalm/Type/Atomic/TNonPositiveInt.php b/src/Psalm/Type/Atomic/TNonPositiveInt.php new file mode 100644 index 00000000000..dbc66be77a4 --- /dev/null +++ b/src/Psalm/Type/Atomic/TNonPositiveInt.php @@ -0,0 +1,41 @@ + $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'; + } +} From 6b6c320fe62e75db20fe41289e7fefd9880effe5 Mon Sep 17 00:00:00 2001 From: "William Owen O. Ponce" Date: Tue, 4 Oct 2022 09:53:22 +0800 Subject: [PATCH 2/5] Arrange use statements alphabetically --- src/Psalm/Type/Atomic.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Type/Atomic.php b/src/Psalm/Type/Atomic.php index 47b54162c4b..2672139322b 100644 --- a/src/Psalm/Type/Atomic.php +++ b/src/Psalm/Type/Atomic.php @@ -45,6 +45,7 @@ use Psalm\Type\Atomic\TLowercaseString; use Psalm\Type\Atomic\TMixed; use Psalm\Type\Atomic\TNamedObject; +use Psalm\Type\Atomic\TNegativeInt; use Psalm\Type\Atomic\TNever; use Psalm\Type\Atomic\TNonEmptyArray; use Psalm\Type\Atomic\TNonEmptyList; @@ -56,15 +57,14 @@ use Psalm\Type\Atomic\TNonFalsyString; use Psalm\Type\Atomic\TNonspecificLiteralInt; use Psalm\Type\Atomic\TNonspecificLiteralString; +use Psalm\Type\Atomic\TNonNegativeInt; +use Psalm\Type\Atomic\TNonPositiveInt; use Psalm\Type\Atomic\TNull; use Psalm\Type\Atomic\TNumeric; 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; From 04c9fe89c1225dd6dff975d20b6fea4ef3a173aa Mon Sep 17 00:00:00 2001 From: "William Owen O. Ponce" Date: Tue, 4 Oct 2022 09:59:34 +0800 Subject: [PATCH 3/5] Arrange use statements alphabetically again --- src/Psalm/Type/Atomic.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Type/Atomic.php b/src/Psalm/Type/Atomic.php index 2672139322b..657657bae41 100644 --- a/src/Psalm/Type/Atomic.php +++ b/src/Psalm/Type/Atomic.php @@ -55,10 +55,10 @@ use Psalm\Type\Atomic\TNonEmptyScalar; use Psalm\Type\Atomic\TNonEmptyString; use Psalm\Type\Atomic\TNonFalsyString; -use Psalm\Type\Atomic\TNonspecificLiteralInt; -use Psalm\Type\Atomic\TNonspecificLiteralString; use Psalm\Type\Atomic\TNonNegativeInt; use Psalm\Type\Atomic\TNonPositiveInt; +use Psalm\Type\Atomic\TNonspecificLiteralInt; +use Psalm\Type\Atomic\TNonspecificLiteralString; use Psalm\Type\Atomic\TNull; use Psalm\Type\Atomic\TNumeric; use Psalm\Type\Atomic\TNumericString; From ae426a00cec386680801c6973e27c8caa0d08003 Mon Sep 17 00:00:00 2001 From: "William Owen O. Ponce" Date: Wed, 5 Oct 2022 09:09:56 +0800 Subject: [PATCH 4/5] Remove irrelevant types, use keep aliases --- src/Psalm/Type/Atomic.php | 21 ++---------- src/Psalm/Type/Atomic/TNegativeInt .php | 41 ----------------------- src/Psalm/Type/Atomic/TNonNegativeInt.php | 41 ----------------------- src/Psalm/Type/Atomic/TNonPositiveInt.php | 41 ----------------------- 4 files changed, 3 insertions(+), 141 deletions(-) delete mode 100644 src/Psalm/Type/Atomic/TNegativeInt .php delete mode 100644 src/Psalm/Type/Atomic/TNonNegativeInt.php delete mode 100644 src/Psalm/Type/Atomic/TNonPositiveInt.php diff --git a/src/Psalm/Type/Atomic.php b/src/Psalm/Type/Atomic.php index 657657bae41..7e5a258ea61 100644 --- a/src/Psalm/Type/Atomic.php +++ b/src/Psalm/Type/Atomic.php @@ -45,7 +45,6 @@ use Psalm\Type\Atomic\TLowercaseString; use Psalm\Type\Atomic\TMixed; use Psalm\Type\Atomic\TNamedObject; -use Psalm\Type\Atomic\TNegativeInt; use Psalm\Type\Atomic\TNever; use Psalm\Type\Atomic\TNonEmptyArray; use Psalm\Type\Atomic\TNonEmptyList; @@ -55,8 +54,6 @@ use Psalm\Type\Atomic\TNonEmptyScalar; use Psalm\Type\Atomic\TNonEmptyString; use Psalm\Type\Atomic\TNonFalsyString; -use Psalm\Type\Atomic\TNonNegativeInt; -use Psalm\Type\Atomic\TNonPositiveInt; use Psalm\Type\Atomic\TNonspecificLiteralInt; use Psalm\Type\Atomic\TNonspecificLiteralString; use Psalm\Type\Atomic\TNull; @@ -234,13 +231,13 @@ public static function create( return new TPositiveInt(); case 'non-positive-int': - return new TNonPositiveInt(); + return new TIntRange(null, -1); case 'negative-int': - return new TNegativeInt(); + return new TIntRange(null, -1); case 'non-negative-int': - return new TNonNegativeInt(); + return new TPositiveInt(); case 'numeric': return $php_version !== null ? new TNamedObject($value) : new TNumeric(); @@ -732,18 +729,6 @@ public function isTruthy(): bool 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; } diff --git a/src/Psalm/Type/Atomic/TNegativeInt .php b/src/Psalm/Type/Atomic/TNegativeInt .php deleted file mode 100644 index 1325492cdac..00000000000 --- a/src/Psalm/Type/Atomic/TNegativeInt .php +++ /dev/null @@ -1,41 +0,0 @@ - $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'; - } -} diff --git a/src/Psalm/Type/Atomic/TNonNegativeInt.php b/src/Psalm/Type/Atomic/TNonNegativeInt.php deleted file mode 100644 index 10c16d638a6..00000000000 --- a/src/Psalm/Type/Atomic/TNonNegativeInt.php +++ /dev/null @@ -1,41 +0,0 @@ - 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 $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'; - } -} diff --git a/src/Psalm/Type/Atomic/TNonPositiveInt.php b/src/Psalm/Type/Atomic/TNonPositiveInt.php deleted file mode 100644 index dbc66be77a4..00000000000 --- a/src/Psalm/Type/Atomic/TNonPositiveInt.php +++ /dev/null @@ -1,41 +0,0 @@ - $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'; - } -} From 0c3a62bc4882dab8e057c58c9f720db891fd085b Mon Sep 17 00:00:00 2001 From: William Owen Ponce <31012084+hamburnyog@users.noreply.github.com> Date: Wed, 5 Oct 2022 14:29:24 +0800 Subject: [PATCH 5/5] Update args --- src/Psalm/Type/Atomic.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Type/Atomic.php b/src/Psalm/Type/Atomic.php index 7e5a258ea61..ff0868a23b5 100644 --- a/src/Psalm/Type/Atomic.php +++ b/src/Psalm/Type/Atomic.php @@ -231,13 +231,13 @@ public static function create( return new TPositiveInt(); case 'non-positive-int': - return new TIntRange(null, -1); + return new TIntRange(null, 0); case 'negative-int': return new TIntRange(null, -1); case 'non-negative-int': - return new TPositiveInt(); + return new TIntRange(0, null); case 'numeric': return $php_version !== null ? new TNamedObject($value) : new TNumeric();