Skip to content

Commit 64bacf0

Browse files
committed
style: run code check by psalm tool
1 parent fd71985 commit 64bacf0

14 files changed

+476
-125
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ array(3) {
173173
}
174174
```
175175

176-
## SFlags
176+
## SFlags Usage
177177

178178
SFlags - is an simple flags(options&argument) parser and manager.
179179

example/refer.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
$flags = ['--name', 'inhere', '--tags', 'php', '-t', 'go', '--tags', 'java', '-f', 'arg0'];
4+
echo "count: ", count($flags), "\n";
5+
6+
$cur = current($flags);
7+
$key = key($flags);
8+
echo "key: $key, current: $cur\n";
9+
10+
while (true) {
11+
if (($key = key($flags)) === null) {
12+
break;
13+
}
14+
15+
$val = next($flags);
16+
echo "key: $key, next: '$val'\n";
17+
}

psalm.xml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="3"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
>
9+
<projectFiles>
10+
<directory name="src"/>
11+
<ignoreFiles>
12+
<directory name="test"/>
13+
<!-- <directory name="vendor"/>-->
14+
</ignoreFiles>
15+
</projectFiles>
16+
<issueHandlers>
17+
</issueHandlers>
18+
</psalm>

src/AbstractParser.php src/AbstractFlags.php

+53-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Toolkit\Stdlib\Str;
2121
use function array_merge;
2222
use function array_shift;
23+
use function array_values;
2324
use function basename;
2425
use function count;
2526
use function explode;
@@ -34,14 +35,15 @@
3435
use function trim;
3536

3637
/**
37-
* class AbstractParser
38+
* class AbstractFlags
39+
* abstract parser
3840
*/
39-
abstract class AbstractParser implements ParserInterface
41+
abstract class AbstractFlags implements ParserInterface
4042
{
4143
use QuickInitTrait;
4244
use NameAliasTrait;
4345

44-
protected const TRIM_CHARS = "; \t\n\r\0\x0B";
46+
protected const TRIM_CHARS = "; \t\n\r\0\x0B";
4547
protected const OPT_MAX_WIDTH = 16;
4648

4749
public const RULE_SEP = ';';
@@ -50,7 +52,9 @@ abstract class AbstractParser implements ParserInterface
5052

5153
public const STATUS_ERR = 1;
5254

53-
public const STATUS_HELP = 2; // found `-h|--help` flag
55+
public const STATUS_ARG = 2;
56+
57+
public const STATUS_HELP = 3; // found `-h|--help` flag
5458

5559
public const SHORT_STYLE_GUN = 'gnu';
5660

@@ -148,7 +152,8 @@ abstract class AbstractParser implements ParserInterface
148152
protected $shortStyle = 'posix';
149153

150154
/**
151-
* Whether stop parse option on first argument
155+
* Stop parse option on found first argument.
156+
* - useful for support multi commands. eg: `top --opt ... sub --opt ...`
152157
*
153158
* @var bool
154159
*/
@@ -198,6 +203,39 @@ public function __construct(array $config = [])
198203
Obj::init($this, $config);
199204
}
200205

206+
/**
207+
* @param array|null $flags
208+
*
209+
* @return bool
210+
*/
211+
public function parse(?array $flags = null): bool
212+
{
213+
if ($this->parsed) {
214+
return true;
215+
}
216+
217+
$this->parsed = true;
218+
$this->rawArgs = [];
219+
220+
if ($flags === null) {
221+
$flags = $_SERVER['argv'];
222+
$sFile = array_shift($flags);
223+
$this->setScriptFile($sFile);
224+
} else {
225+
$flags = array_values($flags);
226+
}
227+
228+
$this->flags = $flags;
229+
return $this->doParse($flags);
230+
}
231+
232+
/**
233+
* @param array $flags
234+
*
235+
* @return bool
236+
*/
237+
abstract protected function doParse(array $flags): bool;
238+
201239
/**
202240
* @param array $rawArgs
203241
*
@@ -227,6 +265,13 @@ protected function parseRawArgs(array $rawArgs): array
227265
return $args;
228266
}
229267

268+
public function resetResults(): void
269+
{
270+
// clear match results
271+
$this->parsed = false;
272+
$this->rawArgs = $this->flags = [];
273+
}
274+
230275
/****************************************************************
231276
* build and render help
232277
***************************************************************/
@@ -346,10 +391,10 @@ protected function doBuildHelp(array $argDefines, array $optDefines, bool $withC
346391
}
347392

348393
/**
349-
* @see DEFINE_ITEM for array $define
350394
* @param array|Option|Argument $define
351395
*
352396
* @return array
397+
* @see DEFINE_ITEM for array $define
353398
*/
354399
protected function formatDesc($define): array
355400
{
@@ -403,7 +448,7 @@ protected function buildArgsForHelp(array $argDefines): array
403448
}
404449

405450
// ensure desc is not empty
406-
$arg['desc'] = $desc ? Str::ucfirst($desc): "Argument $helpName";
451+
$arg['desc'] = $desc ? Str::ucfirst($desc) : "Argument $helpName";
407452

408453
$type = $arg['type'];
409454
if (FlagType::isArray($type)) {
@@ -455,7 +500,7 @@ protected function buildOptsForHelp(array $optDefines): array
455500
}
456501

457502
// ensure desc is not empty
458-
$opt['desc'] = $desc ? Str::ucfirst($desc): "Option $name";
503+
$opt['desc'] = $desc ? Str::ucfirst($desc) : "Option $name";
459504

460505
$helpName = FlagUtil::buildOptHelpName($names);
461506
if ($this->showTypeOnHelp) {

src/Contract/ParserInterface.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,18 @@ public function getFlags(): array;
2525
public function getRawArgs(): array;
2626

2727
/**
28-
* @param array $flags
28+
* @param array|null $flags
2929
*
3030
* @return bool
3131
*/
32-
public function parse(array $flags): bool;
32+
public function parse(?array $flags = null): bool;
33+
34+
/**
35+
* @param string $name
36+
*
37+
* @return bool
38+
*/
39+
public function hasOpt(string $name): bool;
3340

3441
/**
3542
* Get an option value by name
@@ -41,6 +48,13 @@ public function parse(array $flags): bool;
4148
*/
4249
public function getOpt(string $name, $default = null);
4350

51+
/**
52+
* @param string|int $nameOrIndex
53+
*
54+
* @return bool
55+
*/
56+
public function hasArg($nameOrIndex): bool;
57+
4458
/**
4559
* Get an argument value by name
4660
*

src/Flag/AbstractFlag.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ abstract class AbstractFlag implements ArrayAccess, FlagInterface
8181
* The flag value validator
8282
* - if validate fail, please return for OR throw FlagException
8383
*
84-
* @var callable|ValidatorInterface
84+
* @var callable|ValidatorInterface|null
85+
* @psalm-var callable(mixed, string): bool
8586
*/
8687
protected $validator;
8788

0 commit comments

Comments
 (0)