Skip to content

Commit

Permalink
Merge branch 'development' into feature/inpyde-is-now-syde
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Mar 13, 2024
2 parents 389346f + 4bdeb30 commit ed80a12
Show file tree
Hide file tree
Showing 64 changed files with 287 additions and 93 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/quality-assurance-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
uses: inpsyde/reusable-workflows/.github/workflows/lint-php.yml@main
strategy:
matrix:
php-ver: [ '7.4', '8.0', '8.1', '8.2' ]
php-ver: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
with:
PHP_VERSION: ${{ matrix.php-ver }}
LINT_ARGS: '-e php --colors --show-deprecated ./Inpsyde'
Expand All @@ -58,7 +58,7 @@ jobs:
uses: inpsyde/reusable-workflows/.github/workflows/static-analysis-php.yml@main
strategy:
matrix:
php-ver: [ '7.4', '8.0', '8.1', '8.2' ]
php-ver: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
with:
PHP_VERSION: ${{ matrix.php-ver }}
PSALM_ARGS: ${{ format('--no-suggestions --report-show-info=false --find-unused-psalm-suppress --no-diff --output-format={0}', ((github.event_name == 'pull_request') && 'github') || 'compact') }}
Expand All @@ -68,7 +68,7 @@ jobs:
uses: inpsyde/reusable-workflows/.github/workflows/tests-unit-php.yml@main
strategy:
matrix:
php-ver: [ '7.4', '8.0', '8.1', '8.2' ]
php-ver: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
with:
PHP_VERSION: ${{ matrix.php-ver }}
PHPUNIT_ARGS: '--no-coverage'
6 changes: 3 additions & 3 deletions Inpsyde/Helpers/Boundaries.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,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 @@ -88,8 +88,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
6 changes: 3 additions & 3 deletions Inpsyde/Helpers/FunctionDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ 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 [];
}
Expand All @@ -60,7 +60,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 @@ -112,7 +112,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 @@ -43,7 +43,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 @@ -58,7 +58,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 @@ -63,6 +63,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 @@ -58,7 +58,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 @@ -69,7 +69,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 @@ -33,8 +33,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 @@ -52,8 +52,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 @@ -83,7 +83,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 @@ -139,7 +139,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
12 changes: 6 additions & 6 deletions Inpsyde/Sniffs/CodeQuality/FunctionLengthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,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 @@ -114,7 +114,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 @@ -132,7 +132,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 @@ -165,7 +165,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 Down
62 changes: 40 additions & 22 deletions Inpsyde/Sniffs/CodeQuality/LineLengthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,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 !== null) && ($tokens[$i]['line'] === $lastLine)) {
$content = (string)$tokens[$i]['content'];
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 !== null) && isset($data[$lastLine])) {
if (($lastLine !== null) && ($lastLine > 0) && isset($data[$lastLine])) {
$data[$lastLine]['end'] = $i - 1;
}

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

// We still have to set the "end" for last file line.
if (($lastLine !== null) && ($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 Expand Up @@ -204,7 +217,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 @@ -247,7 +260,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 @@ -257,7 +275,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 @@ -296,7 +314,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 @@ -309,7 +327,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 @@ -336,7 +354,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
8 changes: 4 additions & 4 deletions Inpsyde/Sniffs/CodeQuality/NestingLevelSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,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 @@ -58,7 +58,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
2 changes: 1 addition & 1 deletion Inpsyde/Sniffs/CodeQuality/Psr4Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,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
7 changes: 5 additions & 2 deletions Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,13 @@ private function checkInvalidGenerator(
): bool {

$hasGenerator = false;
$hasIterator = false;
while (!$hasGenerator && $returnTypes) {
$returnType = explode('&', rtrim(ltrim(array_shift($returnTypes), '('), ')'));
$hasGenerator = in_array('Generator', $returnType, true)
|| in_array('\Generator', $returnType, true)
|| in_array('\Generator', $returnType, true);
$hasIterator = $hasIterator
|| $hasGenerator
|| in_array('Traversable', $returnType, true)
|| in_array('\Traversable', $returnType, true)
|| in_array('Iterator', $returnType, true)
Expand Down Expand Up @@ -359,7 +362,7 @@ private function checkInvalidGenerator(
return true;
}

if (!$hasGenerator && ($yieldCount > 0)) {
if (!$hasIterator && ($yieldCount > 0)) {
$file->addError(
'Return type does not contain "Generator" but yield found in the function body',
$position,
Expand Down
Loading

0 comments on commit ed80a12

Please sign in to comment.