Skip to content

Commit 4af216c

Browse files
author
Andrey Helldar
authored
Merge pull request #13 from TheDragonCode/4.x
Bump `dragon-code/support` to 6
2 parents c9ab2df + 422751e commit 4af216c

File tree

8 files changed

+59
-151
lines changed

8 files changed

+59
-151
lines changed

.github/workflows/phpunit.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ jobs:
88
strategy:
99
fail-fast: true
1010
matrix:
11-
php: [ "7.3", "7.4", "8.0" ]
11+
php: [ "8.0", "8.1" ]
1212

13-
name: PHP ${{ matrix.php }}, Support ${{ matrix.support }}
13+
name: PHP ${{ matrix.php }}
1414

1515
steps:
1616
- name: Checkout code

composer.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,14 @@
2222
"source": "https://github.com/TheDragonCode/pretty-array"
2323
},
2424
"require": {
25-
"php": "^7.3 || ^8.0",
25+
"php": "^8.0",
2626
"ext-dom": "*",
2727
"ext-mbstring": "*",
2828
"dragon-code/contracts": "^2.6",
29-
"dragon-code/support": "^5.0"
29+
"dragon-code/support": "^6.0"
3030
},
3131
"require-dev": {
32-
"phpunit/phpunit": "^9.0"
33-
},
34-
"conflict": {
35-
"andrey-helldar/pretty-array": "*"
32+
"phpunit/phpunit": "^9.5"
3633
},
3734
"suggest": {
3835
"symfony/thanks": "Give thanks (in the form of a GitHub) to your fellow PHP package maintainers"

src/Concerns/HasCases.php

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -55,63 +55,49 @@ protected function convertKeysCase(array $array): array
5555
return $result;
5656
}
5757

58-
protected function convertKeyCase($key)
58+
protected function convertKeyCase(mixed $key): mixed
5959
{
6060
if (! is_string($key)) {
6161
return $key;
6262
}
6363

64-
switch ($this->case) {
65-
case static::KEBAB_CASE:
66-
return $this->caseTo(
67-
$this->caseTo($key, static::PASCAL_CASE),
68-
static::KEBAB_CASE
69-
);
70-
71-
case static::CAMEL_CASE:
72-
return $this->caseTo(
73-
$this->caseTo($key, static::KEBAB_CASE),
74-
static::CAMEL_CASE
75-
);
76-
77-
case static::SNAKE_CASE:
78-
return $this->caseTo(
79-
$this->caseTo($key, static::PASCAL_CASE),
80-
static::SNAKE_CASE
81-
);
82-
83-
case static::PASCAL_CASE:
84-
return $this->caseTo(
85-
$this->caseTo($key, static::KEBAB_CASE),
86-
static::PASCAL_CASE
87-
);
88-
89-
default:
90-
return $key;
91-
}
64+
return match ($this->case) {
65+
static::KEBAB_CASE => $this->caseTo(
66+
$this->caseTo($key, static::PASCAL_CASE),
67+
static::KEBAB_CASE
68+
),
69+
70+
static::CAMEL_CASE => $this->caseTo(
71+
$this->caseTo($key, static::KEBAB_CASE),
72+
static::CAMEL_CASE
73+
),
74+
75+
static::SNAKE_CASE => $this->caseTo(
76+
$this->caseTo($key, static::PASCAL_CASE),
77+
static::SNAKE_CASE
78+
),
79+
80+
static::PASCAL_CASE => $this->caseTo(
81+
$this->caseTo($key, static::KEBAB_CASE),
82+
static::PASCAL_CASE
83+
),
84+
85+
default => $key,
86+
};
9287
}
9388

94-
protected function caseTo($key, int $case = 0)
89+
protected function caseTo(mixed $key, int $case = 0): mixed
9590
{
9691
if (! is_string($key)) {
9792
return $key;
9893
}
9994

100-
switch ($case) {
101-
case static::CAMEL_CASE:
102-
return Str::camel($key);
103-
104-
case static::KEBAB_CASE:
105-
return Str::snake($key, '-');
106-
107-
case static::SNAKE_CASE:
108-
return Str::snake($key);
109-
110-
case static::PASCAL_CASE:
111-
return Str::studly($key);
112-
113-
default:
114-
return $key;
115-
}
95+
return match ($case) {
96+
static::CAMEL_CASE => Str::camel($key),
97+
static::KEBAB_CASE => Str::snake($key, '-'),
98+
static::SNAKE_CASE => Str::snake($key),
99+
static::PASCAL_CASE => Str::studly($key),
100+
default => $key,
101+
};
116102
}
117103
}

src/Exceptions/FileDoesntExistsException.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/Exceptions/UnknownCaseTypeException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ class UnknownCaseTypeException extends Exception
2323
{
2424
public function __construct(string $type)
2525
{
26-
parent::__construct("Unknown conversion type: {$type}", 500);
26+
parent::__construct("Unknown conversion type: $type", 500);
2727
}
2828
}

src/Services/File.php

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,26 @@
1717

1818
namespace DragonCode\PrettyArray\Services;
1919

