diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b75604a..f8af7b4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: strategy: fail-fast: false matrix: - php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] + php: ['8.2', '8.3', '8.4', '8.5'] steps: - name: Checkout @@ -51,8 +51,8 @@ jobs: - name: Install dependencies run: composer update --prefer-dist --no-progress --no-interaction - - name: Coding standards (phpcs) - run: composer cs-ci + - name: Coding standards (PHP-CS-Fixer) + run: vendor/bin/php-cs-fixer fix --dry-run --diff - name: Static analysis (PHPStan) run: composer stan diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..396d643 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,45 @@ +in([__DIR__ . '/src', __DIR__ . '/tests']) + ->name('*.php'); + +foreach ($excludeDirs as $excludeDir) { + $finder->exclude($excludeDir); +} + +foreach ($excludePaths as $excludePath) { + $finder->notPath($excludePath); +} + +return (new PhpCsFixer\Config()) + ->setRiskyAllowed(false) + ->setRules([ + '@PER-CS' => true, + '@PHP8x2Migration' => true, + 'array_syntax' => ['syntax' => 'short'], + 'no_unused_imports' => true, + 'ordered_imports' => [ + 'imports_order' => ['class', 'function', 'const'], + 'sort_algorithm' => 'alpha', + ], + 'single_quote' => true, + 'trailing_comma_in_multiline' => [ + 'elements' => ['arguments', 'array_destructuring', 'arrays', 'match', 'parameters'], + ], + ]) + ->setFinder($finder) + ->setCacheFile(__DIR__ . '/build/php-cs-fixer.cache'); diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..672da62 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,16 @@ +# Changelog + +All notable changes to `initphp/console` are documented here. The format is based +on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and this project +adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +### Changed (breaking) + +- Requires PHP `^8.2` (was `>=7.2`); PHP 7.2 through 8.1 are no longer + supported. CI now covers PHP 8.2, 8.3, 8.4 and 8.5. +- Code style moved to PER-CS (php-cs-fixer, shared `.php-cs-fixer.dist.php`), + replacing PHP_CodeSniffer (PSR-12); the codebase has been reformatted + accordingly. The `cs`, `cs-ci` and `cs-fix` Composer scripts now run + php-cs-fixer, and `cs-check` was added. diff --git a/README.md b/README.md index e9b1fed..a21ac55 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ A small, dependency-free helper library for writing console / CLI applications i ## Requirements -- PHP **7.2** or higher +- PHP **8.2** or higher - `ext-mbstring` *(optional)* — improves table alignment for multibyte (UTF-8) values ## Installation @@ -218,7 +218,7 @@ The alias is a transition aid and may be removed in a future major release. See ```bash composer test # PHPUnit -composer cs # PHP_CodeSniffer (PSR-12) +composer cs # PHP-CS-Fixer (PER-CS) composer stan # PHPStan (level 6) composer qa # all of the above ``` diff --git a/composer.json b/composer.json index fdd7f12..6345726 100644 --- a/composer.json +++ b/composer.json @@ -28,11 +28,11 @@ } ], "require": { - "php": ">=7.2" + "php": "^8.2" }, "require-dev": { + "friendsofphp/php-cs-fixer": "^3.88", "phpunit/phpunit": "^8.5 || ^9.6 || ^10.5", - "squizlabs/php_codesniffer": "^3.7", "phpstan/phpstan": "^1.11" }, "suggest": { @@ -57,9 +57,10 @@ "scripts": { "test": "phpunit", "test:coverage": "phpunit --coverage-text --coverage-html=build/coverage", - "cs": "phpcs", - "cs-ci": "phpcs --warning-severity=0", - "cs-fix": "phpcbf", + "cs": "@cs-check", + "cs-ci": "@cs-check", + "cs-check": "php-cs-fixer fix --dry-run --diff", + "cs-fix": "php-cs-fixer fix", "stan": "phpstan analyse", "qa": [ "@cs-ci", diff --git a/phpcs.xml b/phpcs.xml deleted file mode 100644 index 002434d..0000000 --- a/phpcs.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - PSR-12 baseline for InitPHP/Console. - - src - tests - - - - - - - - - - diff --git a/src/Application.php b/src/Application.php index 3832b17..8e3fb8f 100644 --- a/src/Application.php +++ b/src/Application.php @@ -150,13 +150,13 @@ public function run(?array $argv = null): bool $argv = \is_array($GLOBALS['argv'] ?? null) ? $GLOBALS['argv'] : []; } - $this->terminal = (string)($argv[0] ?? ''); + $this->terminal = (string) ($argv[0] ?? ''); \array_shift($argv); if (empty($argv)) { return false; } - $this->command = (string)\current($argv); + $this->command = (string) \current($argv); \array_shift($argv); $this->input = new Input($argv); diff --git a/src/InputArgument.php b/src/InputArgument.php index 7be3fb3..32dcf45 100644 --- a/src/InputArgument.php +++ b/src/InputArgument.php @@ -137,7 +137,7 @@ public function getDefinition(): string */ public function getDefault(): ?string { - return isset($this->default) ? (string)$this->default : null; + return isset($this->default) ? (string) $this->default : null; } /** @@ -193,7 +193,7 @@ private function valueCheck(&$value): bool return true; } if (\in_array($value, [0, 1], true)) { - $value = (bool)$value; + $value = (bool) $value; return true; } return false; @@ -209,7 +209,7 @@ private function valueCheck(&$value): bool } if ($this->type === self::STR) { if (\is_numeric($value) || \is_bool($value)) { - $value = (string)$value; + $value = (string) $value; } return \is_string($value); } diff --git a/src/Output.php b/src/Output.php index dd76353..9e8ac6b 100644 --- a/src/Output.php +++ b/src/Output.php @@ -95,7 +95,7 @@ public function progressBar($done, $total): void throw new \InvalidArgumentException('$total must be greater than zero.'); } - $progress = (int)\floor(($done / $total) * 100); + $progress = (int) \floor(($done / $total) * 100); if ($progress < 0) { $progress = 0; } elseif ($progress > 100) { @@ -104,12 +104,12 @@ public function progressBar($done, $total): void $remaining = 100 - $progress; \fwrite($this->stdout, \sprintf( - "\033[0G\033[2K[%'=" . $progress . "s>%-" . $remaining . "s] - %d%% %s/%s", + "\033[0G\033[2K[%'=" . $progress . 's>%-' . $remaining . 's] - %d%% %s/%s', '', '', $progress, $done, - $total + $total, )); } @@ -123,7 +123,7 @@ public function list(array $rows, int $leftSpace = 0): void if (!\is_string($key)) { continue; } - $rowSpace = (int)\ceil(\strlen($key) / 8); + $rowSpace = (int) \ceil(\strlen($key) / 8); if ($space < $rowSpace) { $space = $rowSpace; } @@ -154,7 +154,7 @@ public function ask(string $question, bool $empty = true) $this->write($question, [], true); do { - $input = Helpers::strValueCast(\trim((string)\fgets($this->stdin))); + $input = Helpers::strValueCast(\trim((string) \fgets($this->stdin))); if (\in_array($input, ['exit', 'quit'], true)) { $this->terminate(); return null; @@ -172,7 +172,7 @@ public function question(Question $question) $this->write($question->getQuestion(), [], true); do { - $answer = \trim((string)\fgets($this->stdin)); + $answer = \trim((string) \fgets($this->stdin)); if ($question->hasOption($answer)) { return Helpers::strValueCast($answer); } @@ -195,13 +195,13 @@ public function write(string $str, array $context = [], bool $newLine = false, a $replace = []; foreach ($context as $key => $val) { if (!\is_array($val) && (!\is_object($val) || \method_exists($val, '__toString'))) { - $replace['{' . $key . '}'] = (string)$val; + $replace['{' . $key . '}'] = (string) $val; } } $str = \strtr($str, $replace); } - $msg = "\e[" . \implode(';', $format) . "m" . $str . "\e[0m" + $msg = "\e[" . \implode(';', $format) . 'm' . $str . "\e[0m" . ($newLine ? \PHP_EOL : ''); \fwrite($this->stdout, $msg); } diff --git a/src/Utils/Table.php b/src/Utils/Table.php index 61be712..c93be8a 100644 --- a/src/Utils/Table.php +++ b/src/Utils/Table.php @@ -207,7 +207,7 @@ public function row(array $assoc): self { $row = []; foreach ($assoc as $key => $value) { - $row[\trim((string)$key)] = \trim($this->stringify($value)); + $row[\trim((string) $key)] = \trim($this->stringify($value)); } $this->rows[] = $row; @@ -247,7 +247,7 @@ public function getContent(): string unset($length); $res = $this->getTableTopContent($columnLengths) - . $this->getFormattedRowContent($headerData, $columnLengths, "\e[" . \implode(';', $this->headerStyle) . "m", true) + . $this->getFormattedRowContent($headerData, $columnLengths, "\e[" . \implode(';', $this->headerStyle) . 'm', true) . $this->getTableSeparatorContent($columnLengths); foreach ($this->rows as $row) { foreach ($headerData as $column) { @@ -255,7 +255,7 @@ public function getContent(): string $row[$column] = self::NULL_PLACEHOLDER; } } - $res .= $this->getFormattedRowContent($row, $columnLengths, "\e[" . \implode(';', $this->cellStyle) . "m"); + $res .= $this->getFormattedRowContent($row, $columnLengths, "\e[" . \implode(';', $this->cellStyle) . 'm'); } return $res . $this->getTableBottomContent($columnLengths); @@ -290,7 +290,7 @@ private function stringify($value): string return $value === false ? '[FALSE]' : '[TRUE]'; } - return (string)$value; + return (string) $value; } /** @@ -308,13 +308,13 @@ private function getFormattedRowContent(array $data, array $lengths, string $for $value = ' ' . $value; $len = $this->strlen($value) - $lengths[$key] + 1; if ($isHeader === false && !empty($this->columnCellStyle[$key])) { - $customFormat = "\e[" . \implode(';', $this->columnCellStyle[$key]) . "m"; + $customFormat = "\e[" . \implode(';', $this->columnCellStyle[$key]) . 'm'; } $cells[] = $format . $customFormat . $value . ($format !== '' || $customFormat !== '' ? "\e[0m" : '') - . \str_repeat(' ', (int)\abs($len)); + . \str_repeat(' ', (int) \abs($len)); } $res .= \implode($this->getChar('middle'), $cells); @@ -368,7 +368,7 @@ private function getChar(string $char, int $len = 1): string if (!isset($this->chars[$char])) { return ''; } - $res = empty($this->borderStyle) ? '' : "\e[" . \implode(';', $this->borderStyle) . "m"; + $res = empty($this->borderStyle) ? '' : "\e[" . \implode(';', $this->borderStyle) . 'm'; $res .= $len === 1 ? $this->chars[$char] : \str_repeat($this->chars[$char], $len); $res .= empty($this->borderStyle) ? '' : "\e[0m"; diff --git a/src/aliases.php b/src/aliases.php index 69d1c28..b606433 100644 --- a/src/aliases.php +++ b/src/aliases.php @@ -19,6 +19,6 @@ if (!class_exists(\InitPHP\CLITable\Table::class, false)) { class_alias( \InitPHP\Console\Utils\Table::class, - 'InitPHP\\CLITable\\Table' + 'InitPHP\\CLITable\\Table', ); } diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index eed5f03..ed9eb3b 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -25,8 +25,7 @@ public function testRegisterReturnsSelf(): void { $app = $this->app(new MemoryOutput()); - self::assertSame($app, $app->register('noop', static function (): void { - })); + self::assertSame($app, $app->register('noop', static function (): void {})); } public function testRegisterRejectsUnknownClass(): void @@ -141,8 +140,7 @@ public function testListRendersRegisteredCommands(): void $out = new MemoryOutput(); $app = $this->app($out); $app->register(GreetCommand::class); - $app->register('solo', static function (): void { - }, 'A solo command.'); + $app->register('solo', static function (): void {}, 'A solo command.'); self::assertTrue($app->run(['console.php', 'list'])); $plain = $out->plain(); diff --git a/tests/Fixtures/NoNameCommand.php b/tests/Fixtures/NoNameCommand.php index 60c233e..0c8692a 100644 --- a/tests/Fixtures/NoNameCommand.php +++ b/tests/Fixtures/NoNameCommand.php @@ -13,7 +13,5 @@ */ final class NoNameCommand extends Command { - public function execute(Input $input, Output $output) - { - } + public function execute(Input $input, Output $output) {} } diff --git a/tests/Support/MemoryOutput.php b/tests/Support/MemoryOutput.php index 7994843..6558c2f 100644 --- a/tests/Support/MemoryOutput.php +++ b/tests/Support/MemoryOutput.php @@ -37,7 +37,7 @@ public function captured(): string { \rewind($this->buffer); - return (string)\stream_get_contents($this->buffer); + return (string) \stream_get_contents($this->buffer); } /** @@ -45,7 +45,7 @@ public function captured(): string */ public function plain(): string { - return (string)\preg_replace('/\e\[[0-9;]*m/', '', $this->captured()); + return (string) \preg_replace('/\e\[[0-9;]*m/', '', $this->captured()); } /** diff --git a/tests/Support/TerminateException.php b/tests/Support/TerminateException.php index d360333..a5121d2 100644 --- a/tests/Support/TerminateException.php +++ b/tests/Support/TerminateException.php @@ -7,6 +7,4 @@ /** * Thrown by {@see MemoryOutput::terminate()} in place of `exit`. */ -final class TerminateException extends \RuntimeException -{ -} +final class TerminateException extends \RuntimeException {} diff --git a/tests/Utils/TableTest.php b/tests/Utils/TableTest.php index 74ea028..d49a3b9 100644 --- a/tests/Utils/TableTest.php +++ b/tests/Utils/TableTest.php @@ -14,7 +14,7 @@ public function testCreateReturnsInstanceAndStringCastEqualsGetContent(): void $table = Table::create()->row(['id' => 1, 'name' => 'Ada']); self::assertInstanceOf(Table::class, $table); - self::assertSame($table->getContent(), (string)$table); + self::assertSame($table->getContent(), (string) $table); } public function testFluentSettersReturnSelf(): void