Skip to content

Commit c78973e

Browse files
committed
refactor: restricts enum values to strings
1 parent 742152a commit c78973e

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/Common/AbstractEnum.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@
3535
abstract class AbstractEnum
3636
{
3737
/**
38-
* @var string|int The value of the enum instance
38+
* @var string The value of the enum instance
3939
*/
40-
private $value;
40+
private string $value;
4141

4242
/**
4343
* @var string The name of the enum constant
4444
*/
4545
private string $name;
4646

4747
/**
48-
* @var array<string, array<string, string|int>> Cache for reflection data
48+
* @var array<string, array<string, string>> Cache for reflection data
4949
*/
5050
private static array $cache = [];
5151

@@ -58,10 +58,10 @@ abstract class AbstractEnum
5858
* Constructor is private to ensure instances are created through static methods
5959
*
6060
* @since n.e.x.t
61-
* @param string|int $value The enum value
61+
* @param string $value The enum value
6262
* @param string $name The constant name
6363
*/
64-
final private function __construct($value, string $name)
64+
final private function __construct(string $value, string $name)
6565
{
6666
$this->value = $value;
6767
$this->name = $name;
@@ -105,16 +105,16 @@ final public function __set(string $property, $value): void
105105
* Create an enum instance from a value, throws exception if invalid
106106
*
107107
* @since n.e.x.t
108-
* @param string|int $value The enum value
108+
* @param string $value The enum value
109109
* @return static
110110
* @throws InvalidArgumentException If the value is not valid
111111
*/
112-
final public static function from($value): self
112+
final public static function from(string $value): self
113113
{
114114
$instance = self::tryFrom($value);
115115
if ($instance === null) {
116116
throw new InvalidArgumentException(
117-
sprintf('%s is not a valid backing value for enum %s', (string) $value, static::class)
117+
sprintf('%s is not a valid backing value for enum %s', $value, static::class)
118118
);
119119
}
120120
return $instance;
@@ -124,10 +124,10 @@ final public static function from($value): self
124124
* Try to create an enum instance from a value, returns null if invalid
125125
*
126126
* @since n.e.x.t
127-
* @param string|int $value The enum value
127+
* @param string $value The enum value
128128
* @return static|null
129129
*/
130-
final public static function tryFrom($value): ?self
130+
final public static function tryFrom(string $value): ?self
131131
{
132132
$constants = self::getConstants();
133133
foreach ($constants as $name => $constantValue) {
@@ -158,7 +158,7 @@ final public static function cases(): array
158158
* Check if this enum has the same value as the given value
159159
*
160160
* @since n.e.x.t
161-
* @param string|int|self $other The value or enum to compare
161+
* @param string|self $other The value or enum to compare
162162
* @return bool
163163
*/
164164
final public function equals($other): bool
@@ -186,7 +186,7 @@ final public function is(self $other): bool
186186
* Get all valid values for this enum
187187
*
188188
* @since n.e.x.t
189-
* @return array<string, string|int>
189+
* @return array<string, string>
190190
*/
191191
final public static function getValues(): array
192192
{
@@ -197,10 +197,10 @@ final public static function getValues(): array
197197
* Check if a value is valid for this enum
198198
*
199199
* @since n.e.x.t
200-
* @param string|int $value The value to check
200+
* @param string $value The value to check
201201
* @return bool
202202
*/
203-
final public static function isValidValue($value): bool
203+
final public static function isValidValue(string $value): bool
204204
{
205205
return in_array($value, self::getValues(), true);
206206
}
@@ -209,11 +209,11 @@ final public static function isValidValue($value): bool
209209
* Get or create a singleton instance for the given value and name
210210
*
211211
* @since n.e.x.t
212-
* @param string|int $value The enum value
212+
* @param string $value The enum value
213213
* @param string $name The constant name
214214
* @return static
215215
*/
216-
private static function getInstance($value, string $name): self
216+
private static function getInstance(string $value, string $name): self
217217
{
218218
$className = static::class;
219219

@@ -234,7 +234,7 @@ private static function getInstance($value, string $name): self
234234
* Get all constants for this enum class
235235
*
236236
* @since n.e.x.t
237-
* @return array<string, string|int>
237+
* @return array<string, string>
238238
* @throws \RuntimeException If invalid constant found
239239
*/
240240
final protected static function getConstants(): array
@@ -260,11 +260,11 @@ final protected static function getConstants(): array
260260
}
261261

262262
// Check if value is valid type
263-
if (!is_string($value) && !is_int($value)) {
263+
if (!is_string($value)) {
264264
throw new \RuntimeException(
265265
sprintf(
266266
'Invalid enum value type for constant %s::%s. ' .
267-
'Only string and int values are allowed, %s given.',
267+
'Only string values are allowed, %s given.',
268268
$className,
269269
$name,
270270
gettype($value)
@@ -354,6 +354,6 @@ private static function camelCaseToConstant(string $camelCase): string
354354
*/
355355
final public function __toString(): string
356356
{
357-
return (string) $this->value;
357+
return $this->value;
358358
}
359359
}

0 commit comments

Comments
 (0)