Skip to content

Commit

Permalink
Fix all the "SpaceAfterCast" errors
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioeatgoat committed Nov 17, 2023
1 parent 4cc6896 commit 472ccff
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 51 deletions.
6 changes: 3 additions & 3 deletions Inpsyde/Helpers/Boundaries.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static function arrayBoundaries(File $file, int $position): array
return [-1, -1];
}

return [(int)$openClose['opener'], (int)$openClose['closer']];
return [(int) $openClose['opener'], (int) $openClose['closer']];
}

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

$start = (int)($token['scope_opener'] ?? 0);
$end = (int)($token['scope_closer'] ?? 0);
$start = (int) ($token['scope_opener'] ?? 0);
$end = (int) ($token['scope_closer'] ?? 0);
if (($start <= 0) || ($end <= 0) || ($start >= ($end - 1))) {
return [-1, -1];
}
Expand Down
8 changes: 4 additions & 4 deletions Inpsyde/Helpers/FunctionDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ public static function allTags(
return [];
}

$functionLine = (int)($tokens[$position]['line'] ?? -1);
$closeLine = (int)($tokens[$closeTag]['line'] ?? -1);
$functionLine = (int) ($tokens[$position]['line'] ?? -1);
$closeLine = (int) ($tokens[$closeTag]['line'] ?? -1);
if ($closeLine !== ($functionLine - 1)) {
return [];
}

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

Expand All @@ -84,7 +84,7 @@ public static function allTags(
continue;
}

$content = (string)$tokens[$i]['content'];
$content = (string) $tokens[$i]['content'];
if (($tokens[$i]['code'] === T_DOC_COMMENT_TAG)) {
$inTag = true;
$key++;
Expand Down
2 changes: 1 addition & 1 deletion Inpsyde/Helpers/Misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static function tokensSubsetToString(

$content = '';
foreach ($filtered as $token) {
$content .= (string)($token['content'] ?? '');
$content .= (string) ($token['content'] ?? '');
}

return $content;
Expand Down
4 changes: 2 additions & 2 deletions Inpsyde/Helpers/Names.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function nameableTokenName(File $file, int $position): ?string
}

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

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

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

return ($name === '') ? null : $name;
}
Expand Down
2 changes: 1 addition & 1 deletion Inpsyde/Helpers/WpHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ public static function isHookClosure(
*/
public static function isHookFunction(File $file, int $position): bool
{
return (bool)FunctionDocBlock::tag('@wp-hook', $file, $position);
return (bool) FunctionDocBlock::tag('@wp-hook', $file, $position);
}
}
4 changes: 2 additions & 2 deletions Inpsyde/Sniffs/CodeQuality/ForbiddenPublicPropertySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function isSniffClass(File $file, int $position): bool
{
$classNameTokenPosition = $file->findNext(
T_STRING,
(int)$file->findPrevious(T_CLASS, $position)
(int) $file->findPrevious(T_CLASS, $position)
);

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

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

Expand Down
12 changes: 6 additions & 6 deletions Inpsyde/Sniffs/CodeQuality/FunctionBodyStartSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function process(File $phpcsFile, $stackPtr): void
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr] ?? [];

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

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

[$code, $message, $expectedLine] = $this->checkBodyStart(
$bodyStart,
(int)($tokens[$scopeOpener]['line'] ?? -1),
(int)($token['line'] ?? -1),
(int) ($tokens[$scopeOpener]['line'] ?? -1),
(int) ($token['line'] ?? -1),
$phpcsFile
);

