Skip to content

Commit b53f4bf

Browse files
committed
Fix more Psalm issues
1 parent 2a0ee12 commit b53f4bf

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

Diff for: Inpsyde/Sniffs/CodeQuality/FunctionLengthSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -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} = filter_var($this->{$flag}, FILTER_VALIDATE_BOOLEAN);
212212
}
213213
}
214214
}

Diff for: Inpsyde/Sniffs/CodeQuality/LineLengthSniff.php

+28-15
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ private function collectLongLinesData(File $file, int $start): array
112112
$lastLine = null;
113113
for ($i = $start; $i < $file->numTokens; $i++) {
114114
// Still processing previous line: increment length and continue.
115-
if ($lastLine && ($tokens[$i]['line'] === $lastLine)) {
115+
if (($lastLine !== null) && ($lastLine > 0) && ($tokens[$i]['line'] === $lastLine)) {
116116
$content = (string) $tokens[$i]['content'];
117117
$data[$lastLine]['length'] += strlen($content);
118118
$data[$lastLine]['nonEmptyLength'] += strlen(trim($content));
119119
continue;
120120
}
121121

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

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

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

142143
$longLines = [];
144+
/**
145+
* @var int $lineNumber
146+
* @var array{length:int, nonEmptyLength:int, start:int, end:int|null} $lineData
147+
*/
143148
foreach ($data as $lineNumber => $lineData) {
144-
$lineEnd = $lineData['end'] ?? $lineData['start'];
145-
if (
146-
(($lineData['length'] - $this->lineLimit) <= 1) // 1 char of tolerance
147-
|| ($lineData['nonEmptyLength'] === 0) // ignore empty lines
148-
|| $this->isLongUse($file, $tokens, $lineData['start'], $lineEnd)
149-
|| $this->isLongI10nFunction($file, $tokens, $lineData['start'], $lineEnd)
150-
|| $this->isLongWord($file, $tokens, $lineData['start'], $lineEnd)
151-
) {
152-
continue;
149+
if (!$this->isLengthAcceptable($lineData, $file, $tokens)) {
150+
$longLines[$lineNumber] = [$lineData['length'], $lineData['start']];
153151
}
154-
155-
$longLines[$lineNumber] = [$lineData['length'], $lineData['start']];
156152
}
157153

158154
return $longLines;
159155
}
160156

157+
/**
158+
* @param array{length:int, nonEmptyLength:int, start:int, end:int|null} $lineData
159+
* @param File $file
160+
* @param array<int, array<string, mixed>> $tokens
161+
* @return bool
162+
*/
163+
private function isLengthAcceptable(array $lineData, File $file, array $tokens): bool
164+
{
165+
$lineEnd = $lineData['end'] ?? $lineData['start'];
166+
167+
return (($lineData['length'] - $this->lineLimit) <= 1) // 1 char of tolerance
168+
|| ($lineData['nonEmptyLength'] === 0) // ignore empty lines
169+
|| $this->isLongUse($file, $tokens, $lineData['start'], $lineEnd)
170+
|| $this->isLongI10nFunction($file, $tokens, $lineData['start'], $lineEnd)
171+
|| $this->isLongWord($file, $tokens, $lineData['start'], $lineEnd);
172+
}
173+
161174
/**
162175
* We don't want to split a single word in multiple lines.
163-
* So if there's a long word (e.g. an URL) that alone is above max line length, we don't show
176+
* So if there's a long word (e.g. a URL) that alone is above max line length, we don't show
164177
* warnings for it.
165178
*
166179
* @param File $file

Diff for: Inpsyde/Sniffs/CodeQuality/NestingLevelSniff.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,14 @@ private function endOfTryBlock(int $catchPosition, File $phpcsFile): int
151151
$tokens = $phpcsFile->getTokens();
152152
$currentEnd = (int) $tokens[$catchPosition]['scope_closer'];
153153
$nextCatch = $phpcsFile->findNext(T_CATCH, $currentEnd + 1, $currentEnd + 3);
154-
if ($nextCatch) {
154+
if ($nextCatch !== false) {
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 !== false)
161+
? (int) $tokens[$finally]['scope_closer'] + 1
162+
: $currentEnd + 1;
161163
}
162164
}

Diff for: Inpsyde/Sniffs/CodeQuality/VariablesNameSniff.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -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 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 filter_var($this->ignoreLocalVars, FILTER_VALIDATE_BOOLEAN);
164164
}
165165

166166
/**

0 commit comments

Comments
 (0)