Skip to content

Commit

Permalink
coding style: TRUE/FALSE/NULL -> true/false/null
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 11, 2017
1 parent d04fe0a commit d6b0a5b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
10 changes: 5 additions & 5 deletions src/CommandLine/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ class Console
public function __construct()
{
$this->useColors = PHP_SAPI === 'cli' && ((function_exists('posix_isatty') && posix_isatty(STDOUT))
|| getenv('ConEmuANSI') === 'ON' || getenv('ANSICON') !== FALSE || getenv('term') === 'xterm-256color');
|| getenv('ConEmuANSI') === 'ON' || getenv('ANSICON') !== false || getenv('term') === 'xterm-256color');
}


public function useColors($state = TRUE)
public function useColors($state = true)
{
$this->useColors = (bool) $state;
}


public function color($color = NULL, $s = NULL)
public function color($color = null, $s = null)
{
static $colors = [
'black' => '0;30', 'gray' => '1;30', 'silver' => '0;37', 'white' => '1;37',
'navy' => '0;34', 'blue' => '1;34', 'green' => '0;32', 'lime' => '1;32',
'teal' => '0;36', 'aqua' => '1;36', 'maroon' => '0;31', 'red' => '1;31',
'purple' => '0;35', 'fuchsia' => '1;35', 'olive' => '0;33', 'yellow' => '1;33',
NULL => '0',
null => '0',
];
if ($this->useColors) {
$c = explode('/', $color);
return "\033["
. ($c[0] ? $colors[$c[0]] : '')
. (empty($c[1]) ? '' : ';4' . substr($colors[$c[1]], -1))
. 'm' . $s
. ($s === NULL ? '' : "\033[0m");
. ($s === null ? '' : "\033[0m");
}
return $s;
}
Expand Down
20 changes: 10 additions & 10 deletions src/CommandLine/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public function __construct($help, array $defaults = [])
self::ARGUMENT => (bool) end($m[2]),
self::OPTIONAL => isset($line[2]) || (substr(end($m[2]), 0, 1) === '[') || isset($opts[self::VALUE]),
self::REPEATABLE => (bool) end($m[3]),
self::ENUM => count($enums = explode('|', trim(end($m[2]), '<[]>'))) > 1 ? $enums : NULL,
self::VALUE => isset($line[2]) ? $line[2] : NULL,
self::ENUM => count($enums = explode('|', trim(end($m[2]), '<[]>'))) > 1 ? $enums : null,
self::VALUE => isset($line[2]) ? $line[2] : null,
];
if ($name !== $m[1][0]) {
$this->aliases[$m[1][0]] = $name;
Expand All @@ -68,9 +68,9 @@ public function __construct($help, array $defaults = [])
}


