Skip to content

Commit f807048

Browse files
committed
Fix tests and coding standards for updated deps
1 parent 63d7feb commit f807048

30 files changed

+442
-767
lines changed

.gitignore

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,4 @@ composer.phar
22
composer.lock
33
/vendor/
44
/phpunit.xml
5-
6-
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
7-
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
8-
# composer.lock
9-
.buildpath
10-
.project
11-
.settings/
125
.phpunit.result.cache

Inpsyde/PhpcsHelpers.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,14 @@ public static function findOopContext(File $file, int $position): int
119119
$targetLevel = (int)$tokens[$position]['level'] - 1;
120120

121121
foreach ($tokens[$position]['conditions'] as $condPosition => $condCode) {
122+
assert(is_int($condPosition));
122123
$condLevel = (int)($tokens[$condPosition]['level'] ?? -1);
123124

124125
if (
125126
in_array($condCode, Tokens::$ooScopeTokens, true)
126127
&& ($condLevel === $targetLevel)
127128
) {
128-
return (int)$condPosition;
129+
return $condPosition;
129130
}
130131
}
131132

@@ -387,7 +388,7 @@ public static function functionDocBlockTags(
387388
$normalizedTags = [];
388389
static $rand;
389390
$rand or $rand = bin2hex(random_bytes(3));
390-
foreach ($tags as list($tagName, $tagContent)) {
391+
foreach ($tags as [$tagName, $tagContent]) {
391392
empty($normalizedTags[$tagName]) and $normalizedTags[$tagName] = [];
392393
if (!$normalizeContent) {
393394
$normalizedTags[$tagName][] = $tagContent;
@@ -433,7 +434,7 @@ public static function functionDocBlockParamTypes(File $file, int $functionPosit
433434

434435
$types = [];
435436
foreach ($params as $param) {
436-
preg_match('~^([^$]+)\s*(\$(?:[^\s]+))~', trim($param), $matches);
437+
preg_match('~^([^$]+)\s*(\$\S+)~', trim($param), $matches);
437438
if (empty($matches[1]) || empty($matches[2])) {
438439
continue;
439440
}
@@ -461,7 +462,7 @@ public static function isHookFunction(File $file, int $position): bool
461462
*/
462463
public static function functionBody(File $file, int $position): string
463464
{
464-
list($start, $end) = static::functionBoundaries($file, $position);
465+
[$start, $end] = static::functionBoundaries($file, $position);
465466
if ($start < 0 || $end < 0) {
466467
return '';
467468
}
@@ -470,7 +471,7 @@ public static function functionBody(File $file, int $position): string
470471
$tokens = $file->getTokens();
471472
$body = '';
472473
for ($i = $start + 1; $i < $end; $i++) {
473-
$body .= (string)$tokens[$i]['content'];
474+
$body .= (string)($tokens[$i]['content'] ?? '');
474475
}
475476

476477
return $body;
@@ -531,7 +532,7 @@ public static function returnsCountInfo(File $file, int $position): array
531532
{
532533
$returnCount = ['nonEmpty' => 0, 'void' => 0, 'null' => 0, 'total' => 0];
533534

534-
list($start, $end) = self::functionBoundaries($file, $position);
535+
[$start, $end] = self::functionBoundaries($file, $position);
535536
if ($start < 0 || $end <= 0) {
536537
return $returnCount;
537538
}
@@ -541,8 +542,8 @@ public static function returnsCountInfo(File $file, int $position): array
541542

542543
$pos = $start + 1;
543544
while ($pos < $end) {
544-
list(, $innerFunctionEnd) = self::functionBoundaries($file, $pos);
545-
list(, $innerClassEnd) = self::classBoundaries($file, $pos);
545+
[, $innerFunctionEnd] = self::functionBoundaries($file, $pos);
546+
[, $innerClassEnd] = self::classBoundaries($file, $pos);
546547
if ($innerFunctionEnd > 0 || $innerClassEnd > 0) {
547548
$pos = ($innerFunctionEnd > 0) ? $innerFunctionEnd + 1 : $innerClassEnd + 1;
548549
continue;

Inpsyde/Sniffs/CodeQuality/ArgumentTypeDeclarationSniff.php

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,87 +19,98 @@
1919

2020
class ArgumentTypeDeclarationSniff implements Sniff
2121
{
22-
const TYPE_CODES = [
22+
public const TYPE_CODES = [
2323
T_STRING,
2424
T_ARRAY_HINT,
2525
T_CALLABLE,
2626
T_SELF,
2727
];
2828

29-
const METHODS_WHITELIST = [
29+
public const METHODS_WHITELIST = [
3030
'unserialize',
3131
'seek',
3232
];
3333

3434
/**
35-
* @return array<int|string>
36-
*
37-
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration
35+
* @return list<int|string>
3836
*/
39-
public function register()
37+
public function register(): array
4038
{
41-
// phpcs:enable Inpsyde.CodeQuality.ReturnTypeDeclaration
42-
4339
return [T_FUNCTION, T_CLOSURE];
4440
}
4541

4642
/**
47-
* @param File $file
48-
* @param int $position
43+
* @param File $phpcsFile
44+
* @param int $stackPtr
4945
* @return void
5046
*
51-
* phpcs:disable Inpsyde.CodeQuality
47+
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
48+
* phpcs:disable Generic.Metrics.CyclomaticComplexity
5249
*/
53-
public function process(File $file, $position)
50+
public function process(File $phpcsFile, $stackPtr): void
5451
{
55-
// phpcs:enable Inpsyde.CodeQuality
52+
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
53+
// phpcs:enable Generic.Metrics.CyclomaticComplexity
5654

57-
if (
58-
PhpcsHelpers::functionIsArrayAccess($file, $position)
59-
|| PhpcsHelpers::isHookClosure($file, $position)
60-
|| PhpcsHelpers::isHookFunction($file, $position)
61-
|| PhpcsHelpers::isUntypedPsrMethod($file, $position)
62-
|| (
63-
PhpcsHelpers::functionIsMethod($file, $position)
64-
&& in_array($file->getDeclarationName($position), self::METHODS_WHITELIST, true)
65-
)
66-
) {
55+
if ($this->shouldIgnore($phpcsFile, $stackPtr)) {
6756
return;
6857
}
6958

7059
/** @var array<int, array<string, mixed>> $tokens */
71-
$tokens = $file->getTokens();
72-
$paramsStart = (int)($tokens[$position]['parenthesis_opener'] ?? 0);
73-
$paramsEnd = (int)($tokens[$position]['parenthesis_closer'] ?? 0);
60+
$tokens = $phpcsFile->getTokens();
61+
$paramsStart = (int)($tokens[$stackPtr]['parenthesis_opener'] ?? 0);
62+
$paramsEnd = (int)($tokens[$stackPtr]['parenthesis_closer'] ?? 0);
7463

7564
if (!$paramsStart || !$paramsEnd || $paramsStart >= ($paramsEnd - 1)) {
7665
return;
7766
}
7867

79-
$docBlockTypes = PhpcsHelpers::functionDocBlockParamTypes($file, $position);
80-
$variables = PhpcsHelpers::filterTokensByType($paramsStart, $paramsEnd, $file, T_VARIABLE);
68+
$docBlockTypes = PhpcsHelpers::functionDocBlockParamTypes($phpcsFile, $stackPtr);
69+
$variables = PhpcsHelpers::filterTokensByType($paramsStart, $paramsEnd, $phpcsFile, T_VARIABLE);
8170

8271
foreach ($variables as $varPosition => $varToken) {
8372
// Not triggering error for variable explicitly declared as mixed (or mixed|null)
8473
if ($this->isMixed((string)($varToken['content'] ?? ''), $docBlockTypes)) {
8574
continue;
8675
}
8776

88-
$typePosition = $file->findPrevious(
77+
$typePosition = $phpcsFile->findPrevious(
8978
[T_WHITESPACE, T_ELLIPSIS, T_BITWISE_AND],
9079
$varPosition - 1,
9180
$paramsStart + 1,
9281
true
9382
);
9483

9584
$type = $tokens[$typePosition] ?? null;
96-
/** @psalm-suppress MixedArgument */
97-
if ($type && !in_array($type['code'], self::TYPE_CODES, true)) {
98-
$file->addWarning('Argument type is missing', $position, 'NoArgumentType');
85+
if ($type && !in_array($type['code'] ?? '', self::TYPE_CODES, true)) {
86+
$phpcsFile->addWarning('Argument type is missing', $stackPtr, 'NoArgumentType');
9987
}
10088
}
10189
}
10290

91+
/**
92+
* @param File $phpcsFile
93+
* @param int $stackPtr
94+
* @return bool
95+
*
96+
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
97+
*/
98+
private function shouldIgnore(File $phpcsFile, $stackPtr): bool
99+
{
100+
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
101+
102+
$name = $phpcsFile->getDeclarationName($stackPtr);
103+
104+
return PhpcsHelpers::functionIsArrayAccess($phpcsFile, $stackPtr)
105+
|| PhpcsHelpers::isHookClosure($phpcsFile, $stackPtr)
106+
|| PhpcsHelpers::isHookFunction($phpcsFile, $stackPtr)
107+
|| PhpcsHelpers::isUntypedPsrMethod($phpcsFile, $stackPtr)
108+
|| (
109+
PhpcsHelpers::functionIsMethod($phpcsFile, $stackPtr)
110+
&& in_array($name, self::METHODS_WHITELIST, true)
111+
);
112+
}
113+
103114
/**
104115
* @param string $paramName
105116
* @param array<string, array<string>> $docBlockTypes

Inpsyde/Sniffs/CodeQuality/ConstantVisibilitySniff.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

Inpsyde/Sniffs/CodeQuality/DisallowShortOpenTagSniff.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,10 @@
1818
class DisallowShortOpenTagSniff extends Generic\DisallowShortOpenTagSniff
1919
{
2020
/**
21-
* @return array<int>
22-
*
23-
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
24-
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration
21+
* @return list<int>
2522
*/
26-
public function register()
23+
public function register(): array
2724
{
28-
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
29-
// phpcs:enable Inpsyde.CodeQuality.ReturnTypeDeclaration
30-
3125
return [T_OPEN_TAG, T_INLINE_HTML];
3226
}
3327
}

Inpsyde/Sniffs/CodeQuality/ElementNameMinimalLengthSniff.php

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@
1010

1111
class ElementNameMinimalLengthSniff implements Sniff
1212
{
13-
/**
14-
* @var int
15-
*/
16-
public $minLength = 3;
13+
public int $minLength = 3;
1714

1815
/**
19-
* @var string[]
16+
* @var list<string>
2017
*/
21-
public $allowedShortNames = [
18+
public array $allowedShortNames = [
2219
'as',
2320
'at',
2421
'be',
@@ -45,46 +42,36 @@ class ElementNameMinimalLengthSniff implements Sniff
4542
'wp',
4643
];
4744

48-
/**
49-
* @var string[]
50-
*/
51-
public $additionalAllowedNames = [];
45+
/** @var list<string> */
46+
public array $additionalAllowedNames = [];
5247

5348
/**
54-
* @return array<int>
55-
*
56-
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
57-
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration
49+
* @return list<int>
5850
*/
59-
public function register()
51+
public function register(): array
6052
{
61-
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
62-
// phpcs:enable Inpsyde.CodeQuality.ReturnTypeDeclaration
63-
6453
return [T_CLASS, T_TRAIT, T_INTERFACE, T_CONST, T_FUNCTION, T_VARIABLE];
6554
}
6655

6756
/**
68-
* @param File $file
69-
* @param int $position
57+
* @param File $phpcsFile
58+
* @param int $stackPtr
7059
* @return void
7160
*
7261
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
73-
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration
7462
*/
75-
public function process(File $file, $position)
63+
public function process(File $phpcsFile, $stackPtr): void
7664
{
7765
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
78-
// phpcs:enable Inpsyde.CodeQuality.ReturnTypeDeclaration
7966

80-
$elementName = PhpcsHelpers::tokenName($file, $position);
67+
$elementName = PhpcsHelpers::tokenName($phpcsFile, $stackPtr);
8168
$elementNameLength = mb_strlen($elementName);
8269

8370
if ($this->shouldBeSkipped($elementNameLength, $elementName)) {
8471
return;
8572
}
8673

87-
$typeName = PhpcsHelpers::tokenTypeName($file, $position);
74+
$typeName = PhpcsHelpers::tokenTypeName($phpcsFile, $stackPtr);
8875
$message = sprintf(
8976
'%s name "%s" is only %d chars long. Must be at least %d.',
9077
$typeName,
@@ -93,7 +80,7 @@ public function process(File $file, $position)
9380
$this->minLength
9481
);
9582

96-
$file->addError($message, $position, 'TooShort');
83+
$phpcsFile->addError($message, $stackPtr, 'TooShort');
9784
}
9885

9986
/**

0 commit comments

Comments
 (0)