Skip to content

Commit ed80a12

Browse files
committed
Merge branch 'development' into feature/inpyde-is-now-syde
2 parents 389346f + 4bdeb30 commit ed80a12

File tree

64 files changed

+287
-93
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+287
-93
lines changed

.github/workflows/quality-assurance-php.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
uses: inpsyde/reusable-workflows/.github/workflows/lint-php.yml@main
4545
strategy:
4646
matrix:
47-
php-ver: [ '7.4', '8.0', '8.1', '8.2' ]
47+
php-ver: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
4848
with:
4949
PHP_VERSION: ${{ matrix.php-ver }}
5050
LINT_ARGS: '-e php --colors --show-deprecated ./Inpsyde'
@@ -58,7 +58,7 @@ jobs:
5858
uses: inpsyde/reusable-workflows/.github/workflows/static-analysis-php.yml@main
5959
strategy:
6060
matrix:
61-
php-ver: [ '7.4', '8.0', '8.1', '8.2' ]
61+
php-ver: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
6262
with:
6363
PHP_VERSION: ${{ matrix.php-ver }}
6464
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') }}
@@ -68,7 +68,7 @@ jobs:
6868
uses: inpsyde/reusable-workflows/.github/workflows/tests-unit-php.yml@main
6969
strategy:
7070
matrix:
71-
php-ver: [ '7.4', '8.0', '8.1', '8.2' ]
71+
php-ver: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
7272
with:
7373
PHP_VERSION: ${{ matrix.php-ver }}
7474
PHPUNIT_ARGS: '--no-coverage'

Inpsyde/Helpers/Boundaries.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function arrayBoundaries(File $file, int $position): array
6767
return [-1, -1];
6868
}
6969

70-
return [(int)$openClose['opener'], (int)$openClose['closer']];
70+
return [(int) $openClose['opener'], (int) $openClose['closer']];
7171
}
7272

7373
/**
@@ -88,8 +88,8 @@ private static function startEnd(File $file, int $position): array
8888
return [$start + 1, $file->findEndOfStatement($start)];
8989
}
9090

91-
$start = (int)($token['scope_opener'] ?? 0);
92-
$end = (int)($token['scope_closer'] ?? 0);
91+
$start = (int) ($token['scope_opener'] ?? 0);
92+
$end = (int) ($token['scope_closer'] ?? 0);
9393
if (($start <= 0) || ($end <= 0) || ($start >= ($end - 1))) {
9494
return [-1, -1];
9595
}

Inpsyde/Helpers/FunctionDocBlock.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public static function allTags(
4242
return [];
4343
}
4444

45-
$functionLine = (int)($tokens[$position]['line'] ?? -1);
46-
$closeLine = (int)($tokens[$closeTag]['line'] ?? -1);
45+
$functionLine = (int) ($tokens[$position]['line'] ?? -1);
46+
$closeLine = (int) ($tokens[$closeTag]['line'] ?? -1);
4747
if ($closeLine !== ($functionLine - 1)) {
4848
return [];
4949
}
@@ -60,7 +60,7 @@ public static function allTags(
6060
continue;
6161
}
6262

63-
$content = (string)$tokens[$i]['content'];
63+
$content = (string) $tokens[$i]['content'];
6464
if (($tokens[$i]['code'] === T_DOC_COMMENT_TAG)) {
6565
$inTag = true;
6666
$key++;

Inpsyde/Helpers/Misc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static function tokensSubsetToString(
112112

113113
$content = '';
114114
foreach ($filtered as $token) {
115-
$content .= (string)($token['content'] ?? '');
115+
$content .= (string) ($token['content'] ?? '');
116116
}
117117

118118
return $content;

Inpsyde/Helpers/Names.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function nameableTokenName(File $file, int $position): ?string
4343
}
4444

4545
if ($code === T_VARIABLE) {
46-
$name = ltrim((string)($tokens[$position]['content'] ?? ''), '$');
46+
$name = ltrim((string) ($tokens[$position]['content'] ?? ''), '$');
4747

4848
return ($name === '') ? null : $name;
4949
}
@@ -58,7 +58,7 @@ public static function nameableTokenName(File $file, int $position): ?string
5858
}
5959

6060
$namePosition = $file->findNext(T_STRING, $position, null, false, null, true);
61-
$name = ($namePosition === false) ? null : (string)$tokens[$namePosition]['content'];
61+
$name = ($namePosition === false) ? null : (string) $tokens[$namePosition]['content'];
6262

6363
return ($name === '') ? null : $name;
6464
}

Inpsyde/Helpers/WpHooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ public static function isHookClosure(
6363
*/
6464
public static function isHookFunction(File $file, int $position): bool
6565
{
66-
return (bool)FunctionDocBlock::tag('@wp-hook', $file, $position);
66+
return (bool) FunctionDocBlock::tag('@wp-hook', $file, $position);
6767
}
6868
}

