Repository navigation
Expand file tree
/
Copy pathCamelCaseRouteNameSniff.php
More file actions
61 lines (50 loc) · 1.82 KB
/
Copy pathCamelCaseRouteNameSniff.php
File metadata and controls
61 lines (50 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php declare(strict_types=1);
namespace IxDFCodingStandard\Sniffs\NamingConventions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
final class CamelCaseRouteNameSniff implements Sniff
{
public const string CODE_NOT_CAMEL_CASE_ROUTE_NAME = 'NotCamelCaseRouteName';
private const string PATTERN_CAMEL_CASE = '/^[^-_\s]++$/';
/** @inheritDoc */
public function register(): array
{
return [\T_STRING];
}
/** @inheritDoc */
public function process(File $phpcsFile, $stringPointer): void
{
$tokens = $phpcsFile->getTokens();
$currentToken = $tokens[$stringPointer];
if ($currentToken['content'] !== 'name') {
return;
}
$this->processRouteName($phpcsFile, $stringPointer, $tokens);
}
/**
* Finds the route name and tests it against the camel case pattern.
* If an issue is found, it adds an error.
* @param array<string, int, array<string>> $tokens
**/
public function processRouteName(File $phpcsFile, int $stackPointer, array $tokens): void
{
$routeNameLocation = $phpcsFile->findNext(\T_CONSTANT_ENCAPSED_STRING, $stackPointer);
$routeName = $tokens[$routeNameLocation]['content'];
if (!$this->isCamelCase($routeName)) {
$phpcsFile->addError(
'Route name is not camel cased',
$routeNameLocation,
self::CODE_NOT_CAMEL_CASE_ROUTE_NAME
);
}
}
/**
* Checks each part of the route name to determine whether
* it matches the camel case pattern or not
*/
private function isCamelCase(string $routeName): bool
{
$parts = explode('.', $routeName);
return array_all($parts, static fn($part): bool => !(preg_match(self::PATTERN_CAMEL_CASE, $part) !== 1));
}
}