Skip to content

Commit b109e05

Browse files
authored
Merge pull request #76 from inpsyde/feature/55
Add rule for space after cast and space after Not
2 parents 7de8480 + 472ccff commit b109e05

15 files changed

+57
-51
lines changed

Inpsyde/Helpers/Boundaries.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static function arrayBoundaries(File $file, int $position): array
9191
return [-1, -1];
9292
}
9393

94-
return [(int)$openClose['opener'], (int)$openClose['closer']];
94+
return [(int) $openClose['opener'], (int) $openClose['closer']];
9595
}
9696

9797
/**
@@ -112,8 +112,8 @@ private static function startEnd(File $file, int $position): array
112112
return [$start + 1, $file->findEndOfStatement($start)];
113113
}
114114

115-
$start = (int)($token['scope_opener'] ?? 0);
116-
$end = (int)($token['scope_closer'] ?? 0);
115+
$start = (int) ($token['scope_opener'] ?? 0);
116+
$end = (int) ($token['scope_closer'] ?? 0);
117117
if (($start <= 0) || ($end <= 0) || ($start >= ($end - 1))) {
118118
return [-1, -1];
119119
}

Inpsyde/Helpers/FunctionDocBlock.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ public static function allTags(
6666
return [];
6767
}
6868

69-
$functionLine = (int)($tokens[$position]['line'] ?? -1);
70-
$closeLine = (int)($tokens[$closeTag]['line'] ?? -1);
69+
$functionLine = (int) ($tokens[$position]['line'] ?? -1);
70+
$closeLine = (int) ($tokens[$closeTag]['line'] ?? -1);
7171
if ($closeLine !== ($functionLine - 1)) {
7272
return [];
7373
}
7474

7575
/** @var array<int, array{string, string}> $tags */
7676
$tags = [];
77-
$start = (int)$tokens[$closeTag]['comment_opener'] + 1;
77+
$start = (int) $tokens[$closeTag]['comment_opener'] + 1;
7878
$key = -1;
7979
$inTag = false;
8080

@@ -84,7 +84,7 @@ public static function allTags(
8484
continue;
8585
}
8686

87-
$content = (string)$tokens[$i]['content'];
87+
$content = (string) $tokens[$i]['content'];
8888
if (($tokens[$i]['code'] === T_DOC_COMMENT_TAG)) {
8989
$inTag = true;
9090
$key++;

Inpsyde/Helpers/Misc.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static function tokensSubsetToString(
136136

137137
$content = '';
138138
foreach ($filtered as $token) {
139-
$content .= (string)($token['content'] ?? '');
139+
$content .= (string) ($token['content'] ?? '');
140140
}
141141

142142
return $content;

Inpsyde/Helpers/Names.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function nameableTokenName(File $file, int $position): ?string
6767
}
6868

6969
if ($code === T_VARIABLE) {
70-
$name = ltrim((string)($tokens[$position]['content'] ?? ''), '$');
70+
$name = ltrim((string) ($tokens[$position]['content'] ?? ''), '$');
7171

7272
return ($name === '') ? null : $name;
7373
}
@@ -79,7 +79,7 @@ public static function nameableTokenName(File $file, int $position): ?string
7979
}
8080

8181
$namePosition = $file->findNext(T_STRING, $position, null, false, null, true);
82-
$name = ($namePosition === false) ? null : (string)$tokens[$namePosition]['content'];
82+
$name = ($namePosition === false) ? null : (string) $tokens[$namePosition]['content'];
8383

8484
return ($name === '') ? null : $name;
8585
}

Inpsyde/Helpers/WpHooks.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ public static function isHookClosure(
8787
*/
8888
public static function isHookFunction(File $file, int $position): bool
8989
{
90-
return (bool)FunctionDocBlock::tag('@wp-hook', $file, $position);
90+
return (bool) FunctionDocBlock::tag('@wp-hook', $file, $position);
9191
}
9292
}

