Skip to content

Commit aad4bae

Browse files
committed
Merge branch 'review'
2 parents 6ec6bc9 + d60fdcf commit aad4bae

File tree

14 files changed

+393
-367
lines changed

14 files changed

+393
-367
lines changed

src/Toolkit/Http/FormData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private function doGetData(
186186
$preserveKeys = $name === null || $hasArray || (
187187
Arr::isList($data)
188188
? $flags & FormDataFlag::PRESERVE_LIST_KEYS
189-
: (Arr::isIndexed($data)
189+
: (Arr::hasNumericKeys($data)
190190
? $flags & FormDataFlag::PRESERVE_NUMERIC_KEYS
191191
: $flags & FormDataFlag::PRESERVE_STRING_KEYS)
192192
);

src/Toolkit/PHPDoc/PHPDocUtil.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ public static function normaliseType(string $type, array $aliases = [], bool $st
504504
}
505505
}
506506

507+
/** @disregard P1006 */
507508
$types = array_unique(self::replaceTypes($types));
508509
if ($nullable ?? false) {
509510
$types[] = 'null';
@@ -513,10 +514,8 @@ public static function normaliseType(string $type, array $aliases = [], bool $st
513514
}
514515

515516
/**
516-
* @template T of string[]|string
517-
*
518-
* @param T $types
519-
* @return T
517+
* @param string[]|string $types
518+
* @return ($types is string[] ? string[] : string)
520519
*/
521520
private static function replaceTypes($types)
522521
{

src/Toolkit/Sync/AbstractSyncEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ private function _serialize(
606606
}
607607

608608
$isList = false;
609-
if (Arr::isIndexed($node)) {
609+
if (Arr::hasNumericKeys($node)) {
610610
$isList = true;
611611
$last = array_pop($path) . '[]';
612612
$path[] = $last;

src/Toolkit/Utility/Arr.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use ValueError;
1010

1111
/**
12-
* Work with arrays and iterables
12+
* Work with arrays and other iterables
1313
*
1414
* @api
1515
*/
@@ -463,7 +463,7 @@ public static function isList($value, bool $orEmpty = false): bool
463463
* @param mixed $value
464464
* @phpstan-assert-if-true array<int,mixed> $value
465465
*/
466-
public static function isIndexed($value, bool $orEmpty = false): bool
466+
public static function hasNumericKeys($value, bool $orEmpty = false): bool
467467
{
468468
if (!is_array($value)) {
469469
return false;
@@ -657,7 +657,7 @@ public static function implode(
657657

658658
/**
659659
* Trim characters from each value in an array of strings and Stringables
660-
* before removing or optionally replacing empty strings
660+
* before removing empty strings or replacing them with null values
661661
*
662662
* @template TKey
663663
*

src/Toolkit/Utility/Date.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ final class Date extends AbstractUtility
1818
/**
1919
* Get a DateTimeImmutable from a DateTimeInterface
2020
*
21-
* A shim for {@see DateTimeImmutable::createFromInterface()}.
21+
* @param DateTimeInterface|null $datetime If `null`, the current time is
22+
* used.
2223
*/
23-
public static function immutable(DateTimeInterface $datetime): DateTimeImmutable
24+
public static function immutable(?DateTimeInterface $datetime = null): DateTimeImmutable
2425
{
2526
return $datetime instanceof DateTimeImmutable
2627
? $datetime
27-
: DateTimeImmutable::createFromMutable($datetime);
28+
: ($datetime
29+
? DateTimeImmutable::createFromMutable($datetime)
30+
: new DateTimeImmutable());
2831
}
2932

3033
/**

src/Toolkit/Utility/Env.php

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@
55
use Salient\Utility\Exception\InvalidEnvFileSyntaxException;
66
use Salient\Utility\Exception\InvalidEnvironmentException;
77
use Closure;
8-
use InvalidArgumentException;
98
use RuntimeException;
109

1110
/**
1211
* Work with environment variables and .env files
1312
*
14-
* {@see Env::get()}, {@see Env::getInt()}, etc. check `$_ENV`, `$_SERVER` and
15-
* {@see getenv()} for a given variable and return the first value found. If the
16-
* value is not of the expected type, an {@see InvalidEnvironmentException} is
17-
* thrown. If the variable is not present in the environment, `$default` is
18-
* returned if given, otherwise an {@see InvalidEnvironmentException} is thrown.
19-
*
20-
* @todo Add support for variable expansion
13+
* Methods that get a value from the environment check `$_ENV`, `$_SERVER` and
14+
* {@see getenv()}, and return the first value found. If it is not of the
15+
* expected type, an {@see InvalidEnvironmentException} is thrown. If it is not
16+
* present in the environment, `$default` is returned if given, otherwise an
17+
* {@see InvalidEnvironmentException} is thrown.
2118
*
2219
* @api
2320
*/
@@ -97,7 +94,7 @@ public static function apply(int $flags = Env::APPLY_ALL): void
9794

9895
if ($flags & self::APPLY_TIMEZONE) {
9996
$tz = Regex::replace(
100-
['/^:?(.*\/zoneinfo\/)?/', '/^(UTC)0$/'],
97+
['/^:?(?:.*\/zoneinfo\/)?/', '/^(UTC)0$/'],
10198
['', '$1'],
10299
self::get('TZ', '')
103100
);
@@ -154,7 +151,9 @@ public static function unset(string $name): void
154151
*/
155152
public static function has(string $name): bool
156153
{
157-
return self::_get($name, false) !== false;
154+
return array_key_exists($name, $_ENV)
155+
|| array_key_exists($name, $_SERVER)
156+
|| getenv($name) !== false;
158157
}
159158

160159
/**
@@ -236,13 +235,11 @@ public static function getBool(string $name, $default = -1): ?bool
236235
* @template T of string[]|null|false
237236
*
238237
* @param T|Closure(): T $default
238+
* @param non-empty-string $delimiter
239239
* @return (T is string[] ? string[] : (T is null ? string[]|null : string[]|never))
240240
*/
241241
public static function getList(string $name, $default = false, string $delimiter = ','): ?array
242242
{
243-
if ($delimiter === '') {
244-
throw new InvalidArgumentException('Invalid delimiter');
245-
}
246243
$value = self::_get($name);
247244
if ($value === false) {
248245
return self::_default($name, $default, false);
@@ -256,13 +253,11 @@ public static function getList(string $name, $default = false, string $delimiter
256253
* @template T of int[]|null|false
257254
*
258255
* @param T|Closure(): T $default
256+
* @param non-empty-string $delimiter
259257
* @return (T is int[] ? int[] : (T is null ? int[]|null : int[]|never))
260258
*/
261259
public static function getIntList(string $name, $default = false, string $delimiter = ','): ?array
262260
{
263-
if ($delimiter === '') {
264-
throw new InvalidArgumentException('Invalid delimiter');
265-
}
266261
$value = self::_get($name);
267262
if ($value === false) {
268263
return self::_default($name, $default, false);
@@ -354,9 +349,9 @@ public static function getNullableBool(string $name, $default = -1): ?bool
354349
}
355350

356351
/**
357-
* @return ($assertValueIsString is true ? string|false : mixed)
352+
* @return string|false
358353
*/
359-
private static function _get(string $name, bool $assertValueIsString = true)
354+
private static function _get(string $name)
360355
{
361356
if (array_key_exists($name, $_ENV)) {
362357
$value = $_ENV[$name];
@@ -366,7 +361,7 @@ private static function _get(string $name, bool $assertValueIsString = true)
366361
$value = getenv($name, true);
367362
return $value === false ? getenv($name) : $value;
368363
}
369-
if ($assertValueIsString && !is_string($value)) {
364+
if (!is_string($value)) {
370365
throw new InvalidEnvironmentException(sprintf(
371366
'Value is not a string: %s',
372367
$name,
@@ -473,7 +468,8 @@ public static function setFlag(string $name, bool $value): void
473468
}
474469

475470
/**
476-
* Get the current user's home directory from the environment
471+
* Get the current user's home directory, or null if a value for the user's
472+
* home directory is not found in the environment
477473
*/
478474
public static function getHomeDir(): ?string
479475
{
@@ -495,8 +491,6 @@ public static function getHomeDir(): ?string
495491
* @param string[] $lines
496492
* @param array<string,string> $queue
497493
* @param string[] $errors
498-
* @param-out array<string,string> $queue
499-
* @param-out string[] $errors
500494
*/
501495
private static function parseLines(
502496
array $lines,
@@ -525,29 +519,19 @@ private static function parseLines(
525519

526520
/** @var string */
527521
$name = $matches['name'];
528-
if (
529-
array_key_exists($name, $_ENV)
530-
|| array_key_exists($name, $_SERVER)
531-
|| getenv($name) !== false
532-
) {
522+
if (self::has($name)) {
533523
continue;
534524
}
535525

536-
$double = $matches['double'];
537-
if ($double !== null) {
526+
if (($double = $matches['double']) !== null) {
538527
$queue[$name] = Regex::replace('/\\\\(["$\\\\`])/', '$1', $double);
539-
continue;
540-
}
541-
542-
$single = $matches['single'];
543-
if ($single !== null) {
528+
} elseif (($single = $matches['single']) !== null) {
544529
$queue[$name] = str_replace("'\''", "'", $single);
545-
continue;
530+
} else {
531+
/** @var string */
532+
$none = $matches['none'];
533+
$queue[$name] = Regex::replace('/\\\\(.)/', '$1', $none);
546534
}
547-
548-
/** @var string */
549-
$none = $matches['none'];
550-
$queue[$name] = Regex::replace('/\\\\(.)/', '$1', $none);
551535
}
552536
}
553537
}

0 commit comments

Comments
 (0)