Skip to content

Commit d84bd76

Browse files
committed
Merge branch 'utility-cleanup'
2 parents 64dca11 + 286a353 commit d84bd76

32 files changed

+859
-617
lines changed

lk-util/Command/Generate/Concept/GenerateCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -532,25 +532,25 @@ protected function generate(array $innerBlocks = []): string
532532
$blocks[] = implode($line, $this->generateImports());
533533
}
534534

535-
$phpDoc = implode($blank, Arr::notEmpty([
536-
trim($this->Description ?? ''),
537-
trim($this->PhpDoc ?? ''),
538-
]));
535+
$phpDoc = Arr::trimAndImplode($blank, [
536+
$this->Description ?? '',
537+
$this->PhpDoc ?? '',
538+
]);
539539

540540
$lines =
541541
$phpDoc === ''
542542
? []
543543
: $this->generatePhpDocBlock($phpDoc);
544544

545-
$lines[] = implode(' ', Arr::notEmpty([
545+
$lines[] = Arr::implode(' ', [
546546
...$this->Modifiers,
547547
$this->OutputType,
548548
$this->OutputClass,
549549
$this->Extends ? 'extends' : '',
550550
implode(', ', $this->Extends),
551551
$this->Implements ? 'implements' : '',
552552
implode(', ', $this->Implements),
553-
]));
553+
]);
554554

555555
$members = [];
556556

lk-util/Command/Generate/GenerateSyncEntity.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
use Lkrms\Support\Catalog\RelationshipType;
1313
use Lkrms\Sync\Concept\HttpSyncProvider;
1414
use Lkrms\Sync\Concept\SyncEntity;
15+
use Lkrms\Utility\Arr;
1516
use Lkrms\Utility\Convert;
1617
use Lkrms\Utility\Inflect;
1718
use Lkrms\Utility\Pcre;
18-
use Lkrms\Utility\Test;
1919
use Closure;
2020
use DateTimeImmutable;
2121

@@ -327,7 +327,7 @@ protected static function getRemovablePrefixes(): ?array
327327
}
328328
}
329329

330-
if (Test::isListArray($entity)) {
330+
if (Arr::isList($entity)) {
331331
$entity = $entity[0];
332332
}
333333

@@ -370,7 +370,7 @@ protected static function getRemovablePrefixes(): ?array
370370
continue;
371371
}
372372

373-
if (Test::isArrayOfArrayKey($value, true) &&
373+
if (Arr::ofArrayKey($value, true) &&
374374
Pcre::match('/^(?<class>[[:alpha:]_][[:alnum:]_]*)Ids$/', $key, $matches)) {
375375
$key = $matches['class'];
376376
$properties[$key] = "{$key}[]|null";

lk-util/Command/Generate/GenerateSyncProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ protected function run(string ...$args)
204204
}
205205

206206
if ($this->NoMagic) {
207-
$paramCode = Arr::notEmpty([$context . ' $ctx', $paramCode]);
207+
$paramCode = Arr::whereNotEmpty([$context . ' $ctx', $paramCode]);
208208

209209
$phpDoc = [];
210210
if ($paramDoc !== '' &&
@@ -221,7 +221,7 @@ protected function run(string ...$args)
221221
'@method %s %s(%s)',
222222
$returnDoc,
223223
$opMethod[$op],
224-
implode(', ', Arr::notEmpty([$context . ' $ctx', $paramDoc])),
224+
Arr::implode(', ', [$context . ' $ctx', $paramDoc]),
225225
);
226226
}
227227
}

scripts/generate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function generated($commandOrFile): void
175175
$file = dirname(__DIR__) . '/.gitattributes';
176176
$attributes = preg_grep(
177177
'/(^#| linguist-generated$)/',
178-
Arr::notEmpty(Arr::trim(file($file))),
178+
Arr::trim(file($file)),
179179
\PREG_GREP_INVERT
180180
);
181181
// @phpstan-ignore-next-line

src/Auth/AccessToken.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33
namespace Lkrms\Auth;
44

55
use Lkrms\Concern\TFullyReadable;
6+
use Lkrms\Contract\IImmutable;
67
use Lkrms\Contract\IReadable;
7-
use Lkrms\Utility\Convert;
8+
use Lkrms\Utility\Date;
89
use DateTimeImmutable;
910
use DateTimeInterface;
11+
use InvalidArgumentException;
1012

