Skip to content

Commit 78985a6

Browse files
committed
Enforce explicit assertions for advanced string types and int ranges
1 parent d128114 commit 78985a6

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

src/Unleashed/ruleset.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,12 @@
497497
<!-- Forbid useless semicolon `;` -->
498498
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
499499
<!-- Require /* @var type $foo */ and similar simple inline annotations to be replaced by assert() -->
500-
<rule ref="SlevomatCodingStandard.PHP.RequireExplicitAssertion"/>
500+
<rule ref="SlevomatCodingStandard.PHP.RequireExplicitAssertion">
501+
<properties>
502+
<property name="enableAdvancedStringTypes" value="true"/>
503+
<property name="enableIntegerRanges" value="true"/>
504+
</properties>
505+
</rule>
501506
<!-- Require use of short versions of scalar types (i.e. int instead of integer) -->
502507
<rule ref="SlevomatCodingStandard.TypeHints.LongTypeHints"/>
503508
<!-- Require the `null` type hint to be in the last position of annotations -->

tests/expected_report.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ tests/input/ForbiddenClasses.php 7 0
2525
tests/input/fully-qualified-and-fallbacks.php 2 0
2626
tests/input/fully-qualified-without-namespace.php 4 0
2727
tests/input/inheritdoc.php 12 1
28-
tests/input/inline_type_hint_assertions.php 7 0
28+
tests/input/inline_type_hint_assertions.php 10 0
2929
tests/input/LowCaseTypes.php 3 0
3030
tests/input/merge-conflict.php 6 0
3131
tests/input/namespaces-spacing.php 13 0
@@ -55,9 +55,9 @@ tests/input/use-ordering.php 9 0
5555
tests/input/useless-semicolon.php 2 0
5656
tests/input/UselessConditions.php 24 0
5757
----------------------------------------------------------------------
58-
A TOTAL OF 518 ERRORS AND 9 WARNINGS WERE FOUND IN 51 FILES
58+
A TOTAL OF 521 ERRORS AND 9 WARNINGS WERE FOUND IN 51 FILES
5959
----------------------------------------------------------------------
60-
PHPCBF CAN FIX 415 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
60+
PHPCBF CAN FIX 418 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
6161
----------------------------------------------------------------------
6262

6363

tests/fixed/inline_type_hint_assertions.php

+9
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@
2424
assert(is_int($multipleScalarTypes) || is_float($multipleScalarTypes) || is_bool($multipleScalarTypes) || is_string($multipleScalarTypes) || is_array($multipleScalarTypes) || $multipleScalarTypes === null);
2525

2626
/** @var Potato $variableThatIsNowhereToBeFound */
27+
28+
$a = 1;
29+
assert(is_int($a) && $a > 0);
30+
31+
$aa = null;
32+
assert((is_int($aa) && $aa > 0) || $aa === null);
33+
34+
$aaa = 'string';
35+
assert(is_string($aaa) && $aaa !== '');

tests/input/inline_type_hint_assertions.php

+9
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@
2424
$multipleScalarTypes = expression();
2525

2626
/** @var Potato $variableThatIsNowhereToBeFound */
27+
28+
/** @var positive-int $a */
29+
$a = 1;
30+
31+
/** @var positive-int|null $aa */
32+
$aa = null;
33+
34+
/** @var non-empty-string $aaa */
35+
$aaa = 'string';

0 commit comments

Comments
 (0)