Skip to content

Commit 5a79055

Browse files
committed
Merge branch 'cleanup'
2 parents e2a5c09 + a9a44f2 commit 5a79055

File tree

10 files changed

+75
-143
lines changed

10 files changed

+75
-143
lines changed

src/Sli/Command/Generate/AbstractGenerateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ protected function handleOutput($lines): void
968968
$verb = null;
969969
} else {
970970
$file = sprintf('%s.php', $this->OutputClass);
971-
$dir = Package::namespacePath($this->OutputNamespace);
971+
$dir = Package::getNamespacePath($this->OutputNamespace);
972972

973973
if ($dir !== null) {
974974
if (!$this->Check) {

src/Toolkit/Console/ConsoleFormatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,12 @@ function (array $match) use (
544544
}
545545
if ($wrapToWidth !== null) {
546546
if (strlen($break) === 1) {
547-
$string = Str::wordwrap($string, $wrapToWidth, $break);
547+
$string = Str::wrap($string, $wrapToWidth, $break);
548548
} else {
549549
if (strpos($string, "\x7f") !== false) {
550550
$string = $this->insertPlaceholders($string, '/\x7f/', $replace);
551551
}
552-
$string = Str::wordwrap($string, $wrapToWidth, "\x7f");
552+
$string = Str::wrap($string, $wrapToWidth, "\x7f");
553553
$string = $this->insertPlaceholders($string, '/\x7f/', $replace, "\n", $break);
554554
}
555555
}

src/Toolkit/Container/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public function __construct(
326326

327327
Err::register();
328328

329-
$adodb = Package::packagePath('adodb/adodb-php');
329+
$adodb = Package::getPackagePath('adodb/adodb-php');
330330
if ($adodb !== null) {
331331
Err::silencePath($adodb);
332332
}

src/Toolkit/Utility/Format.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static function date(
147147
$format .= ' H:i:s T';
148148
}
149149

150-
return Str::wrap($date->format($format), $before, $after);
150+
return Str::enclose($date->format($format), $before, $after);
151151
}
152152

153153
/**
@@ -207,9 +207,9 @@ public static function dateRange(
207207

208208
return sprintf(
209209
'%s%s%s',
210-
Str::wrap($from->format($fromFormat), $before, $after),
210+
Str::enclose($from->format($fromFormat), $before, $after),
211211
$delimiter,
212-
Str::wrap($to->format($toFormat), $before, $after),
212+
Str::enclose($to->format($toFormat), $before, $after),
213213
);
214214
}
215215

src/Toolkit/Utility/Get.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,6 @@ final class Get extends AbstractUtility
5050
*/
5151
public const COPY_SINGLETONS = 16;
5252

53-
/**
54-
* Throw an exception if a value is null, otherwise return it
55-
*
56-
* @template T
57-
*
58-
* @param T $value
59-
* @return (T is null ? never : T)
60-
* @phpstan-param T|null $value
61-
* @phpstan-return ($value is null ? never : T)
62-
*/
63-
public static function notNull($value)
64-
{
65-
if ($value === null) {
66-
throw new InvalidArgumentException('$value cannot be null');
67-
}
68-
69-
return $value;
70-
}
71-
7253
/**
7354
* Cast a value to boolean, converting boolean strings and preserving null
7455
*

src/Toolkit/Utility/Package.php

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,28 @@ public static function name(): string
3939
/**
4040
* Get the commit reference of the root package, if known
4141
*/
42-
public static function reference(bool $short = true): ?string
42+
public static function ref(bool $short = true): ?string
4343
{
4444
/** @var string|null */
4545
$ref = self::getRootPackageValue('reference');
46-
return self::formatReference($ref, $short);
46+
return self::formatRef($ref, $short);
4747
}
4848

4949
/**
5050
* Get the version of the root package
5151
*
5252
* If Composer returns a version like `dev-*` or `v1.x-dev`, `@<reference>`
53-
* is added. Otherwise, if `$withReference` is `true` and a commit reference
54-
* is available, `-<reference>` is added.
53+
* is added. Otherwise, if `$withRef` is `true` and a commit reference is
54+
* available, `-<reference>` is added.
5555
*
5656
* @param bool $pretty If `true`, return the original version number, e.g.
5757
* `v1.2.3` instead of `1.2.3.0`.
5858
*/
59-
public static function version(
60-
bool $pretty = true,
61-
bool $withReference = false
62-
): string {
59+
public static function version(bool $pretty = true, bool $withRef = false): string
60+
{
6361
/** @var string */
6462
$version = self::getRootPackageValue($pretty ? 'pretty_version' : 'version');
65-
return self::formatVersion(
66-
$version,
67-
$withReference,
68-
fn() => self::reference()
69-
);
63+
return self::formatVersion($version, $withRef, fn() => self::ref());
7064
}
7165

7266
/**
@@ -82,56 +76,53 @@ public static function path(): string
8276
/**
8377
* Get the commit reference of an installed package, if known
8478
*/
85-
public static function packageReference(
86-
string $package,
87-
bool $short = true
88-
): ?string {
79+
public static function getPackageRef(string $package, bool $short = true): ?string
80+
{
8981
if (!self::isInstalled($package)) {
9082
return null;
9183
}
9284

93-
return self::formatReference(
94-
self::filterData(
95-
Installed::getReference($package),
96-
Installed::class,
97-
'getReference',
98-
$package,
99-
),
100-
$short,
85+
$ref = self::filterData(
86+
Installed::getReference($package),
87+
Installed::class,
88+
'getReference',
89+
$package,
10190
);
91+
92+
return self::formatRef($ref, $short);
10293
}
10394

10495
/**
10596
* Get the version of an installed package, or null if it is not installed
10697
*
10798
* If Composer returns a version like `dev-*` or `v1.x-dev`, `@<reference>`
108-
* is added. Otherwise, if `$withReference` is `true` and a commit reference
99+
* is added. Otherwise, if `$withRef` is `true` and a commit reference
109100
* is available, `-<reference>` is added.
110101
*
111102
* @param bool $pretty If `true`, return the original version number, e.g.
112103
* `v1.2.3` instead of `1.2.3.0`.
113104
*/
114-
public static function packageVersion(
105+
public static function getPackageVersion(
115106
string $package,
116107
bool $pretty = true,
117-
bool $withReference = false
108+
bool $withRef = false
118109
): ?string {
119110
if (!self::isInstalled($package)) {
120111
return null;
121112
}
122113

123114
return self::formatVersion(
124115
(string) self::getVersion($package, $pretty),
125-
$withReference,
126-
fn() => self::packageReference($package)
116+
$withRef,
117+
fn() => self::getPackageRef($package),
127118
);
128119
}
129120

130121
/**
131122
* Get the canonical path of an installed package, or null if it is not
132123
* installed
133124
*/
134-
public static function packagePath(string $package): ?string
125+
public static function getPackagePath(string $package): ?string
135126
{
136127
if (!self::isInstalled($package)) {
137128
return null;
@@ -153,7 +144,7 @@ public static function packagePath(string $package): ?string
153144
*
154145
* @see Package::namespacePath()
155146
*/
156-
public static function classPath(string $class): ?string
147+
public static function getClassPath(string $class): ?string
157148
{
158149
$class = ltrim($class, '\\');
159150
foreach (self::getRegisteredLoaders() as $loader) {
@@ -181,27 +172,24 @@ public static function classPath(string $class): ?string
181172
* namespace already exists. If no such prefix exists, preference is given
182173
* to the longest prefix.
183174
*/
184-
public static function namespacePath(string $namespace): ?string
175+
public static function getNamespacePath(string $namespace): ?string
185176
{
186177
$namespace = trim($namespace, '\\');
187178

188179
$prefixes = [];
189180
foreach (self::getRegisteredLoaders() as $loader) {
190-
$prefixes = array_merge_recursive(
191-
self::filterData(
192-
$loader->getPrefixesPsr4(),
193-
Loader::class,
194-
'getPrefixesPsr4',
195-
),
196-
$prefixes
181+
$loaderPrefixes = self::filterData(
182+
$loader->getPrefixesPsr4(),
183+
Loader::class,
184+
'getPrefixesPsr4',
197185
);
186+
$prefixes = array_merge_recursive($loaderPrefixes, $prefixes);
198187
}
199188

200189
// Sort prefixes from longest to shortest
201190
uksort(
202191
$prefixes,
203-
fn(string $p1, string $p2): int =>
204-
strlen($p2) <=> strlen($p1)
192+
fn(string $p1, string $p2): int => strlen($p2) <=> strlen($p1)
205193
);
206194

207195
foreach ($prefixes as $prefix => $dirs) {
@@ -292,14 +280,12 @@ private static function getRegisteredLoaders(): array
292280
* @param mixed ...$args
293281
* @return TData
294282
*/
295-
private static function filterData(
296-
$data,
297-
string $class,
298-
string $method,
299-
...$args
300-
) {
283+
private static function filterData($data, string $class, string $method, ...$args)
284+
{
301285
if (!class_exists(Event::class) || !Event::isLoaded()) {
286+
// @codeCoverageIgnoreStart
302287
return $data;
288+
// @codeCoverageIgnoreEnd
303289
}
304290
$event = new PackageDataReceivedEvent($data, $class, $method, ...$args);
305291
return Event::getInstance()->dispatch($event)->getData();
@@ -330,10 +316,8 @@ private static function formatVersion(
330316
return $version;
331317
}
332318

333-
private static function formatReference(
334-
?string $ref,
335-
bool $short
336-
): ?string {
319+
private static function formatRef(?string $ref, bool $short): ?string
320+
{
337321
if ($ref === null || !$short) {
338322
return $ref;
339323
}

src/Toolkit/Utility/Str.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ public static function splitDelimited(
529529
*
530530
* @param array{int,int}|int $width
531531
*/
532-
public static function wordwrap(
532+
public static function wrap(
533533
string $string,
534534
$width = 75,
535535
string $break = "\n",
@@ -605,7 +605,7 @@ public static function unwrap(
605605
* @param string|null $after If `null`, `$before` is used before and after
606606
* the string.
607607
*/
608-
public static function wrap(string $string, string $before, ?string $after = null): string
608+
public static function enclose(string $string, string $before, ?string $after = null): string
609609
{
610610
return $before . $string . ($after ?? $before);
611611
}

tests/unit/Toolkit/Utility/Get/GetTest.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,6 @@
2626
*/
2727
final class GetTest extends TestCase
2828
{
29-
/**
30-
* @dataProvider notNullProvider
31-
*
32-
* @param mixed $value
33-
*/
34-
public function testNotNull($value): void
35-
{
36-
$this->assertSame($value, Get::notNull($value));
37-
}
38-
39-
/**
40-
* @return array<array{mixed}>
41-
*/
42-
public function notNullProvider(): array
43-
{
44-
return [
45-
[0],
46-
[1],
47-
[''],
48-
['foo'],
49-
[[]],
50-
[['foo']],
51-
[new stdClass()],
52-
];
53-
}
54-
55-
public function testNotNullWithNull(): void
56-
{
57-
$this->expectException(InvalidArgumentException::class);
58-
$this->expectExceptionMessage('$value cannot be null');
59-
Get::notNull(null);
60-
}
61-
6229
/**
6330
* @dataProvider booleanProvider
6431
*

0 commit comments

Comments
 (0)