Skip to content

Commit 213c975

Browse files
committed
refactor(helpers): update str_snake function to use hyphen delimiter
- Change the default delimiter in str_snake from underscore to hyphen. - Remove unused escape_argument function and its related test. - Clean up code by removing unnecessary comments and imports. Signed-off-by: guanguans <ityaozm@gmail.com>
1 parent e71e044 commit 213c975

File tree

4 files changed

+10
-34
lines changed

4 files changed

+10
-34
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"homepage": "https://github.com/guanguans/soar-php",
3131
"support": {
3232
"issues": "https://github.com/guanguans/soar-php/issues",
33+
"source": "https://github.com/guanguans/soar-php",
3334
"discussions": "https://github.com/guanguans/soar-php/discussions"
3435
},
3536
"funding": [
@@ -121,8 +122,6 @@
121122
"phpstan/extension-installer": true,
122123
"symfony/thanks": true
123124
},
124-
"apcu-autoloader": true,
125-
"classmap-authoritative": false,
126125
"optimize-autoloader": true,
127126
"preferred-install": "dist",
128127
"sort-packages": true

src/Concerns/HasOptions.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,10 @@ public function __call(string $method, array $arguments): mixed
416416
{
417417
foreach (['set', 'with', 'get', 'only', 'except'] as $prefix) {
418418
if (str_starts_with($method, $prefix)) {
419-
$name = '-'.str_snake(substr($method, \strlen($prefix)), '-');
420-
$actionMethod = $prefix.'Option';
421-
422-
return $this->{$actionMethod}($name, ...$arguments);
419+
return $this->{$prefix.'Option'}(
420+
'-'.str_snake(substr($method, \strlen($prefix))),
421+
...$arguments
422+
);
423423
}
424424
}
425425

@@ -562,13 +562,11 @@ protected function getNormalizedOptions(): array
562562
*/
563563
private function normalizeOptions(array $options): array
564564
{
565-
$normalizedOptions = [];
566-
567565
foreach ($options as $name => $value) {
568-
$normalizedOptions[$name] = $this->normalizeOption($name, $value);
566+
$options[$name] = $this->normalizeOption($name, $value);
569567
}
570568

571-
return $normalizedOptions;
569+
return $options;
572570
}
573571

574572
/**

src/Support/helpers.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use Composer\Autoload\ClassLoader;
1717
use Illuminate\Support\Collection;
18-
use Symfony\Component\Process\Process;
1918

2019
if (!\function_exists('Guanguans\SoarPHP\Support\classes')) {
2120
/**
@@ -69,34 +68,19 @@ function classes(?callable $filter = null): Collection
6968
}
7069
}
7170

72-
if (!\function_exists('Guanguans\SoarPHP\Support\escape_argument')) {
73-
/**
74-
* Escapes a string to be used as a shell argument.
75-
*
76-
* @see https://github.com/johnstevenson/winbox-args
77-
* @see https://github.com/composer/composer/blob/main/src/Composer/Util/ProcessExecutor.php
78-
*/
79-
function escape_argument(?string $argument): string
80-
{
81-
return (fn (?string $argument): string => $this->escapeArgument($argument))->call(new Process([]), $argument);
82-
}
83-
}
84-
8571
if (!\function_exists('Guanguans\SoarPHP\Support\str_snake')) {
86-
function str_snake(string $value, string $delimiter = '_'): string
72+
function str_snake(string $value, string $delimiter = '-'): string
8773
{
88-
$key = $value;
89-
9074
/** @var array<string, array<string, string>> $snakeCache */
9175
static $snakeCache = [];
76+
$key = $value;
9277

9378
if (isset($snakeCache[$key][$delimiter])) {
9479
return $snakeCache[$key][$delimiter];
9580
}
9681

9782
if (!ctype_lower($value)) {
9883
$value = preg_replace('/\s+/u', '', ucwords($value));
99-
10084
$value = mb_strtolower((string) preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, (string) $value), 'UTF-8');
10185
}
10286

tests/Support/HelpersTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
use Illuminate\Support\Collection;
2222
use function Guanguans\SoarPHP\Support\classes;
23-
use function Guanguans\SoarPHP\Support\escape_argument;
2423
use function Guanguans\SoarPHP\Support\str_snake;
2524

2625
it('can get classes', function (): void {
@@ -30,10 +29,6 @@
3029
->toHaveCount(2);
3130
})->group(__DIR__, __FILE__);
3231

33-
it('can escape argument', function (): void {
34-
expect(escape_argument('foo bar baz'))->toBeString();
35-
})->group(__DIR__, __FILE__);
36-
3732
it('can snake string', function (): void {
38-
expect(str_snake(__FILE__))->toBe(str(__FILE__)->snake()->toString());
33+
expect(str_snake(__FILE__))->toBe(str(__FILE__)->snake('-')->toString());
3934
})->group(__DIR__, __FILE__);

0 commit comments

Comments
 (0)