Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.php]
charset = utf-8
indent_style = tab
indent_size = 4
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: sockets, json, curl

- name: Install dependencies with composer
run: composer install

- name: Run tests
run: ./vendor/bin/phpunit

- name: Run static analysis
run: ./vendor/bin/phpstan
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ vendor/
.idea/
clover.xml
phpunit.xml
*.cache
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
],
"require-dev": {
"donatj/drop": "*",
"phpunit/phpunit": "^9 || ^10"
"phpunit/phpunit": "^9 || ^10",
"phpstan/phpstan": "^2.2"
},
"license": "MIT",
"authors": [
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 9
paths:
- src
71 changes: 40 additions & 31 deletions src/Flags.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
use donatj\Exceptions\InvalidFlagParamException;
use donatj\Exceptions\InvalidFlagTypeException;
use donatj\Exceptions\MissingFlagParamException;
use function PHPStan\dumpType;

class Flags {

/** @var array | null */
protected $args;
/** @var list<string> */
protected array $args ;

/** @var bool */
protected $skipFirstArgument;

private $definedFlags = [];
private $definedShortFlags = [];
private $arguments = [];
private $parsed = false;
protected bool $skipFirstArgument;
/**
* @var array<string,array{type: string, usage: string, required: bool, value: mixed}>
*/
private array $definedFlags = [];
/**
* @var array<string,array{value:int,usage:string}>
*/
private array $definedShortFlags = [];
/** @var list<string> */
private array $arguments = [];
private bool $parsed = false;

/** @access private */
const DEF_TYPE = 'type';
Expand All @@ -44,7 +51,7 @@
/**
* Flags constructor.
*
* @param array|null $args The arguments to parse, defaults to $_SERVER['argv']
* @param array<string>|null $args The arguments to parse, defaults to $_SERVER['argv']
* @param bool $skipFirstArgument Setting to false causes the first argument to be parsed as an parameter
* rather than the command.
*/
Expand All @@ -53,17 +60,17 @@
$args = (array)$_SERVER['argv'];
}

$this->args = $args;
$this->skipFirstArgument = $skipFirstArgument;
$this->args = array_values($args ?? []); // @phpstan-ignore assign.propertyType (this is fine for now)
$this->skipFirstArgument = (bool)$skipFirstArgument;
}

