From fa6bd2a5154698167095cfdd48e7f199b5364fd9 Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Tue, 27 Oct 2026 13:28:51 +0000 Subject: [PATCH] fix: resolve reliance on `Illuminate\Support\Str` --- .gitignore | 1 + src/Sniffs/Classes/ExceptionSuffixSniff.php | 7 +++--- .../Enums/PascalCasingEnumCasesSniff.php | 4 ++-- .../Laravel/ConfigFilenameKebabCaseSniff.php | 6 ++--- .../Laravel/DisallowHasFactorySniff.php | 23 ++++++++----------- .../Laravel/EventListenerSuffixSniff.php | 3 +-- src/Sniffs/Support/PropertyDoc.php | 10 ++++---- src/Utility.php | 18 +++++++++++++++ 8 files changed, 42 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 0bad97c..a865710 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ composer.lock vendor .idea .phpunit.cache +.fixture.lock diff --git a/src/Sniffs/Classes/ExceptionSuffixSniff.php b/src/Sniffs/Classes/ExceptionSuffixSniff.php index bb942c7..1d26472 100644 --- a/src/Sniffs/Classes/ExceptionSuffixSniff.php +++ b/src/Sniffs/Classes/ExceptionSuffixSniff.php @@ -2,7 +2,6 @@ namespace Worksome\CodingStyle\Sniffs\Classes; -use Illuminate\Support\Str; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; @@ -21,12 +20,12 @@ public function process(File $phpcsFile, int $stackPtr) { $baseClassNameLength = 0; - while (Str::endsWith($phpcsFile->getTokensAsString($stackPtr + 2, $baseClassNameLength + 1), '\\')) { + while (str_ends_with($phpcsFile->getTokensAsString($stackPtr + 2, $baseClassNameLength + 1), '\\')) { $baseClassNameLength += 2; } $baseClassName = $phpcsFile->getTokensAsString($stackPtr + 2, max($baseClassNameLength, 1)); - if (! Str::endsWith($baseClassName, $this->suffix) || ! Str::contains($baseClassName, 'Exception')) { + if (! str_ends_with($baseClassName, $this->suffix) || ! str_contains($baseClassName, 'Exception')) { return; } @@ -35,7 +34,7 @@ public function process(File $phpcsFile, int $stackPtr) $className = $phpcsFile->getTokensAsString($classNamePointer, 1); // Check if class is named with Exceptions suffix. - if (Str::endsWith($className, $this->suffix)) { + if (str_ends_with($className, $this->suffix)) { return; } diff --git a/src/Sniffs/Enums/PascalCasingEnumCasesSniff.php b/src/Sniffs/Enums/PascalCasingEnumCasesSniff.php index 9972b7e..05dcdba 100644 --- a/src/Sniffs/Enums/PascalCasingEnumCasesSniff.php +++ b/src/Sniffs/Enums/PascalCasingEnumCasesSniff.php @@ -4,9 +4,9 @@ namespace Worksome\CodingStyle\Sniffs\Enums; -use Illuminate\Support\Str; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +use Worksome\CodingStyle\Utility; class PascalCasingEnumCasesSniff implements Sniff { @@ -22,7 +22,7 @@ public function process(File $phpcsFile, int $stackPtr) $nameRef = $stackPtr + 2; $caseName = $phpcsFile->getTokensAsString($nameRef, 1); - if ($caseName === Str::studly($caseName)) { + if ($caseName === Utility::studly($caseName)) { return; } diff --git a/src/Sniffs/Laravel/ConfigFilenameKebabCaseSniff.php b/src/Sniffs/Laravel/ConfigFilenameKebabCaseSniff.php index 1ebe430..11491a4 100644 --- a/src/Sniffs/Laravel/ConfigFilenameKebabCaseSniff.php +++ b/src/Sniffs/Laravel/ConfigFilenameKebabCaseSniff.php @@ -2,9 +2,9 @@ namespace Worksome\CodingStyle\Sniffs\Laravel; -use Illuminate\Support\Str; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +use Worksome\CodingStyle\Utility; class ConfigFilenameKebabCaseSniff implements Sniff { @@ -25,9 +25,9 @@ public function process(File $phpcsFile, int $stackPtr) } $filenameWithExtension = basename($path); - $filename = Str::before($filenameWithExtension, '.'); + $filename = explode('.', $filenameWithExtension, 2)[0]; - if (Str::kebab($filename) === $filename) { + if (Utility::kebab($filename) === $filename) { return; } diff --git a/src/Sniffs/Laravel/DisallowHasFactorySniff.php b/src/Sniffs/Laravel/DisallowHasFactorySniff.php index 3e30a73..e6451bd 100644 --- a/src/Sniffs/Laravel/DisallowHasFactorySniff.php +++ b/src/Sniffs/Laravel/DisallowHasFactorySniff.php @@ -2,7 +2,6 @@ namespace Worksome\CodingStyle\Sniffs\Laravel; -use Illuminate\Support\Str; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; @@ -22,7 +21,7 @@ public function register(): array public function process(File $phpcsFile, int $stackPtr) { $traitNamePointer = $stackPtr + 2; - $className = Str::before($phpcsFile->getTokensAsString($traitNamePointer, 10), ';'); + $className = explode(';', $phpcsFile->getTokensAsString($traitNamePointer, 10), 2)[0]; $this->trackRelevantPartialNamespace($className); @@ -34,7 +33,8 @@ public function process(File $phpcsFile, int $stackPtr) private function isHasFactoryNamespace(string $givenNamespace): bool { if ($this->partial !== null && strlen($this->partial) > 0) { - $givenNamespace = $this->partial . Str::after($givenNamespace, Str::afterLast($this->partial, '\\')); + $lastSegment = substr((string) strrchr($this->partial, '\\'), 1); + $givenNamespace = $this->partial . (explode($lastSegment, $givenNamespace, 2)[1] ?? $givenNamespace); } if ($givenNamespace !== self::HAS_FACTORY_FQCN) { @@ -46,18 +46,15 @@ private function isHasFactoryNamespace(string $givenNamespace): bool private function trackRelevantPartialNamespace(string $givenNamespace): void { - Str::of(self::HAS_FACTORY_FQCN) - ->explode('\\') - ->skip(1) - ->reduce(function (string $carry, string $item) use ($givenNamespace) { - $updatedNamespacePartial = "{$carry}\\{$item}"; + $namespacePartial = 'Illuminate'; - if (Str::contains($givenNamespace, $updatedNamespacePartial)) { - $this->partial = $updatedNamespacePartial; - } + foreach (array_slice(explode('\\', self::HAS_FACTORY_FQCN), 1) as $item) { + $namespacePartial = "{$namespacePartial}\\{$item}"; - return $updatedNamespacePartial; - }, 'Illuminate'); + if (str_contains($givenNamespace, $namespacePartial)) { + $this->partial = $namespacePartial; + } + } } private function addError(File $phpcsFile, int $line): void diff --git a/src/Sniffs/Laravel/EventListenerSuffixSniff.php b/src/Sniffs/Laravel/EventListenerSuffixSniff.php index 139ed02..fd931e3 100644 --- a/src/Sniffs/Laravel/EventListenerSuffixSniff.php +++ b/src/Sniffs/Laravel/EventListenerSuffixSniff.php @@ -2,7 +2,6 @@ namespace Worksome\CodingStyle\Sniffs\Laravel; -use Illuminate\Support\Str; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; @@ -30,7 +29,7 @@ public function process(File $phpcsFile, int $stackPtr) $className = $phpcsFile->getTokensAsString($classNamePointer, 1); // Check if class is named with Listener suffix. - if (Str::endsWith($className, $this->suffix)) { + if (str_ends_with($className, $this->suffix)) { return; } diff --git a/src/Sniffs/Support/PropertyDoc.php b/src/Sniffs/Support/PropertyDoc.php index fcfc214..fc8e168 100644 --- a/src/Sniffs/Support/PropertyDoc.php +++ b/src/Sniffs/Support/PropertyDoc.php @@ -4,7 +4,6 @@ namespace Worksome\CodingStyle\Sniffs\Support; -use Illuminate\Support\Str; use InvalidArgumentException; use Worksome\CodingStyle\Utility; @@ -31,11 +30,10 @@ public static function from(string $content): self Utility::preg_match(self::regexForTypes(), $detail, $typeMatches); $types = $typeMatches[0]; - $remainderAfterType = Str::of($detail)->after($types)->trim(); + $remainderAfterType = trim(explode($types, $detail, 2)[1] ?? $detail); - if ($remainderAfterType->isNotEmpty()) { - $variable = $remainderAfterType->before(' ')->__toString(); - $description = $remainderAfterType->after(' ')->__toString(); + if ($remainderAfterType !== '') { + [$variable, $description] = explode(' ', $remainderAfterType, 2) + [1 => $remainderAfterType]; } else { $types = null; $variable = $typeMatches[0]; @@ -66,7 +64,7 @@ public function joined(): string return join(' ', array_filter([ $this->scope, $this->types, - Str::start($this->variableName, '$'), + '$' . ltrim($this->variableName, '$'), $this->description, ])); } diff --git a/src/Utility.php b/src/Utility.php index c7c9a5b..347fb8a 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -26,6 +26,24 @@ public static function preg_match( return $safeResult; } + public static function studly(string $value): string + { + $words = preg_split('/\s+/u', str_replace(['-', '_'], ' ', $value), -1, PREG_SPLIT_NO_EMPTY) ?: []; + + return implode('', array_map(ucfirst(...), $words)); + } + + public static function kebab(string $value): string + { + if (ctype_lower($value)) { + return $value; + } + + $value = (string) preg_replace('/\s+/u', '', ucwords($value)); + + return strtolower((string) preg_replace('/(.)(?=[A-Z])/u', '$1-', $value)); + } + private static function getPcreException(): RuntimeException { return new RuntimeException(match ($lastError = preg_last_error()) {