Skip to content

Commit

Permalink
Fix more Psalm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Feb 28, 2024
1 parent 2a0ee12 commit b53f4bf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Inpsyde/Sniffs/CodeQuality/FunctionLengthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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} = filter_var($this->{$flag}, FILTER_VALIDATE_BOOLEAN);
}
}
}
Expand Down
43 changes: 28 additions & 15 deletions Inpsyde/Sniffs/CodeQuality/LineLengthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ private function collectLongLinesData(File $file, int $start): array
$lastLine = null;
for ($i = $start; $i < $file->numTokens; $i++) {
// Still processing previous line: increment length and continue.
if ($lastLine && ($tokens[$i]['line'] === $lastLine)) {
if (($lastLine !== null) && ($lastLine > 0) && ($tokens[$i]['line'] === $lastLine)) {
$content = (string) $tokens[$i]['content'];
$data[$lastLine]['length'] += strlen($content);
$data[$lastLine]['nonEmptyLength'] += strlen(trim($content));
continue;
}

// A new line started: let's set "end" for the previous line (if this isn't 1st line)
if ($lastLine && isset($data[$lastLine])) {
if (($lastLine !== null) && ($lastLine > 0) && isset($data[$lastLine])) {
$data[$lastLine]['end'] = $i - 1;
}

Expand All @@ -135,32 +135,45 @@ private function collectLongLinesData(File $file, int $start): array
}

// We still have to set the "end" for last file line.
if ($lastLine && ($data[$lastLine]['end'] === null)) {
if (($lastLine !== null) && ($lastLine > 0) && ($data[$lastLine]['end'] === null)) {
/** @var int $lastLine */
$data[$lastLine]['end'] = $i - 1;
}

$longLines = [];
/**
* @var int $lineNumber
* @var array{length:int, nonEmptyLength:int, start:int, end:int|null} $lineData
*/
foreach ($data as $lineNumber => $lineData) {
$lineEnd = $lineData['end'] ?? $lineData['start'];
if (
(($lineData['length'] - $this->lineLimit) <= 1) // 1 char of tolerance
|| ($lineData['nonEmptyLength'] === 0) // ignore empty lines
|| $this->isLongUse($file, $tokens, $lineData['start'], $lineEnd)
|| $this->isLongI10nFunction($file, $tokens, $lineData['start'], $lineEnd)
|| $this->isLongWord($file, $tokens, $lineData['start'], $lineEnd)
) {
continue;
if (!$this->isLengthAcceptable($lineData, $file, $tokens)) {
$longLines[$lineNumber] = [$lineData['length'], $lineData['start']];
}

$longLines[$lineNumber] = [$lineData['length'], $lineData['start']];
}

return $longLines;
}

/**
* @param array{length:int, nonEmptyLength:int, start:int, end:int|null} $lineData
* @param File $file
* @param array<int, array<string, mixed>> $tokens
* @return bool
*/
private function isLengthAcceptable(array $lineData, File $file, array $tokens): bool
{
$lineEnd = $lineData['end'] ?? $lineData['start'];

return (($lineData['length'] - $this->lineLimit) <= 1) // 1 char of tolerance
|| ($lineData['nonEmptyLength'] === 0) // ignore empty lines
|| $this->isLongUse($file, $tokens, $lineData['start'], $lineEnd)
|| $this->isLongI10nFunction($file, $tokens, $lineData['start'], $lineEnd)
|| $this->isLongWord($file, $tokens, $lineData['start'], $lineEnd);
}

/**
* We don't want to split a single word in multiple lines.
* So if there's a long word (e.g. an URL) that alone is above max line length, we don't show
* So if there's a long word (e.g. a URL) that alone is above max line length, we don't show
* warnings for it.
*
* @param File $file
Expand Down
6 changes: 4 additions & 2 deletions Inpsyde/Sniffs/CodeQuality/NestingLevelSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,14 @@ private function endOfTryBlock(int $catchPosition, File $phpcsFile): int
$tokens = $phpcsFile->getTokens();
$currentEnd = (int) $tokens[$catchPosition]['scope_closer'];
$nextCatch = $phpcsFile->findNext(T_CATCH, $currentEnd + 1, $currentEnd + 3);
if ($nextCatch) {
if ($nextCatch !== false) {
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 !== false)
? (int) $tokens[$finally]['scope_closer'] + 1
: $currentEnd + 1;
}
}
4 changes: 2 additions & 2 deletions Inpsyde/Sniffs/CodeQuality/VariablesNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ private function checkType(): string
*/
private function arePropertiesIgnored(): bool
{
return (bool) filter_var($this->ignoreProperties, FILTER_VALIDATE_BOOLEAN);
return filter_var($this->ignoreProperties, FILTER_VALIDATE_BOOLEAN);
}

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

/**
Expand Down

0 comments on commit b53f4bf

Please sign in to comment.