/**
* Returns the n'th command-line argument. `arg(0)` is the first remaining argument after flags have been processed.
*
* @param int $index
* @return string
* @return string|null
*/
public function arg( $index ) {
public function arg( $index ) : ?string {
return isset($this->arguments[$index]) ? $this->arguments[$index] : null;
}

Expand All @@ -72,7 +79,7 @@
*
* @return string[] Array of argument strings
*/
public function args() {
public function args() : array {
return $this->arguments;
}

Expand All @@ -81,9 +88,9 @@
*
* `-v` would set the 'v' index to 1, whereas `-vvv` will set the 'v' index to 3
*
* @return array
* @return array<string,int> map of short value to instances
*/
public function shorts() {
public function shorts() : array {
$out = [];
foreach( $this->definedShortFlags as $key => $data ) {
$out[$key] = $data[self::DEF_VALUE];
Expand All @@ -95,9 +102,9 @@
/**
* Returns an array of long-flag values indexed by flag name
*
* @return array
* @return array<string,mixed>
*/
public function longs() {
public function longs() : array {
$out = [];
foreach( $this->definedFlags as $key => $data ) {
$out[$key] = $data[self::DEF_VALUE];
Expand Down Expand Up @@ -301,7 +308,7 @@
*
* Will throw exceptions on Missing Require Flags, Unknown Flags or Incorrect Flag Types
*
* @param array|null $args The arguments to parse. Defaults to arguments defined in the constructor.
* @param array<string>|null $args The arguments to parse. Defaults to arguments defined in the constructor.
* @param bool $ignoreExceptions Setting to true causes parsing to continue even after an exception has been
* thrown.
* @param bool $skipFirstArgument Option to parse the first argument as an parameter rather than the command.
Expand All @@ -310,9 +317,11 @@
* @throws Exceptions\InvalidFlagParamException
* @throws Exceptions\InvalidFlagTypeException
*/
public function parse( ?array $args = null, $ignoreExceptions = false, $skipFirstArgument = null ) {
public function parse( ?array $args = null, $ignoreExceptions = false, $skipFirstArgument = null ) : void {
if( $args === null ) {
$args = $this->args;
}else{
$args = array_values($args);
}

if( $skipFirstArgument === null ) {
Expand Down Expand Up @@ -368,7 +377,7 @@
*
* @return bool
*/
public function parsed() {
public function parsed() : bool {
return $this->parsed;
}

Expand All @@ -379,7 +388,7 @@
*/
private function validateType( $type, &$value ) : bool {
$validate = [
self::TYPE_BOOL => function ( &$val ) {
self::TYPE_BOOL => function( &$val ) {
$val = strtolower((string)$val);
if( $val == '0' || $val == 'f' || $val == 'false' ) {
$val = false;
Expand All @@ -394,7 +403,7 @@

return false;
},
self::TYPE_UINT => function ( &$val ) {
self::TYPE_UINT => function( &$val ) {
if( abs((float)$val) == (int)$val ) {
$val = (int)$val;

Expand All @@ -403,7 +412,7 @@

return false;
},
self::TYPE_INT => function ( &$val ) {
self::TYPE_INT => function( &$val ) {
if( is_numeric($val) && floatval($val) == intval($val) ) {
$val = intval($val);

Expand All @@ -412,7 +421,7 @@

return false;
},
self::TYPE_FLOAT => function ( &$val ) {
self::TYPE_FLOAT => function( &$val ) {
if( is_numeric($val) ) {
$val = floatval($val);

Expand All @@ -421,7 +430,7 @@

return false;
},
self::TYPE_STRING => function ( &$val ) {
self::TYPE_STRING => function( &$val ) {
if( $val !== true ) {
$val = (string)$val;

Expand All @@ -438,9 +447,9 @@
}

/**
* @param array $args
* @param array $definedFlags
* @return array
* @param list<string> $args
* @param array<string,array{type: string, usage: string, required: bool, value: mixed}> $definedFlags
* @return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>}
*/
protected function splitArguments( array $args, array $definedFlags ) : array {
$longParams = [];
Expand All @@ -451,7 +460,7 @@
$getValue = false;
$startArgs = false;
foreach( $args as $arg ) {
if( isset($arg[0]) && $arg[0] == '-' && !$startArgs && !$forceValue && $arg !== '-' ) {
if( isset($arg[0]) && $arg[0] === '-' && !$startArgs && !$forceValue && $arg !== '-' ) {
$cleanArg = ltrim($arg, '- ');

if( $getValue ) {
Expand All @@ -470,7 +479,7 @@
} else {
$getValue = $cleanArg;

if( isset($definedFlags[$cleanArg]) && $definedFlags[$cleanArg][self::DEF_TYPE] != self::TYPE_BOOL ) {
if( isset($definedFlags[$cleanArg]) && $definedFlags[$cleanArg][self::DEF_TYPE] !== self::TYPE_BOOL ) {
$forceValue = true;
}
}
Expand All @@ -492,10 +501,10 @@
if( $getValue ) {
$longParams[$getValue] = true;

return [ $longParams, $shortParams, $arguments ];

Check failure on line 504 in src/Flags.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 8.1)

Method donatj\Flags::splitArguments() should return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>} but returns array{non-empty-array<0|string, string|true>, array<string, int<1, max>>, list<string>}.

Check failure on line 504 in src/Flags.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 8.0)

Method donatj\Flags::splitArguments() should return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>} but returns array{non-empty-array<0|string, string|true>, array<string, int<1, max>>, list<string>}.

Check failure on line 504 in src/Flags.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 7.4)

Method donatj\Flags::splitArguments() should return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>} but returns array{non-empty-array<0|string, string|true>, array<string, int<1, max>>, list<string>}.

Check failure on line 504 in src/Flags.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 8.0)

Method donatj\Flags::splitArguments() should return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>} but returns array{non-empty-array<0|string, string|true>, array<string, int<1, max>>, list<string>}.

Check failure on line 504 in src/Flags.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 7.4)

Method donatj\Flags::splitArguments() should return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>} but returns array{non-empty-array<0|string, string|true>, array<string, int<1, max>>, list<string>}.

Check failure on line 504 in src/Flags.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 8.1)

Method donatj\Flags::splitArguments() should return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>} but returns array{non-empty-array<0|string, string|true>, array<string, int<1, max>>, list<string>}.
}

return [ $longParams, $shortParams, $arguments ];

Check failure on line 507 in src/Flags.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 8.1)

Method donatj\Flags::splitArguments() should return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>} but returns array{array<0|string, string|true>, array<string, int<1, max>>, list<string>}.

Check failure on line 507 in src/Flags.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 8.0)

Method donatj\Flags::splitArguments() should return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>} but returns array{array<0|string, string|true>, array<string, int<1, max>>, list<string>}.

Check failure on line 507 in src/Flags.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 7.4)

Method donatj\Flags::splitArguments() should return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>} but returns array{array<0|string, string|true>, array<string, int<1, max>>, list<string>}.

Check failure on line 507 in src/Flags.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 8.0)

Method donatj\Flags::splitArguments() should return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>} but returns array{array<0|string, string|true>, array<string, int<1, max>>, list<string>}.

Check failure on line 507 in src/Flags.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 7.4)

Method donatj\Flags::splitArguments() should return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>} but returns array{array<0|string, string|true>, array<string, int<1, max>>, list<string>}.

Check failure on line 507 in src/Flags.php

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 8.1)

Method donatj\Flags::splitArguments() should return array{array<0|string, string|true>, array<non-empty-string, int<1, max>>, list<string>} but returns array{array<0|string, string|true>, array<string, int<1, max>>, list<string>}.
}

}
Loading