Skip to content

Commit 757efc8

Browse files
committed
RequireExplicitAssertionSniff: Fixed fixer
1 parent 538ab63 commit 757efc8

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

SlevomatCodingStandard/Sniffs/PHP/RequireExplicitAssertionSniff.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use SlevomatCodingStandard\Helpers\Annotation\VariableAnnotation;
1313
use SlevomatCodingStandard\Helpers\AnnotationHelper;
1414
use SlevomatCodingStandard\Helpers\IndentationHelper;
15+
use SlevomatCodingStandard\Helpers\ScopeHelper;
1516
use SlevomatCodingStandard\Helpers\TokenHelper;
1617
use SlevomatCodingStandard\Helpers\TypeHintHelper;
1718
use function array_key_exists;
@@ -109,7 +110,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
109110
continue;
110111
}
111112

112-
$pointerToAddAssertion = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $codePointer + 1);
113+
$pointerToAddAssertion = $this->getNextSemicolonInSameScope($phpcsFile, $codePointer, $codePointer + 1);
113114
$indentation = IndentationHelper::getIndentation($phpcsFile, $docCommentOpenPointer);
114115

115116
} elseif ($tokens[$codePointer]['code'] === T_LIST) {
@@ -120,7 +121,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
120121
continue;
121122
}
122123

123-
$pointerToAddAssertion = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $codePointer + 1);
124+
$pointerToAddAssertion = $this->getNextSemicolonInSameScope($phpcsFile, $codePointer, $codePointer + 1);
124125
$indentation = IndentationHelper::getIndentation($phpcsFile, $docCommentOpenPointer);
125126

126127
} elseif ($tokens[$codePointer]['code'] === T_OPEN_SHORT_ARRAY) {
@@ -134,7 +135,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
134135
continue;
135136
}
136137

137-
$pointerToAddAssertion = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $tokens[$codePointer]['bracket_closer'] + 1);
138+
$pointerToAddAssertion = $this->getNextSemicolonInSameScope($phpcsFile, $codePointer, $tokens[$codePointer]['bracket_closer'] + 1);
138139
$indentation = IndentationHelper::getIndentation($phpcsFile, $docCommentOpenPointer);
139140

140141
} else {
@@ -226,6 +227,24 @@ private function isValidTypeNode(TypeNode $typeNode): bool
226227
return !in_array($typeNode->name, ['mixed', 'static'], true);
227228
}
228229

230+
private function getNextSemicolonInSameScope(File $phpcsFile, int $scopePointer, int $searchAt): int
231+
{
232+
$semicolonPointer = null;
233+
234+
do {
235+
$semicolonPointer = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $searchAt);
236+
237+
if (ScopeHelper::isInSameScope($phpcsFile, $scopePointer, $semicolonPointer)) {
238+
break;
239+
}
240+
241+
$searchAt = $semicolonPointer + 1;
242+
243+
} while (true);
244+
245+
return $semicolonPointer;
246+
}
247+
229248
/**
230249
* @param string $variableName
231250
* @param IdentifierTypeNode|ThisTypeNode|UnionTypeNode|IntersectionTypeNode $typeNode

tests/Sniffs/PHP/RequireExplicitAssertionSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/requireExplicitAssertionErrors.php');
1919

20-
self::assertSame(27, $report->getErrorCount());
20+
self::assertSame(28, $report->getErrorCount());
2121

2222
self::assertSniffError($report, 3, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
2323
self::assertSniffError($report, 6, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
@@ -46,6 +46,7 @@ public function testErrors(): void
4646
self::assertSniffError($report, 96, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
4747
self::assertSniffError($report, 97, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
4848
self::assertSniffError($report, 102, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
49+
self::assertSniffError($report, 109, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
4950

5051
self::assertAllFixedInFile($report);
5152
}

tests/Sniffs/PHP/data/requireExplicitAssertionErrors.fixed.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,12 @@ function () {
9090
\assert($y2 === true);
9191
};
9292

93+
function () {
94+
$form = $this->formCreator->create(['inputs' => array_map(static function (Config $config): array {
95+
return $config->toArray();
96+
}, $this->config->inputs)]);
97+
\assert($form instanceof Form);
98+
};
99+
93100
$z = new ArrayObject();
94101
\assert($z instanceof \Traversable && $z instanceof \Countable);

tests/Sniffs/PHP/data/requireExplicitAssertionErrors.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,12 @@ function () {
9898
*/
9999
};
100100

101+
function () {
102+
/** @var Form $form */
103+
$form = $this->formCreator->create(['inputs' => array_map(static function (Config $config): array {
104+
return $config->toArray();
105+
}, $this->config->inputs)]);
106+
};
107+
101108
$z = new ArrayObject();
102109
/** @var \Traversable&\Countable $z */

0 commit comments

Comments
 (0)