Inpsyde/Sniffs/CodeQuality/ForbiddenPublicPropertySniff.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private function isSniffClass(File $file, int $position): bool
8282
{
8383
$classNameTokenPosition = $file->findNext(
8484
T_STRING,
85-
(int)$file->findPrevious(T_CLASS, $position)
85+
(int) $file->findPrevious(T_CLASS, $position)
8686
);
8787

8888
if ($classNameTokenPosition === false) {
@@ -93,7 +93,7 @@ private function isSniffClass(File $file, int $position): bool
9393
$tokens = $file->getTokens();
9494
$classNameToken = $tokens[$classNameTokenPosition];
9595

96-
if (substr((string)$classNameToken['content'], -5, 5) === 'Sniff') {
96+
if (substr((string) $classNameToken['content'], -5, 5) === 'Sniff') {
9797
return true;
9898
}
9999

Inpsyde/Sniffs/CodeQuality/FunctionBodyStartSniff.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public function process(File $phpcsFile, $stackPtr): void
5757
$tokens = $phpcsFile->getTokens();
5858
$token = $tokens[$stackPtr] ?? [];
5959

60-
$scopeOpener = (int)($token['scope_opener'] ?? -1);
61-
$scopeCloser = (int)($token['scope_closer'] ?? -1);
60+
$scopeOpener = (int) ($token['scope_opener'] ?? -1);
61+
$scopeCloser = (int) ($token['scope_closer'] ?? -1);
6262

6363
if ($scopeOpener < 0 || $scopeCloser < 0 || $scopeCloser <= $scopeOpener) {
6464
return;
@@ -76,8 +76,8 @@ public function process(File $phpcsFile, $stackPtr): void
7676

7777
[$code, $message, $expectedLine] = $this->checkBodyStart(
7878
$bodyStart,
79-
(int)($tokens[$scopeOpener]['line'] ?? -1),
80-
(int)($token['line'] ?? -1),
79+
(int) ($tokens[$scopeOpener]['line'] ?? -1),
80+
(int) ($token['line'] ?? -1),
8181
$phpcsFile
8282
);
8383

@@ -107,7 +107,7 @@ private function checkBodyStart(
107107

108108
/** @var array<int, array<string, mixed>> $tokens */
109109
$tokens = $file->getTokens();
110-
$bodyLine = (int)($tokens[$bodyStart]['line'] ?? -1);
110+
$bodyLine = (int) ($tokens[$bodyStart]['line'] ?? -1);
111111

112112
$isMultiLineDeclare = ($openerLine - $functionLine) > 1;
113113
$isSingleLineDeclare = $openerLine === ($functionLine + 1);
@@ -163,7 +163,7 @@ private function fix(int $bodyStart, int $expectedLine, int $scopeOpener, File $
163163
{
164164
/** @var array<int, array<string, mixed>> $tokens */
165165
$tokens = $file->getTokens();
166-
$currentLine = (int)($tokens[$bodyStart]['line'] ?? -1);
166+
$currentLine = (int) ($tokens[$bodyStart]['line'] ?? -1);
167167

168168
if ($currentLine === $expectedLine) {
169169
return;

Inpsyde/Sniffs/CodeQuality/FunctionLengthSniff.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ private function structureLinesCount(File $file, int $position): int
103103
return 0;
104104
}
105105

106-
$start = (int)$token['scope_opener'];
107-
$end = (int)$token['scope_closer'];
108-
$length = (int)$tokens[$end]['line'] - (int)$tokens[$start]['line'];
106+
$start = (int) $token['scope_opener'];
107+
$end = (int) $token['scope_closer'];
108+
$length = (int) $tokens[$end]['line'] - (int) $tokens[$start]['line'];
109109

110110
if ($length < $this->maxLength) {
111111
return $length;
@@ -138,7 +138,7 @@ private function collectLinesToExclude(int $start, int $end, array $tokens): int
138138
$empty = array_filter(array_column($linesData, 'empty'));
139139
$onlyComment = array_filter(array_column($linesData, 'only-comment'));
140140

141-
$toExcludeCount = (int)array_sum($docblocks);
141+
$toExcludeCount = (int) array_sum($docblocks);
142142
if ($this->ignoreBlankLines) {
143143
$toExcludeCount += count($empty);
144144
}
@@ -156,7 +156,7 @@ private function collectLinesToExclude(int $start, int $end, array $tokens): int
156156
*/
157157
private function ignoredLinesData(array $token, array $lines): array
158158
{
159-
$line = (int)$token['line'];
159+
$line = (int) $token['line'];
160160
if (!array_key_exists($line, $lines)) {
161161
$lines[$line] = ['empty' => true, 'only-comment' => true];
162162
}
@@ -189,7 +189,7 @@ private function docBlocksData(array $tokens, int $position, array $docBlocks):
189189

190190
$closer = $tokens[$position]['comment_closer'] ?? null;
191191
$docBlocks[] = is_numeric($closer)
192-
? 1 + ((int)$tokens[(int)$closer]['line'] - (int)$tokens[$position]['line'])
192+
? 1 + ((int) $tokens[(int) $closer]['line'] - (int) $tokens[$position]['line'])
193193
: 1;
194194

195195
return $docBlocks;
@@ -208,7 +208,7 @@ private function normalizeIgnoreFlags(): void
208208

209209
foreach ($flags as $flag) {
210210
if (is_string($this->{$flag})) {
211-
$this->{$flag} = (bool)filter_var($this->{$flag}, FILTER_VALIDATE_BOOLEAN);
211+
$this->{$flag} = (bool) filter_var($this->{$flag}, FILTER_VALIDATE_BOOLEAN);
212212
}
213213
}
214214
}

Inpsyde/Sniffs/CodeQuality/LineLengthSniff.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private function collectLongLinesData(File $file, int $start): array
113113
for ($i = $start; $i < $file->numTokens; $i++) {
114114
// Still processing previous line: increment length and continue.
115115
if ($lastLine && ($tokens[$i]['line'] === $lastLine)) {
116-
$content = (string)$tokens[$i]['content'];
116+
$content = (string) $tokens[$i]['content'];
117117
$data[$lastLine]['length'] += strlen($content);
118118
$data[$lastLine]['nonEmptyLength'] += strlen(trim($content));
119119
continue;
@@ -124,8 +124,8 @@ private function collectLongLinesData(File $file, int $start): array
124124
$data[$lastLine]['end'] = $i - 1;
125125
}
126126

127-
$lastLine = (int)$tokens[$i]['line'];
128-
$content = (string)$tokens[$i]['content'];
127+
$lastLine = (int) $tokens[$i]['line'];
128+
$content = (string) $tokens[$i]['content'];
129129
$data[$lastLine] = [
130130
'length' => strlen($content),
131131
'nonEmptyLength' => strlen(trim($content)),
@@ -228,7 +228,7 @@ private function isLongHtmlAttribute(
228228
$inPhp = true;
229229
}
230230
if ($tokens[$i]['code'] === T_INLINE_HTML || $inPhp) {
231-
$tokenContent = (string)$tokens[$i]['content'];
231+
$tokenContent = (string) $tokens[$i]['content'];
232232
$content .= $inPhp ? str_repeat('x', strlen($tokenContent)) : $tokenContent;
233233
}
234234
if ($tokens[$i]['code'] === T_CLOSE_TAG && $inPhp) {
@@ -271,7 +271,12 @@ private function isLongSingleWord(
271271
array $tokens
272272
): bool {
273273

274-
$words = preg_split('~\s+~', (string)$tokens[$position]['content'], 2, PREG_SPLIT_NO_EMPTY);
274+
$words = preg_split(
275+
'~\s+~',
276+
(string) $tokens[$position]['content'],
277+
2,
278+
PREG_SPLIT_NO_EMPTY
279+
);
275280

276281
// If multiple words exceed line limit, we can split each word in its own line
277282
if ($words === false || count($words) !== 1) {
@@ -281,7 +286,7 @@ private function isLongSingleWord(
281286
$word = reset($words);
282287
$firstNonWhitePos = $file->findNext(T_WHITESPACE, $position, $lineEnd, true);
283288
$firstNonWhite = ($firstNonWhitePos === false) ? null : $tokens[$firstNonWhitePos];
284-
$tolerance = is_array($firstNonWhite) ? ((int)($firstNonWhite['column'] ?? 1) + 3) : 4;
289+
$tolerance = is_array($firstNonWhite) ? ((int) ($firstNonWhite['column'] ?? 1) + 3) : 4;
285290

286291
return (strlen($word) + $tolerance) > $this->lineLimit;
287292
}
@@ -320,7 +325,7 @@ private function isLongI10nFunction(File $file, array $tokens, int $start, int $
320325
return false;
321326
}
322327

323-
$function = strtolower((string)$tokens[$functionPos]['content']);
328+
$function = strtolower((string) $tokens[$functionPos]['content']);
324329
if (!in_array($function, self::I18N_FUNCTIONS, true)) {
325330
return false;
326331
}
@@ -333,7 +338,7 @@ private function isLongI10nFunction(File $file, array $tokens, int $start, int $
333338
if ($tokens[$i]['line'] !== $targetLine) {
334339
continue;
335340
}
336-
$textLen += max(1, strlen((string)$tokens[$i]['content']));
341+
$textLen += max(1, strlen((string) $tokens[$i]['content']));
337342
}
338343

339344
return ($textLen + 2) > $this->lineLimit;
@@ -360,7 +365,7 @@ private function isLongUse(File $file, array $tokens, int $start, int $end): boo
360365
$endUse = $file->findEndOfStatement($usePos);
361366
$useLen = 0;
362367
for ($i = $usePos; $i <= $endUse; $i++) {
363-
$useLen += strlen((string)$tokens[$i]['content']);
368+
$useLen += strlen((string) $tokens[$i]['content']);
364369
}
365370

366371
return $useLen > $this->lineLimit;

Inpsyde/Sniffs/CodeQuality/NestingLevelSniff.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public function process(File $phpcsFile, $stackPtr): void
6464
return;
6565
}
6666

67-
$start = (int)$tokens[$stackPtr]['scope_opener'];
68-
$end = (int)$tokens[$stackPtr]['scope_closer'];
67+
$start = (int) $tokens[$stackPtr]['scope_opener'];
68+
$end = (int) $tokens[$stackPtr]['scope_closer'];
6969

70-
$baseLevel = (int)$tokens[$stackPtr]['level'];
70+
$baseLevel = (int) $tokens[$stackPtr]['level'];
7171
$nestingLevel = 0;
7272
$inTry = false;
7373
$endTry = null;
@@ -82,7 +82,7 @@ public function process(File $phpcsFile, $stackPtr): void
8282
continue;
8383
}
8484

85-
$level = (int)$tokens[$i]['level'];
85+
$level = (int) $tokens[$i]['level'];
8686

8787
if (!$inTry && $tokens[$i]['code'] === T_TRY && $level === $tryTargetLevel) {
8888
$inTry = true;
@@ -149,14 +149,14 @@ private function endOfTryBlock(int $catchPosition, File $phpcsFile): int
149149
{
150150
/** @var array<int, array<string, mixed>> $tokens */
151151
$tokens = $phpcsFile->getTokens();
152-
$currentEnd = (int)$tokens[$catchPosition]['scope_closer'];
152+
$currentEnd = (int) $tokens[$catchPosition]['scope_closer'];
153153
$nextCatch = $phpcsFile->findNext(T_CATCH, $currentEnd + 1, $currentEnd + 3);
154154
if ($nextCatch) {
155155
return $this->endOfTryBlock($nextCatch, $phpcsFile);
156156
}
157157

158158
$finally = $phpcsFile->findNext(T_FINALLY, $currentEnd + 1, $currentEnd + 3);
159159

160-
return $finally ? (int)$tokens[$finally]['scope_closer'] + 1 : $currentEnd + 1;
160+
return $finally ? (int) $tokens[$finally]['scope_closer'] + 1 : $currentEnd + 1;
161161
}
162162
}

Inpsyde/Sniffs/CodeQuality/Psr4Sniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private function checkPsr4(
128128
$fullyQualifiedName = $namespace . "\\{$className}";
129129

130130
foreach ($this->exclude as $excluded) {
131-
if (strpos($fullyQualifiedName, (string)$excluded) === 0) {
131+
if (strpos($fullyQualifiedName, (string) $excluded) === 0) {
132132
return;
133133
}
134134
}

Inpsyde/Sniffs/CodeQuality/StaticClosureSniff.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function process(File $phpcsFile, $stackPtr): void
7171
$tokens = $phpcsFile->getTokens();
7272
while (!$thisFound && ($i < $functionEnd)) {
7373
$token = $tokens[$i];
74-
$content = (string)($token['content'] ?? '');
74+
$content = (string) ($token['content'] ?? '');
7575
$thisFound = (($token['code'] === T_VARIABLE) && ($content === '$this'))
7676
|| (
7777
in_array($token['code'], [T_DOUBLE_QUOTED_STRING, T_HEREDOC], true)
@@ -96,7 +96,7 @@ public function process(File $phpcsFile, $stackPtr): void
9696
}
9797
}
9898

99-
$line = (int)$tokens[$stackPtr]['line'];
99+
$line = (int) $tokens[$stackPtr]['line'];
100100
$message = sprintf('Closure found at line %d could be static.', $line);
101101

102102
if ($phpcsFile->addFixableWarning($message, $stackPtr, 'PossiblyStaticClosure')) {

Inpsyde/Sniffs/CodeQuality/VariablesNameSniff.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function process(File $phpcsFile, $stackPtr): void
9898

9999
/** @var array<int, array<string, mixed>> $tokens */
100100
$tokens = $phpcsFile->getTokens();
101-
$name = (string)$tokens[$stackPtr]['content'];
101+
$name = (string) $tokens[$stackPtr]['content'];
102102

103103
if (
104104
in_array($name, $ignored, true)
@@ -152,15 +152,15 @@ private function checkType(): string
152152
*/
153153
private function arePropertiesIgnored(): bool
154154
{
155-
return (bool)filter_var($this->ignoreProperties, FILTER_VALIDATE_BOOLEAN);
155+
return (bool) filter_var($this->ignoreProperties, FILTER_VALIDATE_BOOLEAN);
156156
}
157157

158158
/**
159159
* @return bool
160160
*/
161161
private function areVariablesIgnored(): bool
162162
{
163-
return (bool)filter_var($this->ignoreLocalVars, FILTER_VALIDATE_BOOLEAN);
163+
return (bool) filter_var($this->ignoreLocalVars, FILTER_VALIDATE_BOOLEAN);
164164
}
165165

166166
/**
@@ -179,7 +179,7 @@ private function checkCamelCase(string $name): bool
179179
*/
180180
private function checkSnakeCase(string $name): bool
181181
{
182-
return (bool)preg_match('~^\$[a-z]+(?:[a-z0-9_]+)?$~', $name);
182+
return (bool) preg_match('~^\$[a-z]+(?:[a-z0-9_]+)?$~', $name);
183183
}
184184

185185
/**

Inpsyde/ruleset.xml

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
</rule>
145145
<rule ref="Generic.CodeAnalysis.AssignmentInCondition"/>
146146
<rule ref="Generic.CodeAnalysis.EmptyPHPStatement"/>
147+
<rule ref="Generic.Formatting.SpaceAfterCast"/>
147148
<rule ref="Generic.Metrics.CyclomaticComplexity">
148149
<properties>
149150
<property name="absoluteComplexity" value="50"/>

0 commit comments

Comments
 (0)