Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ composer.lock
vendor
.idea
.phpunit.cache
.fixture.lock
7 changes: 3 additions & 4 deletions src/Sniffs/Classes/ExceptionSuffixSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Worksome\CodingStyle\Sniffs\Classes;

use Illuminate\Support\Str;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Sniffs/Enums/PascalCasingEnumCasesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Sniffs/Laravel/ConfigFilenameKebabCaseSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}

Expand Down
23 changes: 10 additions & 13 deletions src/Sniffs/Laravel/DisallowHasFactorySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Worksome\CodingStyle\Sniffs\Laravel;

use Illuminate\Support\Str;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

Expand All @@ -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);

Expand All @@ -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) {
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/Sniffs/Laravel/EventListenerSuffixSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Worksome\CodingStyle\Sniffs\Laravel;

use Illuminate\Support\Str;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 4 additions & 6 deletions src/Sniffs/Support/PropertyDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Worksome\CodingStyle\Sniffs\Support;

use Illuminate\Support\Str;
use InvalidArgumentException;
use Worksome\CodingStyle\Utility;

Expand All @@ -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];
Expand Down Expand Up @@ -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,
]));
}
Expand Down
18 changes: 18 additions & 0 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Loading