Skip to content

Commit

Permalink
Apply the new rules to the current code
Browse files Browse the repository at this point in the history
There will be a lot of fun.
  • Loading branch information
antonioeatgoat committed Oct 27, 2023
1 parent 45b6888 commit 10c0968
Show file tree
Hide file tree
Showing 25 changed files with 159 additions and 154 deletions.
18 changes: 9 additions & 9 deletions Inpsyde/Helpers/Boundaries.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function functionBoundaries(File $file, int $position): array
$tokens = $file->getTokens();

if (
!in_array(
! in_array(
$tokens[$position]['code'] ?? null,
array_keys(Collections::functionDeclarationTokens()),
true
Expand All @@ -68,7 +68,7 @@ public static function objectBoundaries(File $file, int $position): array
/** @var array<int, array<string, mixed>> $tokens */
$tokens = $file->getTokens();

if (!in_array(($tokens[$position]['code'] ?? null), Tokens::$ooScopeTokens, true)) {
if (! in_array(($tokens[$position]['code'] ?? null), Tokens::$ooScopeTokens, true)) {
return [-1, -1];
}

Expand All @@ -84,14 +84,14 @@ public static function arrayBoundaries(File $file, int $position): array
{
$openClose = Arrays::getOpenClose($file, $position);
if (
!is_array($openClose)
|| !is_int($openClose['opener'] ?? null)
|| !is_int($openClose['closer'] ?? null)
! is_array($openClose)
|| ! is_int($openClose['opener'] ?? null)
|| ! is_int($openClose['closer'] ?? null)
) {
return [-1, -1];
}

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

/**
Expand All @@ -105,15 +105,15 @@ 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) {
return [-1, -1];
}

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
20 changes: 10 additions & 10 deletions Inpsyde/Helpers/FunctionDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,28 @@ public static function allTags(
$tokens = $file->getTokens();

if (
!array_key_exists($position, $tokens)
|| !in_array($tokens[$position]['code'], [T_FUNCTION, T_CLOSURE, T_FN], true)
! array_key_exists($position, $tokens)
|| ! in_array($tokens[$position]['code'], [T_FUNCTION, T_CLOSURE, T_FN], true)
) {
return [];
}

$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 || empty($tokens[$closeTag]['comment_opener'])) {
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 All @@ -102,7 +102,7 @@ public static function allTags(
$rand or $rand = bin2hex(random_bytes(3));
foreach ($tags as [$tagName, $tagContent]) {
empty($normalizedTags[$tagName]) and $normalizedTags[$tagName] = [];
if (!$normalizeContent) {
if (! $normalizeContent) {
$normalizedTags[$tagName][] = $tagContent;
continue;
}
Expand Down Expand Up @@ -140,7 +140,7 @@ public static function tag(string $tag, File $file, int $position): array
public static function allParamTypes(File $file, int $functionPosition): array
{
$params = static::tag('@param', $file, $functionPosition);
if (!$params) {
if (! $params) {
return [];
}

Expand Down Expand Up @@ -173,7 +173,7 @@ public static function normalizeTypesString(string $typesString): array
$splitType = substr($splitType, 1) ?: '';
$hasNull = $hasNull || ($splitType !== '');
}
if (!$splitType) {
if (! $splitType) {
continue;
}
if (strtolower($splitType) === 'null') {
Expand Down
6 changes: 3 additions & 3 deletions Inpsyde/Helpers/FunctionReturnStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public static function allInfo(File $file, int $position): array
if ($tokens[$pos]['code'] === T_RETURN) {
$returnCount['total']++;
$void = static::isVoid($file, $pos);
$null = !$void && static::isNull($file, $pos);
$null = ! $void && static::isNull($file, $pos);
$void and $returnCount['void']++;
$null and $returnCount['null']++;
(!$void && !$null) and $returnCount['nonEmpty']++;
(! $void && ! $null) and $returnCount['nonEmpty']++;
}

$pos++;
Expand Down 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) {
return false;
}
}
Expand Down
22 changes: 11 additions & 11 deletions Inpsyde/Helpers/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public static function looksLikeFunctionCall(File $file, int $position): bool
$types[] = T_VARIABLE;

if (
!in_array($code, $types, true)
! in_array($code, $types, true)
|| (($code === T_VARIABLE) && Scopes::isOOProperty($file, $position))
) {
return false;
}

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

Expand Down Expand Up @@ -158,7 +158,7 @@ public static function countYieldInBody(File $file, int $position): int
*/
public static function isPsrMethod(File $file, int $position): bool
{
if (!Scopes::isOOMethod($file, $position)) {
if (! Scopes::isOOMethod($file, $position)) {
return false;
}

Expand All @@ -167,7 +167,7 @@ public static function isPsrMethod(File $file, int $position): bool

$classPos = Conditions::getLastCondition($file, $position, $scopes);
$type = is_int($classPos) ? ($tokens[$classPos]['code'] ?? null) : null;
if (!in_array($type, $scopes, true)) {
if (! in_array($type, $scopes, true)) {
return false;
}

Expand Down Expand Up @@ -209,7 +209,7 @@ public static function isNonDeclarableDocBlockType(array $docTypes, bool $return
// If "never" is there, this is valid for return types and PHP < 8.1,
// not valid for argument types.
if (in_array('never', $docTypes, true)) {
return $return && !$is81;
return $return && ! $is81;
}

if ($count > 1) {
Expand All @@ -219,8 +219,8 @@ public static function isNonDeclarableDocBlockType(array $docTypes, bool $return
}
// Union type without null, valid if we're not on PHP < 8.0, or on PHP < 8.2 and
// there's an intersection (DNF)
if (!in_array('null', $docTypes, true)) {
return !$is80 || (!$is82 && $isIntersection);
if (! in_array('null', $docTypes, true)) {
return ! $is80 || (! $is82 && $isIntersection);
}
$docTypes = array_diff($docTypes, ['null']);
$count = count($docTypes);
Expand All @@ -229,16 +229,16 @@ public static function isNonDeclarableDocBlockType(array $docTypes, bool $return
// Union type with "null" plus something else, valid if we're not on PHP < 8.0 or
// on PHP < 8.2 and there's an intersection (DNF)
if ($count > 1) {
return !$is80 || (!$is82 && $isIntersection);
return ! $is80 || (! $is82 && $isIntersection);
}

$singleDocType = reset($docTypes);

// If the single type is "mixed" is valid if we are on PHP < 8.0.
// If the single type is "null" is valid if we are on PHP < 8.2.
// If the single is an intersection, is valid if we are on PHP < 8.1
return (($singleDocType === 'mixed') && !$is80)
|| (($singleDocType === 'null') && !$is82)
|| ($isIntersection && !$is81);
return (($singleDocType === 'mixed') && ! $is80)
|| (($singleDocType === 'null') && ! $is82)
|| ($isIntersection && ! $is81);
}
}
8 changes: 4 additions & 4 deletions Inpsyde/Helpers/Misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class Misc
public static function minPhpTestVersion(): string
{
$testVersion = trim(Config::getConfigData('testVersion') ?: '');
if (!$testVersion) {
if (! $testVersion) {
return self::MIN_SUPPORTED_VERSION;
}

Expand Down Expand Up @@ -107,8 +107,8 @@ public static function filterTokensByType(
continue;
}
$empty = $types === [];
$inArray = !$empty && in_array($token['code'] ?? '', $types, true);
if ($empty || (!$excludeTypes && $inArray) || ($excludeTypes && !$inArray)) {
$inArray = ! $empty && in_array($token['code'] ?? '', $types, true);
if ($empty || (! $excludeTypes && $inArray) || ($excludeTypes && ! $inArray)) {
$filtered[$i] = $token;
}
}
Expand Down 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
6 changes: 3 additions & 3 deletions Inpsyde/Helpers/Names.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public static function nameableTokenName(File $file, int $position): ?string
$tokens = $file->getTokens();
$code = $tokens[$position]['code'] ?? null;

if (!in_array($code, self::NAMEABLE_TOKENS, true)) {
if (! in_array($code, self::NAMEABLE_TOKENS, true)) {
return null;
}

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
16 changes: 8 additions & 8 deletions Inpsyde/Helpers/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function countProperties(File $file, int $position): int
/** @var array<int, array<string, mixed>> $tokens */
$tokens = $file->getTokens();
if (
!in_array(
! in_array(
$tokens[$position]['code'] ?? null,
Collections::ooPropertyScopes(),
true
Expand Down Expand Up @@ -100,16 +100,16 @@ public static function findAllImportUses(File $file, int $position): array

while (true) {
$nextUse = $file->findNext(T_USE, $nextUse + 1, $position - 1);
if (!$nextUse) {
if (! $nextUse) {
break;
}
if (!UseStatements::isImportUse($file, $nextUse)) {
if (! UseStatements::isImportUse($file, $nextUse)) {
continue;
}
$usePositions[] = $nextUse;
}

if (!$usePositions) {
if (! $usePositions) {
return [];
}

Expand Down Expand Up @@ -148,12 +148,12 @@ public static function allInterfacesFullyQualifiedNames(File $file, int $positio
{
$tokens = $file->getTokens();
$code = $tokens[$position]['code'] ?? null;
if (!in_array($code, Collections::ooCanImplement(), true)) {
if (! in_array($code, Collections::ooCanImplement(), true)) {
return null;
}

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

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

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

$uses = static::findAllImportUses($file, $position - 1);
/** @var non-empty-list<string>|false $names */
$names = ObjectDeclarations::findImplementedInterfaceNames($file, $position);
if (!$names) {
if (! $names) {
return [];
}

Expand Down
10 changes: 5 additions & 5 deletions Inpsyde/Helpers/WpHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function isHookClosure(
/** @var array<int, array<string, mixed>> $tokens */
$tokens = $file->getTokens();

if (!in_array(($tokens[$position]['code'] ?? ''), [T_CLOSURE, T_FN], true)) {
if (! in_array(($tokens[$position]['code'] ?? ''), [T_CLOSURE, T_FN], true)) {
return false;
}

Expand All @@ -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 || ($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) {
return false;
}

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

Expand All @@ -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/ArgumentTypeDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public function process(File $phpcsFile, $stackPtr): void
}

$docTypes = $docBlockTypes[$parameter['name']] ?? [];
if (!Functions::isNonDeclarableDocBlockType($docTypes, false)) {
if (! Functions::isNonDeclarableDocBlockType($docTypes, false)) {
$errors[] = $parameter['name'];
}
}

if (!$errors) {
if (! $errors) {
return;
}

Expand Down
Loading

0 comments on commit 10c0968

Please sign in to comment.