Inpsyde/Sniffs/CodeQuality/ForbiddenPublicPropertySniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private function isSniffClass(File $file, int $position): bool
5858
{
5959
$classNameTokenPosition = $file->findNext(
6060
T_STRING,
61-
(int)$file->findPrevious(T_CLASS, $position)
61+
(int) $file->findPrevious(T_CLASS, $position)
6262
);
6363

6464
if ($classNameTokenPosition === false) {
@@ -69,7 +69,7 @@ private function isSniffClass(File $file, int $position): bool
6969
$tokens = $file->getTokens();
7070
$classNameToken = $tokens[$classNameTokenPosition];
7171

72-
if (substr((string)$classNameToken['content'], -5, 5) === 'Sniff') {
72+
if (substr((string) $classNameToken['content'], -5, 5) === 'Sniff') {
7373
return true;
7474
}
7575

Inpsyde/Sniffs/CodeQuality/FunctionBodyStartSniff.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public function process(File $phpcsFile, $stackPtr): void
3333
$tokens = $phpcsFile->getTokens();
3434
$token = $tokens[$stackPtr] ?? [];
3535

36-
$scopeOpener = (int)($token['scope_opener'] ?? -1);
37-
$scopeCloser = (int)($token['scope_closer'] ?? -1);
36+
$scopeOpener = (int) ($token['scope_opener'] ?? -1);
37+
$scopeCloser = (int) ($token['scope_closer'] ?? -1);
3838

3939
if ($scopeOpener < 0 || $scopeCloser < 0 || $scopeCloser <= $scopeOpener) {
4040
return;
@@ -52,8 +52,8 @@ public function process(File $phpcsFile, $stackPtr): void
5252

5353
[$code, $message, $expectedLine] = $this->checkBodyStart(
5454
$bodyStart,
55-
(int)($tokens[$scopeOpener]['line'] ?? -1),
56-
(int)($token['line'] ?? -1),
55+
(int) ($tokens[$scopeOpener]['line'] ?? -1),
56+
(int) ($token['line'] ?? -1),
5757
$phpcsFile
5858
);
5959

@@ -83,7 +83,7 @@ private function checkBodyStart(
8383

8484
/** @var array<int, array<string, mixed>> $tokens */
8585
$tokens = $file->getTokens();
86-
$bodyLine = (int)($tokens[$bodyStart]['line'] ?? -1);
86+
$bodyLine = (int) ($tokens[$bodyStart]['line'] ?? -1);
8787

8888
$isMultiLineDeclare = ($openerLine - $functionLine) > 1;
8989
$isSingleLineDeclare = $openerLine === ($functionLine + 1);
@@ -139,7 +139,7 @@ private function fix(int $bodyStart, int $expectedLine, int $scopeOpener, File $
139139
{
140140
/** @var array<int, array<string, mixed>> $tokens */
141141
$tokens = $file->getTokens();
142-
$currentLine = (int)($tokens[$bodyStart]['line'] ?? -1);
142+
$currentLine = (int) ($tokens[$bodyStart]['line'] ?? -1);
143143

144144
if ($currentLine === $expectedLine) {
145145
return;

Inpsyde/Sniffs/CodeQuality/FunctionLengthSniff.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ private function structureLinesCount(File $file, int $position): int
7979
return 0;
8080
}
8181

82-
$start = (int)$token['scope_opener'];
83-
$end = (int)$token['scope_closer'];
84-
$length = (int)$tokens[$end]['line'] - (int)$tokens[$start]['line'];
82+
$start = (int) $token['scope_opener'];
83+
$end = (int) $token['scope_closer'];
84+
$length = (int) $tokens[$end]['line'] - (int) $tokens[$start]['line'];
8585

8686
if ($length < $this->maxLength) {
8787
return $length;
@@ -114,7 +114,7 @@ private function collectLinesToExclude(int $start, int $end, array $tokens): int
114114
$empty = array_filter(array_column($linesData, 'empty'));
115115
$onlyComment = array_filter(array_column($linesData, 'only-comment'));
116116

117-
$toExcludeCount = (int)array_sum($docblocks);
117+
$toExcludeCount = (int) array_sum($docblocks);
118118
if ($this->ignoreBlankLines) {
119119
$toExcludeCount += count($empty);
120120
}
@@ -132,7 +132,7 @@ private function collectLinesToExclude(int $start, int $end, array $tokens): int
132132
*/
133133
private function ignoredLinesData(array $token, array $lines): array
134134
{
135-
$line = (int)$token['line'];
135+
$line = (int) $token['line'];
136136
if (!array_key_exists($line, $lines)) {
137137
$lines[$line] = ['empty' => true, 'only-comment' => true];
138138
}
@@ -165,7 +165,7 @@ private function docBlocksData(array $tokens, int $position, array $docBlocks):
165165

166166
$closer = $tokens[$position]['comment_closer'] ?? null;
167167
$docBlocks[] = is_numeric($closer)
168-
? 1 + ((int)$tokens[(int)$closer]['line'] - (int)$tokens[$position]['line'])
168+
? 1 + ((int) $tokens[(int) $closer]['line'] - (int) $tokens[$position]['line'])
169169
: 1;
170170

171171
return $docBlocks;

Inpsyde/Sniffs/CodeQuality/LineLengthSniff.php

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ private function collectLongLinesData(File $file, int $start): array
8888
$lastLine = null;
8989
for ($i = $start; $i < $file->numTokens; $i++) {
9090
// Still processing previous line: increment length and continue.
91-
if (($lastLine !== null) && ($tokens[$i]['line'] === $lastLine)) {
92-
$content = (string)$tokens[$i]['content'];
91+
if (($lastLine !== null) && ($lastLine > 0) && ($tokens[$i]['line'] === $lastLine)) {
92+
$content = (string) $tokens[$i]['content'];
9393
$data[$lastLine]['length'] += strlen($content);
9494
$data[$lastLine]['nonEmptyLength'] += strlen(trim($content));
9595
continue;
9696
}
9797

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

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

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

118119
$longLines = [];
120+
/**
121+
* @var int $lineNumber
122+
* @var array{length:int, nonEmptyLength:int, start:int, end:int|null} $lineData
123+
*/
119124
foreach ($data as $lineNumber => $lineData) {
120-
$lineEnd = $lineData['end'] ?? $lineData['start'];
121-
if (
122-
(($lineData['length'] - $this->lineLimit) <= 1) // 1 char of tolerance
123-
|| ($lineData['nonEmptyLength'] === 0) // ignore empty lines
124-
|| $this->isLongUse($file, $tokens, $lineData['start'], $lineEnd)
125-
|| $this->isLongI10nFunction($file, $tokens, $lineData['start'], $lineEnd)
126-
|| $this->isLongWord($file, $tokens, $lineData['start'], $lineEnd)
127-
) {
128-
continue;
125+
if (!$this->isLengthAcceptable($lineData, $file, $tokens)) {
126+
$longLines[$lineNumber] = [$lineData['length'], $lineData['start']];
129127
}
130-
131-
$longLines[$lineNumber] = [$lineData['length'], $lineData['start']];
132128
}
133129

134130
return $longLines;
135131
}
136132

133+
/**
134+
* @param array{length:int, nonEmptyLength:int, start:int, end:int|null} $lineData
135+
* @param File $file
136+
* @param array<int, array<string, mixed>> $tokens
137+
* @return bool
138+
*/
139+
private function isLengthAcceptable(array $lineData, File $file, array $tokens): bool
140+
{
141+
$lineEnd = $lineData['end'] ?? $lineData['start'];
142+
143+
return (($lineData['length'] - $this->lineLimit) <= 1) // 1 char of tolerance
144+
|| ($lineData['nonEmptyLength'] === 0) // ignore empty lines
145+
|| $this->isLongUse($file, $tokens, $lineData['start'], $lineEnd)
146+
|| $this->isLongI10nFunction($file, $tokens, $lineData['start'], $lineEnd)
147+
|| $this->isLongWord($file, $tokens, $lineData['start'], $lineEnd);
148+
}
149+
137150
/**
138151
* We don't want to split a single word in multiple lines.
139-
* So if there's a long word (e.g. an URL) that alone is above max line length, we don't show
152+
* So if there's a long word (e.g. a URL) that alone is above max line length, we don't show
140153
* warnings for it.
141154
*
142155
* @param File $file
@@ -204,7 +217,7 @@ private function isLongHtmlAttribute(
204217
$inPhp = true;
205218
}
206219
if ($tokens[$i]['code'] === T_INLINE_HTML || $inPhp) {
207-
$tokenContent = (string)$tokens[$i]['content'];
220+
$tokenContent = (string) $tokens[$i]['content'];
208221
$content .= $inPhp ? str_repeat('x', strlen($tokenContent)) : $tokenContent;
209222
}
210223
if ($tokens[$i]['code'] === T_CLOSE_TAG && $inPhp) {
@@ -247,7 +260,12 @@ private function isLongSingleWord(
247260
array $tokens
248261
): bool {
249262

250-
$words = preg_split('~\s+~', (string)$tokens[$position]['content'], 2, PREG_SPLIT_NO_EMPTY);
263+
$words = preg_split(
264+
'~\s+~',
265+
(string) $tokens[$position]['content'],
266+
2,
267+
PREG_SPLIT_NO_EMPTY
268+
);
251269

252270
// If multiple words exceed line limit, we can split each word in its own line
253271
if ($words === false || count($words) !== 1) {
@@ -257,7 +275,7 @@ private function isLongSingleWord(
257275
$word = reset($words);
258276
$firstNonWhitePos = $file->findNext(T_WHITESPACE, $position, $lineEnd, true);
259277
$firstNonWhite = ($firstNonWhitePos === false) ? null : $tokens[$firstNonWhitePos];
260-
$tolerance = is_array($firstNonWhite) ? ((int)($firstNonWhite['column'] ?? 1) + 3) : 4;
278+
$tolerance = is_array($firstNonWhite) ? ((int) ($firstNonWhite['column'] ?? 1) + 3) : 4;
261279

262280
return (strlen($word) + $tolerance) > $this->lineLimit;
263281
}
@@ -296,7 +314,7 @@ private function isLongI10nFunction(File $file, array $tokens, int $start, int $
296314
return false;
297315
}
298316

299-
$function = strtolower((string)$tokens[$functionPos]['content']);
317+
$function = strtolower((string) $tokens[$functionPos]['content']);
300318
if (!in_array($function, self::I18N_FUNCTIONS, true)) {
301319
return false;
302320
}
@@ -309,7 +327,7 @@ private function isLongI10nFunction(File $file, array $tokens, int $start, int $
309327
if ($tokens[$i]['line'] !== $targetLine) {
310328
continue;
311329
}
312-
$textLen += max(1, strlen((string)$tokens[$i]['content']));
330+
$textLen += max(1, strlen((string) $tokens[$i]['content']));
313331
}
314332

315333
return ($textLen + 2) > $this->lineLimit;
@@ -336,7 +354,7 @@ private function isLongUse(File $file, array $tokens, int $start, int $end): boo
336354
$endUse = $file->findEndOfStatement($usePos);
337355
$useLen = 0;
338356
for ($i = $usePos; $i <= $endUse; $i++) {
339-
$useLen += strlen((string)$tokens[$i]['content']);
357+
$useLen += strlen((string) $tokens[$i]['content']);
340358
}
341359

342360
return $useLen > $this->lineLimit;

0 commit comments

Comments
 (0)