added missing named parameters, added strict_types

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-01-26 15:16:46 +01:00
parent cc4a47d3fb
commit 9bdcc6d586
1 changed files with 14 additions and 13 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/keyhelp-php81 #!/usr/bin/keyhelp-php81
<?php <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
@ -14,7 +15,7 @@ $version = '0.0.1';
require dirname(path: __DIR__) . '/vendor/autoload.php'; require dirname(path: __DIR__) . '/vendor/autoload.php';
$configFile = dirname(path: __DIR__) ."/config.json"; $configFile = dirname(path: __DIR__) ."/config.json";
if (!file_exists($configFile)) { if (!file_exists(filename: $configFile)) {
echo 'Missing config file' . PHP_EOL; echo 'Missing config file' . PHP_EOL;
if (confirm(message: 'Should I create a new config based on config.json.sample?')) { if (confirm(message: 'Should I create a new config based on config.json.sample?')) {
copy(from: 'config.json.sample', to: 'config.json'); copy(from: 'config.json.sample', to: 'config.json');
@ -25,9 +26,9 @@ if (!file_exists($configFile)) {
} }
exit(1); exit(1);
} }
$configJSON = file_get_contents($configFile); $configJSON = file_get_contents(filename: $configFile);
if (!$config = json_decode($configJSON, associative: true)) { if (!$config = json_decode(json: $configJSON, associative: true)) {
echo 'Error parsing the config file.' . PHP_EOL; echo 'Error parsing the config file.' . PHP_EOL;
echo $configJSON; echo $configJSON;
} }
@ -42,27 +43,27 @@ $longOpts = [
'help::' 'help::'
]; ];
$options = getopt($shortOpts, $longOpts, rest_index: $restIndex); $options = getopt(short_options: $shortOpts, long_options: $longOpts, rest_index: $restIndex);
if (array_key_exists('v', $options) || array_key_exists('version', $options) ) { if (array_key_exists(key: 'v', array: $options) || array_key_exists(key: 'version', array: $options) ) {
print("bindAPI version: $version" . PHP_EOL); print("bindAPI version: $version" . PHP_EOL);
exit(0); exit(0);
} }
if (array_key_exists('h', $options) || array_key_exists('help', $options) ) { if (array_key_exists(key: 'h', array: $options) || array_key_exists(key: 'help', array: $options) ) {
print("Help …" . PHP_EOL); print("Help …" . PHP_EOL);
exit(0); exit(0);
} }
if (array_key_exists('V', $options) || array_key_exists('verbose', $options) ) { if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options) ) {
$config['verbose'] = true; $config['verbose'] = true;
} else { } else {
$config['verbose'] = false; $config['verbose'] = false;
} }
$arguments = array_slice($argv, $restIndex); $arguments = array_slice(array: $argv, offset: $restIndex);
$app = new BindAPI(config: $config, argumentsCount: count($arguments), arguments: $arguments); $app = new BindAPI(config: $config, argumentsCount: count(value: $arguments), arguments: $arguments);
$app->runCommand(); $app->runCommand();
@ -81,7 +82,7 @@ function confirm(String $message = 'Are you sure? ', array $options = ['y', 'n']
foreach ($options as $option) { foreach ($options as $option) {
// mark default // mark default
if ($option == $default) { if ($option == $default) {
$option = strtoupper($option); $option = strtoupper(string: $option);
} }
if ($first) { if ($first) {
echo $option; echo $option;
@ -93,8 +94,8 @@ function confirm(String $message = 'Are you sure? ', array $options = ['y', 'n']
echo '): '; echo '): ';
$handle = fopen(filename: "php://stdin", mode: 'r'); $handle = fopen(filename: "php://stdin", mode: 'r');
$line = trim(fgetc($handle)); $line = trim(fgetc(stream: $handle));
fclose($handle); fclose(stream: $handle);
if ($line == '') { if ($line == '') {
// enter // enter