1113
/**
12-
* An immutable access token
14+
* A token issued by an authorization provider for access to protected resources
1315
*
1416
* @property-read string $Token
1517
* @property-read string $Type
1618
* @property-read DateTimeImmutable|null $Expires
1719
* @property-read string[] $Scopes
1820
* @property-read array<string,mixed> $Claims
1921
*/
20-
final class AccessToken implements IReadable
22+
final class AccessToken implements IReadable, IImmutable
2123
{
2224
use TFullyReadable;
2325

@@ -47,17 +49,30 @@ final class AccessToken implements IReadable
4749
protected $Claims;
4850

4951
/**
50-
* @param DateTimeInterface|int|null $expires
52+
* Creates a new AccessToken object
53+
*
54+
* @param DateTimeInterface|int|null $expires `null` if the access token's
55+
* lifetime is unknown, otherwise a {@see DateTimeInterface} or Unix
56+
* timestamp representing its expiration time.
5157
* @param string[]|null $scopes
5258
* @param array<string,mixed>|null $claims
5359
*/
5460
public function __construct(string $token, string $type, $expires, ?array $scopes = null, ?array $claims = null)
5561
{
62+
if (is_int($expires) && $expires < 0) {
63+
throw new InvalidArgumentException(sprintf(
64+
'Invalid $expires: %d',
65+
$expires
66+
));
67+
}
68+
5669
$this->Token = $token;
5770
$this->Type = $type;
5871
$this->Expires = $expires instanceof DateTimeInterface
59-
? Convert::toDateTimeImmutable($expires)
60-
: ($expires !== null && $expires > 0 ? new DateTimeImmutable("@$expires") : null);
72+
? Date::immutable($expires)
73+
: ($expires === null
74+
? null
75+
: new DateTimeImmutable("@$expires"));
6176
$this->Scopes = $scopes ?: [];
6277
$this->Claims = $claims ?: [];
6378
}

src/Cli/CliApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private function getUsage(string $name, $node, bool $terse = false): ?string
268268

269269
if ($terse) {
270270
return Formatter::escapeTags("$synopses\n\nSee '"
271-
. (Arr::implodeNotEmpty(' ', ["$progName help", $name, '<command>']))
271+
. (Arr::implode(' ', ["$progName help", $name, '<command>']))
272272
. "' for more information.");
273273
}
274274

src/Cli/CliCommand.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ final public function getSynopsis(bool $withMarkup = true, ?int $width = 80, boo
365365

366366
$name = $b . $this->getNameWithProgram() . $b;
367367
$full = $this->getOptionsSynopsis($withMarkup, $style, $collapsed);
368-
$synopsis = Arr::implodeNotEmpty(' ', [$name, $full]);
368+
$synopsis = Arr::implode(' ', [$name, $full]);
369369

370370
if ($width !== null) {
371371
$wrapped = $prefix . str_replace(
@@ -378,7 +378,7 @@ final public function getSynopsis(bool $withMarkup = true, ?int $width = 80, boo
378378
return $wrapped;
379379
}
380380

381-
$synopsis = Arr::implodeNotEmpty(' ', [$name, $collapsed]);
381+
$synopsis = Arr::implode(' ', [$name, $collapsed]);
382382
}
383383

384384
return $prefix . $this
@@ -750,8 +750,8 @@ private function loadOptionValues(): void
750750

751751
/**
752752
* @param string[] $args
753-
* @param array<string,array<string|int>|string|int|bool|null> $argValues
754-
* @return array<string,array<string|int>|string|int|bool|null>
753+
* @param array<string,array<string|int|true>|string|int|bool|null> $argValues
754+
* @return array<string,array<string|int|true>|string|int|bool|null>
755755
*/
756756
private function mergeArguments(
757757
array $args,
@@ -771,7 +771,7 @@ function (string $key, $value) use (&$argValues, &$saved, &$option) {
771771
($option->IsFlag && !$option->MultipleAllowed)) {
772772
$argValues[$key] = $value;
773773
} else {
774-
$argValues[$key] = array_merge((array) $argValues[$key], Convert::toArray($value));
774+
$argValues[$key] = array_merge((array) $argValues[$key], Arr::wrap($value));
775775
}
776776
};
777777
$merged = [];
@@ -870,7 +870,7 @@ function (string $key, $value) use (&$argValues, &$saved, &$option) {
870870

871871
if (array_key_exists($key, $merged) &&
872872
!($option->IsFlag && !$option->MultipleAllowed)) {
873-
$merged[$key] = array_merge((array) $merged[$key], Convert::toArray($value));
873+
$merged[$key] = array_merge((array) $merged[$key], Arr::wrap($value));
874874
} else {
875875
$merged[$key] = $value;
876876
}
@@ -1009,8 +1009,8 @@ final protected function getDefaultOptionValues(?callable $nameCallback = null):
10091009
* Optionally normalise an array of option values, assign them to the
10101010
* command, and return them to the caller
10111011
*
1012-
* @param array<string,array<string|int>|string|int|bool|null> $values An
1013-
* array that maps options to values.
1012+
* @param array<string,array<string|int|true>|string|int|bool|null> $values
1013+
* An array that maps options to values.
10141014
* @param bool $normalise `false` if `$value` has already been normalised.
10151015
* @param bool $expand If `true`, replace `null` (or `true`, if the option
10161016
* is not a flag and doesn't have type {@see CliOptionValueType::BOOLEAN})
@@ -1163,7 +1163,7 @@ final protected function getEffectiveCommandLine(
11631163
$positional = [];
11641164
foreach ($this->Options as $option) {
11651165
$name = null;
1166-
foreach (Arr::notEmpty([$option->Long, $option->Short]) as $key) {
1166+
foreach (Arr::whereNotEmpty([$option->Long, $option->Short]) as $key) {
11671167
if (array_key_exists($key, $values)) {
11681168
$name = $key;
11691169
break;
@@ -1182,7 +1182,7 @@ final protected function getEffectiveCommandLine(
11821182
}
11831183
array_push($args, ...$positional);
11841184

1185-
return array_values(Arr::notNull($args));
1185+
return array_values(Arr::whereNotNull($args));
11861186
}
11871187

11881188
/**

src/Cli/CliOption.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Lkrms\Contract\ProvidesBuilder;
1818
use Lkrms\Facade\Console;
1919
use Lkrms\Support\Catalog\CharacterSequence as Char;
20+
use Lkrms\Utility\Arr;
2021
use Lkrms\Utility\Assert;
2122
use Lkrms\Utility\Convert;
2223
use Lkrms\Utility\Env;
@@ -377,8 +378,8 @@ public function __construct(
377378
* Split delimited values into an array, if possible
378379
*
379380
* @internal
380-
* @param array<string|int>|string|int|bool|null $value
381-
* @return array<string|int>
381+
* @param array<string|int|true>|string|int|bool|null $value
382+
* @return array<string|int|true>
382383
*/
383384
public function maybeSplitValue($value): array
384385
{
@@ -529,7 +530,7 @@ function (&$value) {
529530
* UnknownValuePolicy
530531
*
531532
* @internal
532-
* @template T of array<string|int>|string|int|bool|null
533+
* @template T of array<string|int|true>|string|int|bool|null
533534
* @param T $value
534535
* @return T
535536
*/
@@ -621,7 +622,7 @@ public function isOriginalDefaultValue($value): bool
621622
* it to the caller
622623
*
623624
* @internal
624-
* @param array<string|int>|string|int|bool|null $value
625+
* @param array<string|int|true>|string|int|bool|null $value
625626
* @param bool $normalise `false` if `$value` has already been normalised.
626627
* @param bool $expand If `true`, replace `null` (or `true`, if the option
627628
* is not a flag and doesn't have type {@see CliOptionValueType::BOOLEAN})
@@ -646,7 +647,7 @@ public function applyValue(
646647
* value to the option's value type
647648
*
648649
* @internal
649-
* @param array<string|int>|string|int|bool|null $value
650+
* @param array<string|int|true>|string|int|bool|null $value
650651
* @param bool $expand If `true`, replace `null` (or `true`, if the option
651652
* is not a flag and doesn't have type {@see CliOptionValueType::BOOLEAN})
652653
* with the default value of the option if it has an optional value.
@@ -681,7 +682,7 @@ public function normaliseValue(
681682
} else {
682683
if (is_array($value)) {
683684
if ($this->IsFlag &&
684-
(!$this->MultipleAllowed || !Test::isArrayOfValue($value, true))) {
685+
(!$this->MultipleAllowed || Arr::unique($value) !== [true])) {
685686
$this->throwValueTypeException(
686687
$value,
687688
$this->MultipleAllowed

src/Console/ConsoleWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ public function group(
781781
?string $msg2 = null
782782
) {
783783
$this->GroupLevel++;
784-
$this->GroupMessageStack[] = Arr::implodeNotEmpty(' ', [$msg1, $msg2]);
784+
$this->GroupMessageStack[] = Arr::implode(' ', [$msg1, $msg2]);
785785

786786
return $this->write(Level::NOTICE, $msg1, $msg2, Type::GROUP_START);
787787
}

src/Container/Application.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private function getPath(
154154
}
155155

156156
return $this->checkPath(
157-
Arr::implodeNotEmpty('/', [$path, $app, $windowsChild]),
157+
Arr::implode('/', [$path, $app, $windowsChild]),
158158
$name,
159159
$create,
160160
$save,
@@ -182,7 +182,7 @@ private function getPath(
182182
}
183183

184184
return $this->checkPath(
185-
Arr::implodeNotEmpty('/', [$path, $app, $child]),
185+
Arr::implode('/', [$path, $app, $child]),
186186
$name,
187187
$create,
188188
$save,

0 commit comments

Comments
 (0)