Skip to content

Commit 9bf54ac

Browse files
committed
fix: unit tests error
1 parent 1b407de commit 9bf54ac

File tree

2 files changed

+23
-60
lines changed

2 files changed

+23
-60
lines changed

src/Flag/AbstractFlag.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Toolkit\PFlag\Flag;
1111

1212
use ArrayAccess;
13-
use Toolkit\Cli\Helper\FlagHelper;
1413
use Toolkit\PFlag\Contract\FlagInterface;
1514
use Toolkit\PFlag\Contract\ValidatorInterface;
1615
use Toolkit\PFlag\Exception\FlagException;
@@ -70,16 +69,16 @@ abstract class AbstractFlag implements ArrayAccess, FlagInterface
7069
/**
7170
* The default value
7271
*
73-
* @var mixed
72+
* @var mixed|null
7473
*/
75-
protected mixed $default;
74+
protected mixed $default = null;
7675

7776
/**
7877
* The flag value
7978
*
80-
* @var mixed
79+
* @var mixed|null
8180
*/
82-
protected mixed $value;
81+
protected mixed $value = null;
8382

8483
// TODO category
8584
// protected $category = '';
@@ -105,15 +104,15 @@ abstract class AbstractFlag implements ArrayAccess, FlagInterface
105104
* @param bool $required
106105
* @param mixed|null $default
107106
*
108-
* @return static|Argument|Option
107+
* @return static
109108
*/
110109
public static function new(
111110
string $name,
112111
string $desc = '',
113112
string $type = 'string',
114113
bool $required = false,
115114
mixed $default = null
116-
): self {
115+
): static {
117116
return new static($name, $desc, $type, $required, $default);
118117
}
119118

@@ -123,9 +122,9 @@ public static function new(
123122
* @param string $name
124123
* @param array $define
125124
*
126-
* @return static|Argument|Option
125+
* @return static
127126
*/
128-
public static function newByArray(string $name, array $define): self
127+
public static function newByArray(string $name, array $define): static
129128
{
130129
$flag = new static($name);
131130
if (isset($define['name'])) {

src/FlagType.php

+15-51
Original file line numberDiff line numberDiff line change
@@ -124,31 +124,14 @@ public static function getHelpName(string $type, bool $toUpper = true): string
124124
*/
125125
public static function getDefault(string $type): float|bool|int|array|string|null
126126
{
127-
$value = null;
128-
switch ($type) {
129-
case self::INT:
130-
$value = 0;
131-
break;
132-
case self::BOOL:
133-
$value = false;
134-
break;
135-
case self::FLOAT:
136-
$value = (float)0;
137-
break;
138-
case self::STRING:
139-
$value = '';
140-
break;
141-
case self::INTS:
142-
case self::ARRAY:
143-
case self::STRINGS:
144-
$value = [];
145-
break;
146-
default:
147-
// nothing
148-
break;
149-
}
150-
151-
return $value;
127+
return match ($type) {
128+
self::INT => 0,
129+
self::BOOL => false,
130+
self::FLOAT => (float)0,
131+
self::STRING => '',
132+
self::INTS, self::ARRAY, self::STRINGS => [],
133+
default => null,
134+
};
152135
}
153136

154137
/**
@@ -164,32 +147,13 @@ public static function fmtBasicTypeValue(string $type, mixed $value): mixed
164147
}
165148

166149
// format value by type
167-
switch ($type) {
168-
case self::INT:
169-
case self::INTS:
170-
$value = (int)$value;
171-
break;
172-
case self::BOOL:
173-
if (is_string($value)) {
174-
// $value = FlagHelper::str2bool($value);
175-
$value = Str::toBool2($value);
176-
} else {
177-
$value = (bool)$value;
178-
}
179-
break;
180-
case self::FLOAT:
181-
$value = (float)$value;
182-
break;
183-
case self::STRING:
184-
case self::STRINGS:
185-
$value = (string)$value;
186-
break;
187-
default:
188-
// nothing
189-
break;
190-
}
191-
192-
return $value;
150+
return match ($type) {
151+
self::INT, self::INTS => (int)$value,
152+
self::BOOL => is_string($value) ? Str::toBool2($value) : (bool)$value,
153+
self::FLOAT => (float)$value,
154+
self::STRING, self::STRINGS => (string)$value,
155+
default => $value,
156+
};
193157
}
194158

195159
/**

0 commit comments

Comments
 (0)