Skip to content

Commit 5f21c13

Browse files
📦 Refacto
1 parent 17543ce commit 5f21c13

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

SymfonyCustom/Sniffs/NamingConventions/ValidClassNameSniff.php

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,72 +39,81 @@ public function process(File $phpcsFile, $stackPtr): void
3939
if (T_INTERFACE === $tokens[$stackPtr]['code']) {
4040
$name = $phpcsFile->findNext(T_STRING, $stackPtr);
4141

42-
if ($name && substr($tokens[$name]['content'], -9) !== 'Interface') {
43-
$phpcsFile->addError(
44-
'Interface name is not suffixed with "Interface"',
45-
$stackPtr,
46-
'InvalidInterfaceName'
47-
);
48-
}
42+
$this->checkSuffix($phpcsFile, $stackPtr, $name, 'Interface');
4943
break;
5044
}
5145

5246
// Suffix traits with Trait
5347
if (T_TRAIT === $tokens[$stackPtr]['code']) {
5448
$name = $phpcsFile->findNext(T_STRING, $stackPtr);
5549

56-
if ($name && substr($tokens[$name]['content'], -5) !== 'Trait') {
57-
$phpcsFile->addError(
58-
'Trait name is not suffixed with "Trait"',
59-
$stackPtr,
60-
'InvalidTraitName'
61-
);
62-
}
50+
$this->checkSuffix($phpcsFile, $stackPtr, $name, 'Trait');
6351
break;
6452
}
6553

6654
// Suffix exceptions with Exception;
6755
if (T_EXTENDS === $tokens[$stackPtr]['code']) {
6856
$extend = $phpcsFile->findNext(T_STRING, $stackPtr);
6957

70-
if ($extend
71-
&& substr($tokens[$extend]['content'], -9) === 'Exception'
72-
) {
58+
if ($extend && substr($tokens[$extend]['content'], -9) === 'Exception') {
7359
$class = $phpcsFile->findPrevious(T_CLASS, $stackPtr);
7460
$name = $phpcsFile->findNext(T_STRING, $class);
7561

76-
if ($name
77-
&& substr($tokens[$name]['content'], -9) !== 'Exception'
78-
) {
79-
$phpcsFile->addError(
80-
'Exception name is not suffixed with "Exception"',
81-
$stackPtr,
82-
'InvalidExceptionName'
83-
);
84-
}
62+
$this->checkSuffix($phpcsFile, $stackPtr, $name, 'Exception');
8563
}
8664
break;
8765
}
8866

8967
// Prefix abstract classes with Abstract.
9068
if (T_ABSTRACT === $tokens[$stackPtr]['code']) {
91-
$name = $phpcsFile->findNext(T_STRING, $stackPtr);
92-
$function = $phpcsFile->findNext(T_FUNCTION, $stackPtr);
69+
$name = $phpcsFile->findNext([T_STRING, T_FUNCTION], $stackPtr);
9370

9471
// Making sure we're not dealing with an abstract function
95-
if ($name && (false === $function || $name < $function)
96-
&& substr($tokens[$name]['content'], 0, 8) !== 'Abstract'
97-
) {
98-
$phpcsFile->addError(
99-
'Abstract class name is not prefixed with "Abstract"',
100-
$stackPtr,
101-
'InvalidAbstractName'
102-
);
72+
if (false !== $name && T_FUNCTION !== $tokens[$name]['code']) {
73+
$this->checkPrefix($phpcsFile, $stackPtr, $name, 'Abstract');
10374
}
10475
break;
10576
}
10677

10778
$stackPtr++;
10879
}
10980
}
81+
82+
/**
83+
* @param File $phpcsFile
84+
* @param int $stackPtr
85+
* @param int|bool $name
86+
* @param string $prefix
87+
*/
88+
private function checkPrefix(File $phpcsFile, int $stackPtr, $name, string $prefix): void
89+
{
90+
$tokens = $phpcsFile->getTokens();
91+
92+
if (false !== $name && substr($tokens[$name]['content'], 0, strlen($prefix)) !== $prefix) {
93+
$phpcsFile->addError(
94+
"$prefix name is not prefixed with '$prefix'",
95+
$stackPtr,
96+
"Invalid{$prefix}Name"
97+
);
98+
}
99+
}
100+
101+
/**
102+
* @param File $phpcsFile
103+
* @param int $stackPtr
104+
* @param int|bool $name
105+
* @param string $suffix
106+
*/
107+
private function checkSuffix(File $phpcsFile, int $stackPtr, $name, string $suffix): void
108+
{
109+
$tokens = $phpcsFile->getTokens();
110+
111+
if (false !== $name && substr($tokens[$name]['content'], -strlen($suffix)) !== $suffix) {
112+
$phpcsFile->addError(
113+
"$suffix name is not suffixed with '$suffix'",
114+
$stackPtr,
115+
"Invalid{$suffix}Name"
116+
);
117+
}
118+
}
110119
}

0 commit comments

Comments
 (0)