public function parse(array $args = NULL)
public function parse(array $args = null)
{
if ($args === NULL) {
if ($args === null) {
$args = isset($_SERVER['argv']) ? array_slice($_SERVER['argv'], 1) : [];
}
$params = [];
Expand All @@ -93,7 +93,7 @@ public function parse(array $args = NULL)
continue;
}

list($name, $arg) = strpos($arg, '=') ? explode('=', $arg, 2) : [$arg, TRUE];
list($name, $arg) = strpos($arg, '=') ? explode('=', $arg, 2) : [$arg, true];

if (isset($this->aliases[$name])) {
$name = $this->aliases[$name];
Expand All @@ -104,18 +104,18 @@ public function parse(array $args = NULL)

$opt = $this->options[$name];

if ($arg !== TRUE && empty($opt[self::ARGUMENT])) {
if ($arg !== true && empty($opt[self::ARGUMENT])) {
throw new \Exception("Option $name has not argument.");

} elseif ($arg === TRUE && !empty($opt[self::ARGUMENT])) {
} elseif ($arg === true && !empty($opt[self::ARGUMENT])) {
if (isset($args[$i]) && $args[$i][0] !== '-') {
$arg = $args[$i++];
} elseif (empty($opt[self::OPTIONAL])) {
throw new \Exception("Option $name requires argument.");
}
}

if (!empty($opt[self::ENUM]) && !in_array($arg, $opt[self::ENUM], TRUE) && !($opt[self::OPTIONAL] && $arg === TRUE)) {
if (!empty($opt[self::ENUM]) && !in_array($arg, $opt[self::ENUM], true) && !($opt[self::OPTIONAL] && $arg === true)) {
throw new \Exception("Value of option $name must be " . implode(', or ', $opt[self::ENUM]) . '.');
}
$this->checkArg($opt, $arg);
Expand All @@ -135,7 +135,7 @@ public function parse(array $args = NULL)
} elseif ($name[0] !== '-' && empty($opt[self::OPTIONAL])) {
throw new \Exception("Missing required argument <$name>.");
} else {
$params[$name] = NULL;
$params[$name] = null;
}
if (!empty($opt[self::REPEATABLE])) {
$params[$name] = (array) $params[$name];
Expand All @@ -155,7 +155,7 @@ public function checkArg(array $opt, &$arg)
{
if (!empty($opt[self::REALPATH])) {
$path = realpath($arg);
if ($path === FALSE) {
if ($path === false) {
throw new \Exception("File path '$arg' not found.");
}
$arg = $path;
Expand Down
4 changes: 2 additions & 2 deletions tests/Console.color.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ require __DIR__ . '/bootstrap.php';
$console = new Console;
$console->useColors();

Assert::same("\x1b[m", $console->color(NULL));
Assert::same("\x1b[m", $console->color(null));
Assert::same("\x1b[1;31m", $console->color('red'));
Assert::same("\x1b[1;31;42m", $console->color('red/green'));
Assert::same("\x1b[1;31;42m", $console->color('red/lime'));

Assert::same("\x1b[mhello\x1b[0m", $console->color(NULL, 'hello'));
Assert::same("\x1b[mhello\x1b[0m", $console->color(null, 'hello'));
Assert::same("\x1b[1;31mhello\x1b[0m", $console->color('red', 'hello'));
Assert::same("\x1b[1;31;42mhello\x1b[0m", $console->color('red/green', 'hello'));
Assert::same("\x1b[1;31;42mhello\x1b[0m", $console->color('red/lime', 'hello'));
56 changes: 28 additions & 28 deletions tests/Parser.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ test(function () {
--a-b
');

Assert::same(['-p' => NULL, '--p' => NULL, '--a-b' => NULL], $cmd->parse([]));
Assert::same(['-p' => TRUE, '--p' => NULL, '--a-b' => NULL], $cmd->parse(['-p']));
Assert::same(['-p' => null, '--p' => null, '--a-b' => null], $cmd->parse([]));
Assert::same(['-p' => true, '--p' => null, '--a-b' => null], $cmd->parse(['-p']));

$cmd = new Parser('
-p description
');

Assert::same(['-p' => NULL], $cmd->parse([]));
Assert::same(['-p' => TRUE], $cmd->parse(['-p']));
Assert::same(['-p' => null], $cmd->parse([]));
Assert::same(['-p' => true], $cmd->parse(['-p']));
});


Expand All @@ -32,7 +32,7 @@ test(function () { // default value
');

Assert::same(['-p' => '123'], $cmd->parse([]));
Assert::same(['-p' => TRUE], $cmd->parse(['-p']));
Assert::same(['-p' => true], $cmd->parse(['-p']));


$cmd = new Parser('
Expand All @@ -42,7 +42,7 @@ test(function () { // default value
]);

Assert::same(['-p' => 123], $cmd->parse([]));
Assert::same(['-p' => TRUE], $cmd->parse(['-p']));
Assert::same(['-p' => true], $cmd->parse(['-p']));
});


Expand All @@ -51,10 +51,10 @@ test(function () { // alias
-p | --param
');

Assert::same(['--param' => NULL], $cmd->parse([]));
Assert::same(['--param' => TRUE], $cmd->parse(['-p']));
Assert::same(['--param' => TRUE], $cmd->parse(['--param']));
Assert::same(['--param' => TRUE], $cmd->parse(explode(' ', '-p --param')));
Assert::same(['--param' => null], $cmd->parse([]));
Assert::same(['--param' => true], $cmd->parse(['-p']));
Assert::same(['--param' => true], $cmd->parse(['--param']));
Assert::same(['--param' => true], $cmd->parse(explode(' ', '-p --param')));
Assert::exception(function () use ($cmd) {
$cmd->parse(['-p=val']);
}, 'Exception', 'Option --param has not argument.');
Expand All @@ -63,13 +63,13 @@ test(function () { // alias
-p --param
');

Assert::same(['--param' => TRUE], $cmd->parse(['-p']));
Assert::same(['--param' => true], $cmd->parse(['-p']));

$cmd = new Parser('
-p, --param
');

Assert::same(['--param' => TRUE], $cmd->parse(['-p']));
Assert::same(['--param' => true], $cmd->parse(['-p']));
});


Expand All @@ -78,7 +78,7 @@ test(function () { // argument
-p param
');

Assert::same(['-p' => NULL], $cmd->parse([]));
Assert::same(['-p' => null], $cmd->parse([]));
Assert::same(['-p' => 'val'], $cmd->parse(explode(' ', '-p val')));
Assert::same(['-p' => 'val'], $cmd->parse(explode(' ', '-p=val')));
Assert::same(['-p' => 'val2'], $cmd->parse(explode(' ', '-p val1 -p val2')));
Expand Down Expand Up @@ -106,8 +106,8 @@ test(function () { // optional argument
-p [param]
');

Assert::same(['-p' => NULL], $cmd->parse([]));
Assert::same(['-p' => TRUE], $cmd->parse(['-p']));
Assert::same(['-p' => null], $cmd->parse([]));
Assert::same(['-p' => true], $cmd->parse(['-p']));
Assert::same(['-p' => 'val'], $cmd->parse(explode(' ', '-p val')));


Expand All @@ -118,18 +118,18 @@ test(function () { // optional argument
]);

Assert::same(['-p' => 123], $cmd->parse([]));
Assert::same(['-p' => TRUE], $cmd->parse(['-p']));
Assert::same(['-p' => true], $cmd->parse(['-p']));
Assert::same(['-p' => 'val'], $cmd->parse(explode(' ', '-p val')));


$cmd = new Parser('
-p param
', [
'-p' => [Parser::OPTIONAL => TRUE],
'-p' => [Parser::OPTIONAL => true],
]);

Assert::same(['-p' => NULL], $cmd->parse([]));
Assert::same(['-p' => TRUE], $cmd->parse(['-p']));
Assert::same(['-p' => null], $cmd->parse([]));
Assert::same(['-p' => true], $cmd->parse(['-p']));
Assert::same(['-p' => 'val'], $cmd->parse(explode(' ', '-p val')));
});

Expand All @@ -141,7 +141,7 @@ test(function () { // repeatable argument
');

Assert::same(['-p' => []], $cmd->parse([]));
Assert::same(['-p' => [TRUE]], $cmd->parse(['-p']));
Assert::same(['-p' => [true]], $cmd->parse(['-p']));
Assert::same(['-p' => ['val']], $cmd->parse(explode(' ', '-p val')));
Assert::same(['-p' => ['val1', 'val2']], $cmd->parse(explode(' ', '-p val1 -p val2')));
});
Expand All @@ -153,7 +153,7 @@ test(function () { // enumerates
-p <a|b|c>
');

Assert::same(['-p' => NULL], $cmd->parse([]));
Assert::same(['-p' => null], $cmd->parse([]));
Assert::exception(function () use ($cmd) {
$cmd->parse(['-p']);
}, 'Exception', 'Option -p requires argument.');
Expand All @@ -167,8 +167,8 @@ test(function () { // enumerates
-p [a|b|c]
');

Assert::same(['-p' => NULL], $cmd->parse([]));
Assert::same(['-p' => TRUE], $cmd->parse(['-p']));
Assert::same(['-p' => null], $cmd->parse([]));
Assert::same(['-p' => true], $cmd->parse(['-p']));
Assert::same(['-p' => 'a'], $cmd->parse(explode(' ', '-p a')));
Assert::exception(function () use ($cmd) {
$cmd->parse(explode(' ', '-p foo'));
Expand All @@ -181,7 +181,7 @@ test(function () { // realpath
$cmd = new Parser('
-p <path>
', [
'-p' => [Parser::REALPATH => TRUE],
'-p' => [Parser::REALPATH => true],
]);

Assert::exception(function () use ($cmd) {
Expand All @@ -208,21 +208,21 @@ test(function () { // positional arguments
}, 'Exception', 'Unexpected parameter val2.');

$cmd = new Parser('', [
'pos' => [Parser::REPEATABLE => TRUE],
'pos' => [Parser::REPEATABLE => true],
]);

Assert::same(['pos' => ['val1', 'val2']], $cmd->parse(['val1', 'val2']));


$cmd = new Parser('', [
'pos' => [Parser::OPTIONAL => TRUE],
'pos' => [Parser::OPTIONAL => true],
]);

Assert::same(['pos' => NULL], $cmd->parse([]));
Assert::same(['pos' => null], $cmd->parse([]));


$cmd = new Parser('', [
'pos' => [Parser::VALUE => 'default', Parser::REPEATABLE => TRUE],
'pos' => [Parser::VALUE => 'default', Parser::REPEATABLE => true],
]);

Assert::same(['pos' => ['default']], $cmd->parse([]));
Expand Down

0 comments on commit d6b0a5b

Please sign in to comment.