Expand Down Expand Up @@ -107,7 +107,7 @@ private function checkBodyStart(

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

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

if ($currentLine === $expectedLine) {
return;
Expand Down
14 changes: 7 additions & 7 deletions Inpsyde/Sniffs/CodeQuality/FunctionLengthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ private function structureLinesCount(File $file, int $position): int
return 0;
}

$start = (int)$token['scope_opener'];
$end = (int)$token['scope_closer'];
$length = (int)$tokens[$end]['line'] - (int)$tokens[$start]['line'];
$start = (int) $token['scope_opener'];
$end = (int) $token['scope_closer'];
$length = (int) $tokens[$end]['line'] - (int) $tokens[$start]['line'];

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

$toExcludeCount = (int)array_sum($docblocks);
$toExcludeCount = (int) array_sum($docblocks);
if ($this->ignoreBlankLines) {
$toExcludeCount += count($empty);
}
Expand All @@ -156,7 +156,7 @@ private function collectLinesToExclude(int $start, int $end, array $tokens): int
*/
private function ignoredLinesData(array $token, array $lines): array
{
$line = (int)$token['line'];
$line = (int) $token['line'];
if (!array_key_exists($line, $lines)) {
$lines[$line] = ['empty' => true, 'only-comment' => true];
}
Expand Down Expand Up @@ -189,7 +189,7 @@ private function docBlocksData(array $tokens, int $position, array $docBlocks):

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

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

foreach ($flags as $flag) {
if (is_string($this->{$flag})) {
$this->{$flag} = (bool)filter_var($this->{$flag}, FILTER_VALIDATE_BOOLEAN);
$this->{$flag} = (bool) filter_var($this->{$flag}, FILTER_VALIDATE_BOOLEAN);
}
}
}
Expand Down
23 changes: 14 additions & 9 deletions Inpsyde/Sniffs/CodeQuality/LineLengthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private function collectLongLinesData(File $file, int $start): array
for ($i = $start; $i < $file->numTokens; $i++) {
// Still processing previous line: increment length and continue.
if ($lastLine && ($tokens[$i]['line'] === $lastLine)) {
$content = (string)$tokens[$i]['content'];
$content = (string) $tokens[$i]['content'];
$data[$lastLine]['length'] += strlen($content);
$data[$lastLine]['nonEmptyLength'] += strlen(trim($content));
continue;
Expand All @@ -124,8 +124,8 @@ private function collectLongLinesData(File $file, int $start): array
$data[$lastLine]['end'] = $i - 1;
}

$lastLine = (int)$tokens[$i]['line'];
$content = (string)$tokens[$i]['content'];
$lastLine = (int) $tokens[$i]['line'];
$content = (string) $tokens[$i]['content'];
$data[$lastLine] = [
'length' => strlen($content),
'nonEmptyLength' => strlen(trim($content)),
Expand Down Expand Up @@ -228,7 +228,7 @@ private function isLongHtmlAttribute(
$inPhp = true;
}
if ($tokens[$i]['code'] === T_INLINE_HTML || $inPhp) {
$tokenContent = (string)$tokens[$i]['content'];
$tokenContent = (string) $tokens[$i]['content'];
$content .= $inPhp ? str_repeat('x', strlen($tokenContent)) : $tokenContent;
}
if ($tokens[$i]['code'] === T_CLOSE_TAG && $inPhp) {
Expand Down Expand Up @@ -271,7 +271,12 @@ private function isLongSingleWord(
array $tokens
): bool {

$words = preg_split('~\s+~', (string)$tokens[$position]['content'], 2, PREG_SPLIT_NO_EMPTY);
$words = preg_split(
'~\s+~',
(string) $tokens[$position]['content'],
2,
PREG_SPLIT_NO_EMPTY
);

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

return (strlen($word) + $tolerance) > $this->lineLimit;
}
Expand Down Expand Up @@ -320,7 +325,7 @@ private function isLongI10nFunction(File $file, array $tokens, int $start, int $
return false;
}

$function = strtolower((string)$tokens[$functionPos]['content']);
$function = strtolower((string) $tokens[$functionPos]['content']);
if (!in_array($function, self::I18N_FUNCTIONS, true)) {
return false;
}
Expand All @@ -333,7 +338,7 @@ private function isLongI10nFunction(File $file, array $tokens, int $start, int $
if ($tokens[$i]['line'] !== $targetLine) {
continue;
}
$textLen += max(1, strlen((string)$tokens[$i]['content']));
$textLen += max(1, strlen((string) $tokens[$i]['content']));
}

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

return $useLen > $this->lineLimit;
Expand Down
12 changes: 6 additions & 6 deletions Inpsyde/Sniffs/CodeQuality/NestingLevelSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function process(File $phpcsFile, $stackPtr): void
return;
}

$start = (int)$tokens[$stackPtr]['scope_opener'];
$end = (int)$tokens[$stackPtr]['scope_closer'];
$start = (int) $tokens[$stackPtr]['scope_opener'];
$end = (int) $tokens[$stackPtr]['scope_closer'];

$baseLevel = (int)$tokens[$stackPtr]['level'];
$baseLevel = (int) $tokens[$stackPtr]['level'];
$nestingLevel = 0;
$inTry = false;
$endTry = null;
Expand All @@ -82,7 +82,7 @@ public function process(File $phpcsFile, $stackPtr): void
continue;
}

$level = (int)$tokens[$i]['level'];
$level = (int) $tokens[$i]['level'];

if (!$inTry && $tokens[$i]['code'] === T_TRY && $level === $tryTargetLevel) {
$inTry = true;
Expand Down Expand Up @@ -149,14 +149,14 @@ private function endOfTryBlock(int $catchPosition, File $phpcsFile): int
{
/** @var array<int, array<string, mixed>> $tokens */
$tokens = $phpcsFile->getTokens();
$currentEnd = (int)$tokens[$catchPosition]['scope_closer'];
$currentEnd = (int) $tokens[$catchPosition]['scope_closer'];
$nextCatch = $phpcsFile->findNext(T_CATCH, $currentEnd + 1, $currentEnd + 3);
if ($nextCatch) {
return $this->endOfTryBlock($nextCatch, $phpcsFile);
}

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

return $finally ? (int)$tokens[$finally]['scope_closer'] + 1 : $currentEnd + 1;
return $finally ? (int) $tokens[$finally]['scope_closer'] + 1 : $currentEnd + 1;
}
}
2 changes: 1 addition & 1 deletion Inpsyde/Sniffs/CodeQuality/Psr4Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function checkPsr4(
$fullyQualifiedName = $namespace . "\\{$className}";

foreach ($this->exclude as $excluded) {
if (strpos($fullyQualifiedName, (string)$excluded) === 0) {
if (strpos($fullyQualifiedName, (string) $excluded) === 0) {
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Inpsyde/Sniffs/CodeQuality/StaticClosureSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function process(File $phpcsFile, $stackPtr): void
$tokens = $phpcsFile->getTokens();
while (!$thisFound && ($i < $functionEnd)) {
$token = $tokens[$i];
$content = (string)($token['content'] ?? '');
$content = (string) ($token['content'] ?? '');
$thisFound = (($token['code'] === T_VARIABLE) && ($content === '$this'))
|| (
in_array($token['code'], [T_DOUBLE_QUOTED_STRING, T_HEREDOC], true)
Expand All @@ -96,7 +96,7 @@ public function process(File $phpcsFile, $stackPtr): void
}
}

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

if ($phpcsFile->addFixableWarning($message, $stackPtr, 'PossiblyStaticClosure')) {
Expand Down
8 changes: 4 additions & 4 deletions Inpsyde/Sniffs/CodeQuality/VariablesNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function process(File $phpcsFile, $stackPtr): void

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

if (
in_array($name, $ignored, true)
Expand Down Expand Up @@ -152,15 +152,15 @@ private function checkType(): string
*/
private function arePropertiesIgnored(): bool
{
return (bool)filter_var($this->ignoreProperties, FILTER_VALIDATE_BOOLEAN);
return (bool) filter_var($this->ignoreProperties, FILTER_VALIDATE_BOOLEAN);
}

/**
* @return bool
*/
private function areVariablesIgnored(): bool
{
return (bool)filter_var($this->ignoreLocalVars, FILTER_VALIDATE_BOOLEAN);
return (bool) filter_var($this->ignoreLocalVars, FILTER_VALIDATE_BOOLEAN);
}

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

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/src/FixtureContentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ public function parse(string $fixturePath): array
throw new \Error("Fixture file {$fixturePath} is not readable.");
}

$accumulator = (object)[
$accumulator = (object) [
'sniff' => null,
'warnings' => [],
'errors' => [],
'messages' => [],
'properties' => (object)[
'properties' => (object) [
'start' => false,
'end' => false,
'values' => [],
],
'process' => (object)[
'process' => (object) [
'start' => false,
'end' => false,
'content' => '',
Expand Down

0 comments on commit 472ccff

Please sign in to comment.