Skip to content

Commit 371d863

Browse files
committed
fix: isValidName - num should not an valid name
1 parent 8e716ec commit 371d863

File tree

6 files changed

+17
-10
lines changed

6 files changed

+17
-10
lines changed

Diff for: composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"test": "vendor/bin/phpunit"
4141
},
4242
"suggest": {
43+
"toolkit/pflag": "Command line flag parse library of the php",
4344
"inhere/console": "a lightweight php console application library."
4445
}
4546
}

Diff for: src/App.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public function addObject(callable $handler, array $config = []): void
305305
*/
306306
public function addByConfig(callable $handler, array $config): void
307307
{
308-
if (empty($config['name']) || !$handler) {
308+
if (empty($config['name'])) {
309309
throw new InvalidArgumentException('Invalid arguments for add command');
310310
}
311311

@@ -329,7 +329,7 @@ public function add(string $command, callable $handler, $config = null): void
329329
*/
330330
public function addCommand(string $command, callable $handler, $config = null): void
331331
{
332-
if (!$command || !$handler) {
332+
if (!$command) {
333333
throw new InvalidArgumentException('Invalid arguments for add command');
334334
}
335335

Diff for: src/Cli.php

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
* @method static error(string ...$message) Print error style message line.
5252
* @method static warn(string ...$message) Print warn style message line.
5353
* @method static info(string ...$message) Print info style message line.
54+
* @method static note(string ...$message) Print note style message line.
55+
* @method static notice(string ...$message) Print notice style message line.
5456
* @method static success(string ...$message) Print success style message line.
5557
*/
5658
class Cli

Diff for: src/Helper/FlagHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static function isOptionValue($val): bool
112112
*/
113113
public static function isValidName(string $name): bool
114114
{
115-
return preg_match('#^\w[\w-]{0,36}$#', $name) === 1;
115+
return preg_match('#^[a-zA-Z_][\w-]{0,36}$#', $name) === 1;
116116
}
117117

118118
/**

Diff for: test/FlagsTest.php

-6
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,4 @@ public function testStopParseOnTwoHl(): void
8181
$this->assertSame('--age', $args[0]);
8282
$this->assertCount(4, $args);
8383
}
84-
85-
public function testIsValidArgName(): void
86-
{
87-
$this->assertTrue(FlagHelper::isValidName('arg0'));
88-
$this->assertFalse(FlagHelper::isValidName('/path/to'));
89-
}
9084
}

Diff for: test/Helper/FlagHelperTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
*/
1111
class FlagHelperTest extends TestCase
1212
{
13+
14+
public function testIsValidArgName1(): void
15+
{
16+
$this->assertTrue(FlagHelper::isValidName('arg0'));
17+
$this->assertFalse(FlagHelper::isValidName('9'));
18+
$this->assertFalse(FlagHelper::isValidName('/path/to'));
19+
}
20+
1321
public function testIsValidName(): void
1422
{
1523
$tests = [
@@ -26,10 +34,12 @@ public function testIsValidName(): void
2634
'-name' => false,
2735
' name' => false,
2836
'+name' => false,
37+
3 => false,
38+
30 => false,
2939
];
3040

3141
foreach ($tests as $name => $ok) {
32-
$this->assertSame($ok, FlagHelper::isValidName($name));
42+
$this->assertSame($ok, FlagHelper::isValidName((string)$name));
3343
}
3444
}
3545
}

0 commit comments

Comments
 (0)