Skip to content

Commit

Permalink
Fix Psalm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Feb 25, 2024
1 parent a61ffed commit 1865191
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Inpsyde/Helpers/Boundaries.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static function startEnd(File $file, int $position): array
$token = $file->getTokens()[$position] ?? [];
if (($token['code'] ?? '') === T_FN) {
$start = $file->findNext(T_FN_ARROW, $position + 1, null, false, null, true);
if (!$start) {
if ($start === false) {
return [-1, -1];
}

Expand Down
14 changes: 7 additions & 7 deletions Inpsyde/Helpers/FunctionDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function allTags(
$closeType = T_DOC_COMMENT_CLOSE_TAG;
$closeTag = $file->findPrevious($closeType, $position - 1, null, false, null, true);

if (!$closeTag || empty($tokens[$closeTag]['comment_opener'])) {
if (($closeTag === false) || !isset($tokens[$closeTag]['comment_opener'])) {
return [];
}

Expand All @@ -74,7 +74,7 @@ public static function allTags(

/** @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 @@ -101,7 +101,7 @@ public static function allTags(
static $rand;
$rand or $rand = bin2hex(random_bytes(3));
foreach ($tags as [$tagName, $tagContent]) {
empty($normalizedTags[$tagName]) and $normalizedTags[$tagName] = [];
isset($normalizedTags[$tagName]) or $normalizedTags[$tagName] = [];
if (!$normalizeContent) {
$normalizedTags[$tagName][] = $tagContent;
continue;
Expand Down Expand Up @@ -147,7 +147,7 @@ public static function allParamTypes(File $file, int $functionPosition): array
$types = [];
foreach ($params as $param) {
preg_match('~^([^$]+)\s*(\$\S+)~', trim($param), $matches);
if (($matches[1] ?? null) && ($matches[2] ?? null)) {
if (isset($matches[1]) && isset($matches[2])) {
$types[$matches[2]] = static::normalizeTypesString($matches[1]);
}
}
Expand All @@ -170,10 +170,10 @@ public static function normalizeTypesString(string $typesString): array
if (strpos($splitType, '&') !== false) {
$splitType = rtrim(ltrim($splitType, '('), ')');
} elseif (strpos($splitType, '?') === 0) {
$splitType = substr($splitType, 1) ?: '';
$hasNull = $hasNull || ($splitType !== '');
$splitType = substr($splitType, 1);
$hasNull = $hasNull || (($splitType !== '') && ($splitType !== false));
}
if (!$splitType) {
if (($splitType === false) || ($splitType === '')) {
continue;
}
if (strtolower($splitType) === 'null') {
Expand Down
2 changes: 1 addition & 1 deletion Inpsyde/Helpers/FunctionReturnStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static function isNull(File $file, int $position): bool

if ($code === T_FN) {
$position = $file->findNext(T_FN_ARROW, $position + 1, null, false, null, true);
if (!$position) {
if ($position === false) {
return false;
}
}
Expand Down
15 changes: 10 additions & 5 deletions Inpsyde/Helpers/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,33 @@ public static function looksLikeFunctionCall(File $file, int $position): bool
}

$callOpen = $file->findNext(Tokens::$emptyTokens, $position + 1, null, true, null, true);
if (!$callOpen || $tokens[$callOpen]['code'] !== T_OPEN_PARENTHESIS) {
if (($callOpen === false) || $tokens[$callOpen]['code'] !== T_OPEN_PARENTHESIS) {
return false;
}

$prevExclude = Tokens::$emptyTokens;
$prevMeaningful = $file->findPrevious($prevExclude, $position - 1, null, true, null, true);

if ($prevMeaningful && ($tokens[$prevMeaningful]['code'] ?? -1) === T_NS_SEPARATOR) {
if (
($prevMeaningful !== false)
&& ($tokens[$prevMeaningful]['code'] ?? -1) === T_NS_SEPARATOR
) {
$prevExclude = array_merge($prevExclude, [T_STRING, T_NS_SEPARATOR]);
$prevStart = $prevMeaningful - 1;
$prevMeaningful = $file->findPrevious($prevExclude, $prevStart, null, true, null, true);
}

$prevMeaningfulCode = $prevMeaningful ? $tokens[$prevMeaningful]['code'] : null;
if ($prevMeaningfulCode && in_array($prevMeaningfulCode, [T_NEW, T_FUNCTION], true)) {
$prevMeaningfulCode = ($prevMeaningful !== false)
? $tokens[$prevMeaningful]['code']
: null;
if (in_array($prevMeaningfulCode, [T_NEW, T_FUNCTION], true)) {
return false;
}

$callClose = $file->findNext([T_CLOSE_PARENTHESIS], $callOpen + 1, null, false, null, true);
$expectedCallClose = $tokens[$callOpen]['parenthesis_closer'] ?? -1;

return $callClose && ($callClose === $expectedCallClose);
return ($callClose !== false) && ($callClose === $expectedCallClose);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Inpsyde/Helpers/Misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ final class Misc
*/
public static function minPhpTestVersion(): string
{
$testVersion = trim(Config::getConfigData('testVersion') ?: '');
if (!$testVersion) {
$testVersion = trim(Config::getConfigData('testVersion') ?? '');
if ($testVersion === '') {
return self::MIN_SUPPORTED_VERSION;
}

Expand Down
9 changes: 6 additions & 3 deletions Inpsyde/Helpers/Names.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ public static function nameableTokenName(File $file, int $position): ?string
}

if ($code === T_NAMESPACE) {
return Namespaces::isDeclaration($file, $position)
? (Namespaces::getDeclaredName($file, $position) ?: '')
: null;
if (!Namespaces::isDeclaration($file, $position)) {
return null;
}
$declaredName = Namespaces::getDeclaredName($file, $position);

return ($declaredName !== '' && is_string($declaredName)) ? $declaredName : null;
}

$namePosition = $file->findNext(T_STRING, $position, null, false, null, true);
Expand Down
13 changes: 7 additions & 6 deletions Inpsyde/Helpers/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ public static function findAllImportUses(File $file, int $position): array
{
// phpcs:enable Generic.Metrics.CyclomaticComplexity
$usePositions = [];
$nextUse = $file->findPrevious(T_NAMESPACE, $position - 1) ?: 0;
$nextUse = $file->findPrevious(T_NAMESPACE, $position - 1);
($nextUse === false) and $nextUse = 0;

while (true) {
$nextUse = $file->findNext(T_USE, $nextUse + 1, $position - 1);
if (!$nextUse) {
if ($nextUse === false) {
break;
}
if (!UseStatements::isImportUse($file, $nextUse)) {
Expand All @@ -121,14 +122,14 @@ public static function findAllImportUses(File $file, int $position): array
$asPos = $file->findNext(T_AS, $usePosition + 1, $end, false, null, true);
$useName = Misc::tokensSubsetToString(
$usePosition + 1,
($asPos ?: $end) - 1,
(($asPos !== false) ? $asPos : $end) - 1,
$file,
[T_STRING, T_NS_SEPARATOR]
);
$useName = trim($useName, '\\');
$useNameParts = explode('\\', $useName);
$key = end($useNameParts);
if ($asPos) {
if ($asPos !== false) {
$keyPos = $file->findNext(T_STRING, $asPos + 1, null, false, null, true);
/** @var string $key */
$key = $tokens[$keyPos]['content'] ?? '';
Expand All @@ -153,7 +154,7 @@ public static function allInterfacesFullyQualifiedNames(File $file, int $positio
}

$implementsPos = $file->findNext(T_IMPLEMENTS, $position, null, false, null, true);
if (!$implementsPos) {
if ($implementsPos === false) {
return null;
}

Expand All @@ -166,7 +167,7 @@ public static function allInterfacesFullyQualifiedNames(File $file, int $positio
true
);

if (!$namesEnd) {
if ($namesEnd === false) {
return null;
}

Expand Down
6 changes: 3 additions & 3 deletions Inpsyde/Helpers/WpHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ public static function isHookClosure(
$exclude = $empty;
$exclude[] = T_STATIC;
$commaPos = $file->findPrevious($exclude, $position - 1, null, true, null, true);
if (!$commaPos || ($tokens[$commaPos]['code'] ?? '') !== T_COMMA) {
if (($commaPos === false) || ($tokens[$commaPos]['code'] ?? '') !== T_COMMA) {
return false;
}

$openType = [T_OPEN_PARENTHESIS];
$openCallPos = $file->findPrevious($openType, $commaPos - 2, null, false, null, true);
if (!$openCallPos) {
if ($openCallPos === false) {
return false;
}

$functionCallPos = $file->findPrevious($empty, $openCallPos - 1, null, true, null, true);
if (!$functionCallPos || $tokens[$functionCallPos]['code'] !== T_STRING) {
if (($functionCallPos === false) || $tokens[$functionCallPos]['code'] !== T_STRING) {
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion Inpsyde/Sniffs/CodeQuality/ArgumentTypeDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public function process(File $phpcsFile, $stackPtr): void

$errors = [];
foreach ($parameters as $parameter) {
if ($parameter['type_hint'] ?? null) {
$typeHint = $parameter['type_hint'] ?? '';
if (($typeHint !== '') && ($typeHint !== false)) {
continue;
}

Expand Down
8 changes: 4 additions & 4 deletions Inpsyde/Sniffs/CodeQuality/FunctionBodyStartSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function process(File $phpcsFile, $stackPtr): void

$bodyStart = $phpcsFile->findNext([T_WHITESPACE], $scopeOpener + 1, null, true);
if (
!$bodyStart
($bodyStart === false)
|| !array_key_exists($bodyStart, $tokens)
|| $bodyStart <= $scopeOpener
|| $bodyStart >= $scopeCloser
Expand All @@ -82,9 +82,9 @@ public function process(File $phpcsFile, $stackPtr): void
);

if (
$code
&& $message
&& $expectedLine
(($code !== null) && ($code !== ''))
&& (($message !== null) && ($message !== ''))
&& ($expectedLine !== null)
&& $phpcsFile->addFixableWarning($message, $stackPtr, $code)
) {
$this->fix($bodyStart, $expectedLine, $scopeOpener, $phpcsFile);
Expand Down
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
10 changes: 5 additions & 5 deletions Inpsyde/Sniffs/CodeQuality/LineLengthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,20 @@ 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) && ($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) && isset($data[$lastLine])) {
$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 All @@ -135,7 +135,7 @@ 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) && ($data[$lastLine]['end'] === null)) {
$data[$lastLine]['end'] = $i - 1;
}

Expand Down
8 changes: 5 additions & 3 deletions Inpsyde/Sniffs/CodeQuality/NestingLevelSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,16 @@ 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) {
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;
}
}
12 changes: 6 additions & 6 deletions Inpsyde/Sniffs/CodeQuality/NoAccessorsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public function process(File $phpcsFile, $stackPtr): void
return;
}

$functionName = $phpcsFile->getDeclarationName($stackPtr);
if (!$functionName || in_array($functionName, self::ALLOWED_NAMES, true)) {
$functionName = $phpcsFile->getDeclarationName($stackPtr) ?? '';
if (($functionName === '') || in_array($functionName, self::ALLOWED_NAMES, true)) {
return;
}

Expand All @@ -91,16 +91,16 @@ public function process(File $phpcsFile, $stackPtr): void
$tokens = $phpcsFile->getTokens();
$modifierPointer = $tokens[$modifierPointerPosition] ?? null;
if (
$modifierPointer
is_array($modifierPointer)
&& !in_array($modifierPointer['code'], Tokens::$scopeModifiers, true)
) {
$modifierPointer = null;
}

$modifier = $modifierPointer ? $modifierPointer['code'] ?? null : null;
$modifier = ($modifierPointer !== null) ? ($modifierPointer['code'] ?? null) : null;
if (
($modifier === T_PRIVATE && $this->skipForPrivate)
|| ($modifier === T_PROTECTED && $this->skipForProtected)
(($modifier === T_PRIVATE) && $this->skipForPrivate)
|| (($modifier === T_PROTECTED) && $this->skipForProtected)
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function process(File $phpcsFile, $stackPtr): void
return;
}
$name = FunctionDeclarations::getName($phpcsFile, $stackPtr);
if (!$name) {
if (($name === null) || ($name === '')) {
return;
}

Expand Down
6 changes: 4 additions & 2 deletions Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ public function process(File $phpcsFile, $stackPtr): void
}

$returnType = $data['return_type'] ?? null;
$returnTypes = $returnType ? $this->normalizeReturnTypes($phpcsFile, $data) : [];
$returnTypes = (is_string($returnType) && $returnType !== '')
? $this->normalizeReturnTypes($phpcsFile, $data)
: [];
$returnInfo = FunctionReturnStatement::allInfo($phpcsFile, $stackPtr);

if ($returnTypes) {
Expand Down Expand Up @@ -161,7 +163,7 @@ private function normalizeReturnTypes(File $file, array $data): array

if (($start > 0) && ($end > 0)) {
$returnTypesStr = Misc::tokensSubsetToString($start, $end, $file, []);
if ($data['nullable_return_type'] ?? false) {
if ((bool) ($data['nullable_return_type'] ?? false)) {
$returnTypesStr .= '|null';
}

Expand Down
2 changes: 1 addition & 1 deletion Inpsyde/Sniffs/CodeQuality/StaticClosureSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function process(File $phpcsFile, $stackPtr): void
}

$isStatic = $phpcsFile->findPrevious(T_STATIC, $stackPtr, $stackPtr - 3, false, null, true);
if ($isStatic) {
if ($isStatic !== false) {
return;
}

Expand Down
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
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@

<issueHandlers>
<MixedAssignment errorLevel="suppress" />
<RedundantCastGivenDocblockType errorLevel="suppress" />
</issueHandlers>
</psalm>

0 comments on commit 1865191

Please sign in to comment.