Skip to content

Commit baff79f

Browse files
authored
Merge pull request #293 from simPod/assert
feat(explicit assertions): enable advanced string types and integer ranges
2 parents 6db7d78 + c905749 commit baff79f

File tree

9 files changed

+47
-24
lines changed

9 files changed

+47
-24
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"require": {
2424
"php": "^7.2 || ^8.0",
2525
"dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7",
26-
"slevomat/coding-standard": "^8.2",
26+
"slevomat/coding-standard": "^8.5.1",
2727
"squizlabs/php_codesniffer": "^3.7"
2828
},
2929
"config": {

lib/Doctrine/ruleset.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,12 @@
411411
<!-- Forbid useless semicolon `;` -->
412412
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
413413
<!-- Require /* @var type $foo */ and similar simple inline annotations to be replaced by assert() -->
414-
<rule ref="SlevomatCodingStandard.PHP.RequireExplicitAssertion"/>
414+
<rule ref="SlevomatCodingStandard.PHP.RequireExplicitAssertion">
415+
<properties>
416+
<property name="enableAdvancedStringTypes" value="true"/>
417+
<property name="enableIntegerRanges" value="true"/>
418+
</properties>
419+
</rule>
415420
<!-- Require use of short versions of scalar types (i.e. int instead of integer) -->
416421
<rule ref="SlevomatCodingStandard.TypeHints.LongTypeHints"/>
417422
<!-- Require the `null` type hint to be in the last position of annotations -->

tests/expected_report.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tests/input/ExampleBackedEnum.php 3 0
2020
tests/input/Exceptions.php 1 0
2121
tests/input/forbidden-comments.php 14 0
2222
tests/input/forbidden-functions.php 6 0
23-
tests/input/inline_type_hint_assertions.php 7 0
23+
tests/input/inline_type_hint_assertions.php 10 0
2424
tests/input/LowCaseTypes.php 2 0
2525
tests/input/namespaces-spacing.php 7 0
2626
tests/input/NamingCamelCase.php 9 0
@@ -50,9 +50,9 @@ tests/input/use-ordering.php 1 0
5050
tests/input/useless-semicolon.php 2 0
5151
tests/input/UselessConditions.php 21 0
5252
----------------------------------------------------------------------
53-
A TOTAL OF 432 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
53+
A TOTAL OF 435 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
5454
----------------------------------------------------------------------
55-
PHPCBF CAN FIX 365 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
55+
PHPCBF CAN FIX 368 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
5656
----------------------------------------------------------------------
5757

5858

tests/fixed/inline_type_hint_assertions.php

Lines changed: 9 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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';

tests/php72-compatibility.patch

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ index 5110131..53dada5 100644
1414
+tests/input/example-class.php 41 0
1515
tests/input/forbidden-comments.php 14 0
1616
tests/input/forbidden-functions.php 6 0
17-
tests/input/inline_type_hint_assertions.php 7 0
17+
tests/input/inline_type_hint_assertions.php 10 0
1818
tests/input/LowCaseTypes.php 2 0
1919
tests/input/namespaces-spacing.php 7 0
2020
-tests/input/NamingCamelCase.php 9 0
@@ -51,11 +51,11 @@ index 5110131..53dada5 100644
5151
-tests/input/UselessConditions.php 21 0
5252
+tests/input/UselessConditions.php 20 0
5353
----------------------------------------------------------------------
54-
-A TOTAL OF 432 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
55-
+A TOTAL OF 385 ERRORS AND 0 WARNINGS WERE FOUND IN 42 FILES
54+
-A TOTAL OF 435 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
55+
+A TOTAL OF 388 ERRORS AND 0 WARNINGS WERE FOUND IN 42 FILES
5656
----------------------------------------------------------------------
57-
-PHPCBF CAN FIX 365 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
58-
+PHPCBF CAN FIX 318 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
57+
-PHPCBF CAN FIX 368 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
58+
+PHPCBF CAN FIX 321 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
5959
----------------------------------------------------------------------
6060

6161

tests/php73-compatibility.patch

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ index 5110131..5616fdf 100644
1414
+tests/input/example-class.php 41 0
1515
tests/input/forbidden-comments.php 14 0
1616
tests/input/forbidden-functions.php 6 0
17-
tests/input/inline_type_hint_assertions.php 7 0
17+
tests/input/inline_type_hint_assertions.php 10 0
1818
tests/input/LowCaseTypes.php 2 0
1919
tests/input/namespaces-spacing.php 7 0
2020
-tests/input/NamingCamelCase.php 9 0
@@ -52,11 +52,11 @@ index 5110131..5616fdf 100644
5252
-tests/input/UselessConditions.php 21 0
5353
+tests/input/UselessConditions.php 20 0
5454
----------------------------------------------------------------------
55-
-A TOTAL OF 432 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
56-
+A TOTAL OF 387 ERRORS AND 0 WARNINGS WERE FOUND IN 43 FILES
55+
-A TOTAL OF 435 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
56+
+A TOTAL OF 390 ERRORS AND 0 WARNINGS WERE FOUND IN 43 FILES
5757
----------------------------------------------------------------------
58-
-PHPCBF CAN FIX 365 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
59-
+PHPCBF CAN FIX 320 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
58+
-PHPCBF CAN FIX 368 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
59+
+PHPCBF CAN FIX 323 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
6060
----------------------------------------------------------------------
6161

6262

tests/php74-compatibility.patch

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ index 5110131..dc2cf18 100644
1414
+tests/input/example-class.php 44 0
1515
tests/input/forbidden-comments.php 14 0
1616
tests/input/forbidden-functions.php 6 0
17-
tests/input/inline_type_hint_assertions.php 7 0
17+
tests/input/inline_type_hint_assertions.php 10 0
1818
tests/input/LowCaseTypes.php 2 0
1919
tests/input/namespaces-spacing.php 7 0
2020
-tests/input/NamingCamelCase.php 9 0
@@ -50,11 +50,11 @@ index 5110131..dc2cf18 100644
5050
-tests/input/UselessConditions.php 21 0
5151
+tests/input/UselessConditions.php 20 0
5252
----------------------------------------------------------------------
53-
-A TOTAL OF 432 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
54-
+A TOTAL OF 396 ERRORS AND 0 WARNINGS WERE FOUND IN 43 FILES
53+
-A TOTAL OF 435 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
54+
+A TOTAL OF 399 ERRORS AND 0 WARNINGS WERE FOUND IN 43 FILES
5555
----------------------------------------------------------------------
56-
-PHPCBF CAN FIX 365 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
57-
+PHPCBF CAN FIX 329 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
56+
-PHPCBF CAN FIX 368 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
57+
+PHPCBF CAN FIX 332 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
5858
----------------------------------------------------------------------
5959

6060

tests/php80-compatibility.patch

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ index 5110131..f0a3fc9 100644
2323
tests/input/useless-semicolon.php 2 0
2424
tests/input/UselessConditions.php 21 0
2525
----------------------------------------------------------------------
26-
-A TOTAL OF 432 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
27-
+A TOTAL OF 426 ERRORS AND 0 WARNINGS WERE FOUND IN 45 FILES
26+
-A TOTAL OF 435 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
27+
+A TOTAL OF 429 ERRORS AND 0 WARNINGS WERE FOUND IN 45 FILES
2828
----------------------------------------------------------------------
29-
-PHPCBF CAN FIX 365 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
30-
+PHPCBF CAN FIX 359 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
29+
-PHPCBF CAN FIX 368 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
30+
+PHPCBF CAN FIX 362 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
3131
----------------------------------------------------------------------
3232

3333

0 commit comments

Comments
 (0)