20-
use DragonCode\PrettyArray\Exceptions\FileDoesntExistsException;
2120
use DragonCode\Support\Concerns\Makeable;
22-
use DragonCode\Support\Facades\Helpers\Filesystem\File as FileSupport;
21+
use DragonCode\Support\Facades\Filesystem\File as Storage;
2322
use DragonCode\Support\Facades\Tools\Stub;
2423
use DragonCode\Support\Tools\Stub as StubTool;
2524

2625
/**
27-
* @method static \DragonCode\PrettyArray\Services\File make(string $content = null)
26+
* @method static File make(?string $content = null)
2827
*/
2928
class File
3029
{
3130
use Makeable;
3231

33-
protected $content;
34-
35-
public function __construct(?string $content = null)
36-
{
37-
$this->content = $content;
32+
public function __construct(
33+
protected ?string $content = null
34+
) {
3835
}
3936

40-
/**
41-
* @param string $filename
42-
*
43-
* @throws \DragonCode\PrettyArray\Exceptions\FileDoesntExistsException
44-
*
45-
* @return mixed
46-
*/
47-
public function load(string $filename)
37+
public function load(string $filename): array
4838
{
49-
if (! file_exists($filename)) {
50-
throw new FileDoesntExistsException($filename);
51-
}
52-
53-
return require $filename;
54-
}
55-
56-
public function loadRaw(string $filename)
57-
{
58-
return file_get_contents($filename);
39+
return Storage::load($filename);
5940
}
6041

6142
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
6445
'{{slot}}' => $this->content,
6546
]);
6647

67-
FileSupport::store($path, $content);
68-
}
69-
70-
public function storeRaw(string $path): void
71-
{
72-
FileSupport::store($path, $this->content);
48+
Storage::store($path, $content);
7349
}
7450
}

src/Services/Formatter.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ class Formatter implements Caseable
2929
use HasCastable;
3030
use Makeable;
3131

32-
protected $key_as_string = false;
32+
protected bool $key_as_string = false;
3333

34-
protected $equals_align = false;
34+
protected bool $equals_align = false;
3535

36-
protected $is_simple = false;
36+
protected bool $is_simple = false;
3737

38-
protected $pad_length = 4;
38+
protected int $pad_length = 4;
3939

40-
protected $line_break = PHP_EOL;
40+
protected string $line_break = PHP_EOL;
4141

4242
public function setKeyAsString(): void
4343
{
@@ -64,7 +64,8 @@ public function raw(array $array, int $pad = 1): string
6464

6565
$keys_size = $this->sizeKeys($array);
6666
$pad_length = $this->pad_length * $pad;
67-
$formatted = '[' . $this->line_break;
67+
68+
$formatted = '[' . $this->line_break;
6869

6970
foreach ($array as $key => $value) {
7071
$key = $this->key($key, $keys_size);
@@ -82,14 +83,12 @@ public function raw(array $array, int $pad = 1): string
8283

8384
protected function pad(string $value, int $pad = 1, $type = STR_PAD_LEFT): string
8485
{
85-
$pad += $type === STR_PAD_LEFT
86-
? strlen($value)
87-
: 2;
86+
$pad += $type === STR_PAD_LEFT ? strlen($value) : 2;
8887

8988
return str_pad($value, $pad, ' ', $type);
9089
}
9190

92-
protected function value($value, int $pad = 1)
91+
protected function value($value, int $pad = 1): mixed
9392
{
9493
if (! empty($value) && (is_array($value) || is_object($value))) {
9594
return $this->raw($value, $pad);
@@ -98,7 +97,7 @@ protected function value($value, int $pad = 1)
9897
return $this->castValue($value);
9998
}
10099

101-
protected function key($key, int $size = 0)
100+
protected function key(mixed $key, int $size = 0): string
102101
{
103102
$key = $this->isStringKey($key) ? "'{$key}'" : $key;
104103

@@ -111,20 +110,14 @@ protected function key($key, int $size = 0)
111110

112111
protected function sizeKeys(array $array): int
113112
{
114-
$sizes = Arr::longestStringLength(
115-
array_keys($array)
116-
);
113+
$sizes = Arr::of($array)->keys()->longestStringLength();
117114

118-
return $this->key_as_string
119-
? $sizes + 2
120-
: $sizes;
115+
return $this->key_as_string ? $sizes + 2 : $sizes;
121116
}
122117

123118
protected function keySizeCollision($key, int $size): int
124119
{
125-
$collision = is_numeric($key)
126-
? 0
127-
: ($this->isAlignAndString() ? -2 : 0);
120+
$collision = is_numeric($key) ? 0 : ($this->isAlignAndString() ? -2 : 0);
128121

129122
return $size + $collision;
130123
}

tests/TestCase.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,13 @@ protected function getFile(string $filename): string
4040
);
4141
}
4242

43-
/**
44-
* @param string $filename
45-
*
46-
* @throws \DragonCode\PrettyArray\Exceptions\FileDoesntExistsException
47-
*
48-
* @return array
49-
*/
5043
protected function requireFile(string $filename): array
5144
{
5245
return File::make()->load(
5346
$this->path($filename)
5447
);
5548
}
5649

57-
/**
58-
* @param string $filename
59-
*
60-
* @throws \DragonCode\PrettyArray\Exceptions\FileDoesntExistsException
61-
*
62-
* @return array
63-
*/
6450
protected function requireSource(string $filename = 'source.php'): array
6551
{
6652
return $this->requireFile($filename);

0 commit comments

Comments
 (0)