Skip to content
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/*
* Shared php-cs-fixer configuration for every InitPHP package (ADR-005).
*
* This file is identical in all repositories as `.php-cs-fixer.dist.php`.
* The only package-specific part is the two lines below; scripts/check-cs-config.sh
* ignores exactly those two lines when it compares a package with this template.
* Keep each of them on a single line.
*/
$excludeDirs = [];
$excludePaths = [];

$finder = PhpCsFixer\Finder::create()
->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');
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
```
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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",
Expand Down
16 changes: 0 additions & 16 deletions phpcs.xml

This file was deleted.

4 changes: 2 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/InputArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
16 changes: 8 additions & 8 deletions src/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
));
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
14 changes: 7 additions & 7 deletions src/Utils/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -247,15 +247,15 @@ 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) {
if (!isset($row[$column])) {
$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);
Expand Down Expand Up @@ -290,7 +290,7 @@ private function stringify($value): string
return $value === false ? '[FALSE]' : '[TRUE]';
}

return (string)$value;
return (string) $value;
}

/**
Expand All @@ -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);

Expand Down Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion src/aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
}
6 changes: 2 additions & 4 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 1 addition & 3 deletions tests/Fixtures/NoNameCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@
*/
final class NoNameCommand extends Command
{
public function execute(Input $input, Output $output)
{
}
public function execute(Input $input, Output $output) {}
}
4 changes: 2 additions & 2 deletions tests/Support/MemoryOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public function captured(): string
{
\rewind($this->buffer);

return (string)\stream_get_contents($this->buffer);
return (string) \stream_get_contents($this->buffer);
}

/**
* Returns the captured output with SGR escape sequences stripped.
*/
public function plain(): string
{
return (string)\preg_replace('/\e\[[0-9;]*m/', '', $this->captured());
return (string) \preg_replace('/\e\[[0-9;]*m/', '', $this->captured());
}

/**
Expand Down
4 changes: 1 addition & 3 deletions tests/Support/TerminateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* Thrown by {@see MemoryOutput::terminate()} in place of `exit`.
*/
final class TerminateException extends \RuntimeException
{
}
final class TerminateException extends \RuntimeException {}
2 changes: 1 addition & 1 deletion tests/Utils/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading