Skip to content

Commit 1e0b74f

Browse files
Merge pull request #95 from VincentLanglet/refactoBis
📦 Refacto
2 parents 17543ce + d3e7e5a commit 1e0b74f

12 files changed

+148
-161
lines changed

SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,8 @@ public function process(File $phpcsFile, $stackPtr): void
3636
$tokens = $phpcsFile->getTokens();
3737

3838
if (T_ARRAY === $tokens[$stackPtr]['code']) {
39-
$phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no');
40-
4139
// Array keyword should be lower case.
4240
if (strtolower($tokens[$stackPtr]['content']) !== $tokens[$stackPtr]['content']) {
43-
if (strtoupper($tokens[$stackPtr]['content']) === $tokens[$stackPtr]['content']) {
44-
$phpcsFile->recordMetric($stackPtr, 'Array keyword case', 'upper');
45-
} else {
46-
$phpcsFile->recordMetric($stackPtr, 'Array keyword case', 'mixed');
47-
}
48-
4941
$fix = $phpcsFile->addFixableError(
5042
'Array keyword should be lower case; expected "array" but found "%s"',
5143
$stackPtr,
@@ -56,8 +48,6 @@ public function process(File $phpcsFile, $stackPtr): void
5648
if ($fix) {
5749
$phpcsFile->fixer->replaceToken($stackPtr, 'array');
5850
}
59-
} else {
60-
$phpcsFile->recordMetric($stackPtr, 'Array keyword case', 'lower');
6151
}
6252

6353
$arrayStart = $tokens[$stackPtr]['parenthesis_opener'];
@@ -79,7 +69,6 @@ public function process(File $phpcsFile, $stackPtr): void
7969
}
8070
}
8171
} else {
82-
$phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'yes');
8372
$arrayStart = $stackPtr;
8473
$arrayEnd = $tokens[$stackPtr]['bracket_closer'];
8574
}
@@ -449,17 +438,15 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
449438
);
450439

451440
if (T_COMMA !== $tokens[$trailingContent]['code']) {
452-
$phpcsFile->recordMetric($stackPtr, 'Array end comma', 'no');
453441
$fix = $phpcsFile->addFixableError(
454442
'Comma required after last value in array declaration',
455443
$trailingContent,
456444
'NoCommaAfterLast'
457445
);
446+
458447
if ($fix) {
459448
$phpcsFile->fixer->addContent($trailingContent, ',');
460449
}
461-
} else {
462-
$phpcsFile->recordMetric($stackPtr, 'Array end comma', 'yes');
463450
}
464451

465452
$lastValueLine = $stackPtr;

SymfonyCustom/Sniffs/Formatting/ConditionalReturnOrThrowSniff.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ public function process(File $phpcsFile, $stackPtr): void
2929
$tokens = $phpcsFile->getTokens();
3030
$opener = $phpcsFile->findPrevious([T_IF, T_CASE], $stackPtr);
3131

32-
if ($opener
32+
if (false !== $opener
3333
&& isset($tokens[$opener]['scope_closer'])
34-
&& $stackPtr <= $tokens[$opener]['scope_closer']) {
34+
&& $stackPtr <= $tokens[$opener]['scope_closer']
35+
) {
3536
$isClosure = $phpcsFile->findPrevious(T_CLOSURE, $stackPtr, $opener);
3637
if (false !== $isClosure) {
3738
return;

SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private function isClassUsed(File $phpcsFile, int $usePtr, int $classPtr): bool
311311
|| ('class' === $type
312312
&& ((T_DOC_COMMENT_STRING === $tokens[$classUsed]['code']
313313
&& preg_match(
314-
'/(\s|\||\(|\<|\,|^)'.preg_quote($searchName, '/').'(\s|\||\\\\|\<|\,|\>|$|\[\])/i',
314+
'/(\s|\||\(|\<|\,|\:|^)'.preg_quote($searchName, '/').'(\s|\||\\\\|\<|\,|\>|$|\[\])/i',
315315
$tokens[$classUsed]['content']
316316
))
317317
|| (T_DOC_COMMENT_TAG === $tokens[$classUsed]['code']

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
}

SymfonyCustom/Sniffs/NamingConventions/ValidFileNameSniff.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ public function process(File $phpcsFile, $stackPtr): void
4848
'Invalid',
4949
[$filename]
5050
);
51-
$phpcsFile->recordMetric($stackPtr, 'Alphanumeric filename', 'no');
52-
} else {
53-
$phpcsFile->recordMetric($stackPtr, 'Alphanumeric filename', 'yes');
5451
}
5552
}
5653
}

0 commit comments

Comments
 (0)