diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 30e1ae3..9efd925 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -8,9 +8,9 @@ jobs: strategy: fail-fast: true matrix: - php: [ "7.3", "7.4", "8.0" ] + php: [ "8.0", "8.1" ] - name: PHP ${{ matrix.php }}, Support ${{ matrix.support }} + name: PHP ${{ matrix.php }} steps: - name: Checkout code diff --git a/composer.json b/composer.json index 8963349..967d9b3 100644 --- a/composer.json +++ b/composer.json @@ -22,17 +22,14 @@ "source": "https://github.com/TheDragonCode/pretty-array" }, "require": { - "php": "^7.3 || ^8.0", + "php": "^8.0", "ext-dom": "*", "ext-mbstring": "*", "dragon-code/contracts": "^2.6", - "dragon-code/support": "^5.0" + "dragon-code/support": "^6.0" }, "require-dev": { - "phpunit/phpunit": "^9.0" - }, - "conflict": { - "andrey-helldar/pretty-array": "*" + "phpunit/phpunit": "^9.5" }, "suggest": { "symfony/thanks": "Give thanks (in the form of a GitHub) to your fellow PHP package maintainers" diff --git a/src/Concerns/HasCases.php b/src/Concerns/HasCases.php index 4746904..aeff16e 100644 --- a/src/Concerns/HasCases.php +++ b/src/Concerns/HasCases.php @@ -55,63 +55,49 @@ protected function convertKeysCase(array $array): array return $result; } - protected function convertKeyCase($key) + protected function convertKeyCase(mixed $key): mixed { if (! is_string($key)) { return $key; } - switch ($this->case) { - case static::KEBAB_CASE: - return $this->caseTo( - $this->caseTo($key, static::PASCAL_CASE), - static::KEBAB_CASE - ); - - case static::CAMEL_CASE: - return $this->caseTo( - $this->caseTo($key, static::KEBAB_CASE), - static::CAMEL_CASE - ); - - case static::SNAKE_CASE: - return $this->caseTo( - $this->caseTo($key, static::PASCAL_CASE), - static::SNAKE_CASE - ); - - case static::PASCAL_CASE: - return $this->caseTo( - $this->caseTo($key, static::KEBAB_CASE), - static::PASCAL_CASE - ); - - default: - return $key; - } + return match ($this->case) { + static::KEBAB_CASE => $this->caseTo( + $this->caseTo($key, static::PASCAL_CASE), + static::KEBAB_CASE + ), + + static::CAMEL_CASE => $this->caseTo( + $this->caseTo($key, static::KEBAB_CASE), + static::CAMEL_CASE + ), + + static::SNAKE_CASE => $this->caseTo( + $this->caseTo($key, static::PASCAL_CASE), + static::SNAKE_CASE + ), + + static::PASCAL_CASE => $this->caseTo( + $this->caseTo($key, static::KEBAB_CASE), + static::PASCAL_CASE + ), + + default => $key, + }; } - protected function caseTo($key, int $case = 0) + protected function caseTo(mixed $key, int $case = 0): mixed { if (! is_string($key)) { return $key; } - switch ($case) { - case static::CAMEL_CASE: - return Str::camel($key); - - case static::KEBAB_CASE: - return Str::snake($key, '-'); - - case static::SNAKE_CASE: - return Str::snake($key); - - case static::PASCAL_CASE: - return Str::studly($key); - - default: - return $key; - } + return match ($case) { + static::CAMEL_CASE => Str::camel($key), + static::KEBAB_CASE => Str::snake($key, '-'), + static::SNAKE_CASE => Str::snake($key), + static::PASCAL_CASE => Str::studly($key), + default => $key, + }; } } diff --git a/src/Exceptions/FileDoesntExistsException.php b/src/Exceptions/FileDoesntExistsException.php deleted file mode 100644 index 4bdcd39..0000000 --- a/src/Exceptions/FileDoesntExistsException.php +++ /dev/null @@ -1,30 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/TheDragonCode/pretty-array - */ - -namespace DragonCode\PrettyArray\Exceptions; - -use Exception; - -class FileDoesntExistsException extends Exception -{ - public function __construct(string $filename) - { - $message = "File \"{$filename}\" doesn't exist"; - - parent::__construct($message, 500); - } -} diff --git a/src/Exceptions/UnknownCaseTypeException.php b/src/Exceptions/UnknownCaseTypeException.php index 31fbbfc..33cb320 100644 --- a/src/Exceptions/UnknownCaseTypeException.php +++ b/src/Exceptions/UnknownCaseTypeException.php @@ -23,6 +23,6 @@ class UnknownCaseTypeException extends Exception { public function __construct(string $type) { - parent::__construct("Unknown conversion type: {$type}", 500); + parent::__construct("Unknown conversion type: $type", 500); } } diff --git a/src/Services/File.php b/src/Services/File.php index d63cdfe..0370812 100644 --- a/src/Services/File.php +++ b/src/Services/File.php @@ -17,45 +17,26 @@ namespace DragonCode\PrettyArray\Services; -use DragonCode\PrettyArray\Exceptions\FileDoesntExistsException; use DragonCode\Support\Concerns\Makeable; -use DragonCode\Support\Facades\Helpers\Filesystem\File as FileSupport; +use DragonCode\Support\Facades\Filesystem\File as Storage; use DragonCode\Support\Facades\Tools\Stub; use DragonCode\Support\Tools\Stub as StubTool; /** - * @method static \DragonCode\PrettyArray\Services\File make(string $content = null) + * @method static File make(?string $content = null) */ class File { use Makeable; - protected $content; - - public function __construct(?string $content = null) - { - $this->content = $content; + public function __construct( + protected ?string $content = null + ) { } - /** - * @param string $filename - * - * @throws \DragonCode\PrettyArray\Exceptions\FileDoesntExistsException - * - * @return mixed - */ - public function load(string $filename) + public function load(string $filename): array { - if (! file_exists($filename)) { - throw new FileDoesntExistsException($filename); - } - - return require $filename; - } - - public function loadRaw(string $filename) - { - return file_get_contents($filename); + return Storage::load($filename); } public function store(string $path, string $stub = StubTool::PHP_ARRAY): void @@ -64,11 +45,6 @@ public function store(string $path, string $stub = StubTool::PHP_ARRAY): void '{{slot}}' => $this->content, ]); - FileSupport::store($path, $content); - } - - public function storeRaw(string $path): void - { - FileSupport::store($path, $this->content); + Storage::store($path, $content); } } diff --git a/src/Services/Formatter.php b/src/Services/Formatter.php index 7d962bd..734e63e 100644 --- a/src/Services/Formatter.php +++ b/src/Services/Formatter.php @@ -29,15 +29,15 @@ class Formatter implements Caseable use HasCastable; use Makeable; - protected $key_as_string = false; + protected bool $key_as_string = false; - protected $equals_align = false; + protected bool $equals_align = false; - protected $is_simple = false; + protected bool $is_simple = false; - protected $pad_length = 4; + protected int $pad_length = 4; - protected $line_break = PHP_EOL; + protected string $line_break = PHP_EOL; public function setKeyAsString(): void { @@ -64,7 +64,8 @@ public function raw(array $array, int $pad = 1): string $keys_size = $this->sizeKeys($array); $pad_length = $this->pad_length * $pad; - $formatted = '[' . $this->line_break; + + $formatted = '[' . $this->line_break; foreach ($array as $key => $value) { $key = $this->key($key, $keys_size); @@ -82,14 +83,12 @@ public function raw(array $array, int $pad = 1): string protected function pad(string $value, int $pad = 1, $type = STR_PAD_LEFT): string { - $pad += $type === STR_PAD_LEFT - ? strlen($value) - : 2; + $pad += $type === STR_PAD_LEFT ? strlen($value) : 2; return str_pad($value, $pad, ' ', $type); } - protected function value($value, int $pad = 1) + protected function value($value, int $pad = 1): mixed { if (! empty($value) && (is_array($value) || is_object($value))) { return $this->raw($value, $pad); @@ -98,7 +97,7 @@ protected function value($value, int $pad = 1) return $this->castValue($value); } - protected function key($key, int $size = 0) + protected function key(mixed $key, int $size = 0): string { $key = $this->isStringKey($key) ? "'{$key}'" : $key; @@ -111,20 +110,14 @@ protected function key($key, int $size = 0) protected function sizeKeys(array $array): int { - $sizes = Arr::longestStringLength( - array_keys($array) - ); + $sizes = Arr::of($array)->keys()->longestStringLength(); - return $this->key_as_string - ? $sizes + 2 - : $sizes; + return $this->key_as_string ? $sizes + 2 : $sizes; } protected function keySizeCollision($key, int $size): int { - $collision = is_numeric($key) - ? 0 - : ($this->isAlignAndString() ? -2 : 0); + $collision = is_numeric($key) ? 0 : ($this->isAlignAndString() ? -2 : 0); return $size + $collision; } diff --git a/tests/TestCase.php b/tests/TestCase.php index 67de65d..ea1c4ec 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -40,13 +40,6 @@ protected function getFile(string $filename): string ); } - /** - * @param string $filename - * - * @throws \DragonCode\PrettyArray\Exceptions\FileDoesntExistsException - * - * @return array - */ protected function requireFile(string $filename): array { return File::make()->load( @@ -54,13 +47,6 @@ protected function requireFile(string $filename): array ); } - /** - * @param string $filename - * - * @throws \DragonCode\PrettyArray\Exceptions\FileDoesntExistsException - * - * @return array - */ protected function requireSource(string $filename = 'source.php'): array { return $this->requireFile($filename);