Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(explicit assertions): enable advanced string types and integer ranges #293

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"require": {
"php": "^7.2 || ^8.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7",
"slevomat/coding-standard": "^8.2",
"slevomat/coding-standard": "^8.5.1",
"squizlabs/php_codesniffer": "^3.7"
},
"config": {
Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,12 @@
<!-- Forbid useless semicolon `;` -->
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
<!-- Require /* @var type $foo */ and similar simple inline annotations to be replaced by assert() -->
<rule ref="SlevomatCodingStandard.PHP.RequireExplicitAssertion"/>
<rule ref="SlevomatCodingStandard.PHP.RequireExplicitAssertion">
<properties>
<property name="enableAdvancedStringTypes" value="true"/>
<property name="enableIntegerRanges" value="true"/>
</properties>
</rule>
<!-- Require use of short versions of scalar types (i.e. int instead of integer) -->
<rule ref="SlevomatCodingStandard.TypeHints.LongTypeHints"/>
<!-- Require the `null` type hint to be in the last position of annotations -->
Expand Down
6 changes: 3 additions & 3 deletions tests/expected_report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tests/input/ExampleBackedEnum.php 3 0
tests/input/Exceptions.php 1 0
tests/input/forbidden-comments.php 14 0
tests/input/forbidden-functions.php 6 0
tests/input/inline_type_hint_assertions.php 7 0
tests/input/inline_type_hint_assertions.php 10 0
tests/input/LowCaseTypes.php 2 0
tests/input/namespaces-spacing.php 7 0
tests/input/NamingCamelCase.php 9 0
Expand Down Expand Up @@ -50,9 +50,9 @@ tests/input/use-ordering.php 1 0
tests/input/useless-semicolon.php 2 0
tests/input/UselessConditions.php 21 0
----------------------------------------------------------------------
A TOTAL OF 432 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
A TOTAL OF 435 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
----------------------------------------------------------------------
PHPCBF CAN FIX 365 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX 368 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


9 changes: 9 additions & 0 deletions tests/fixed/inline_type_hint_assertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@
assert(is_int($multipleScalarTypes) || is_float($multipleScalarTypes) || is_bool($multipleScalarTypes) || is_string($multipleScalarTypes) || is_array($multipleScalarTypes) || $multipleScalarTypes === null);

/** @var Potato $variableThatIsNowhereToBeFound */

$a = 1;
assert(is_int($a) && $a > 0);

$aa = null;
assert((is_int($aa) && $aa > 0) || $aa === null);

$aaa = 'string';
assert(is_string($aaa) && $aaa !== '');
9 changes: 9 additions & 0 deletions tests/input/inline_type_hint_assertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@
$multipleScalarTypes = expression();

/** @var Potato $variableThatIsNowhereToBeFound */

/** @var positive-int $a */
$a = 1;

/** @var positive-int|null $aa */
$aa = null;

/** @var non-empty-string $aaa */
$aaa = 'string';
10 changes: 5 additions & 5 deletions tests/php72-compatibility.patch
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ index 5110131..53dada5 100644
+tests/input/example-class.php 41 0
tests/input/forbidden-comments.php 14 0
tests/input/forbidden-functions.php 6 0
tests/input/inline_type_hint_assertions.php 7 0
tests/input/inline_type_hint_assertions.php 10 0
tests/input/LowCaseTypes.php 2 0
tests/input/namespaces-spacing.php 7 0
-tests/input/NamingCamelCase.php 9 0
Expand Down Expand Up @@ -51,11 +51,11 @@ index 5110131..53dada5 100644
-tests/input/UselessConditions.php 21 0
+tests/input/UselessConditions.php 20 0
----------------------------------------------------------------------
-A TOTAL OF 432 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
+A TOTAL OF 385 ERRORS AND 0 WARNINGS WERE FOUND IN 42 FILES
-A TOTAL OF 435 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
+A TOTAL OF 388 ERRORS AND 0 WARNINGS WERE FOUND IN 42 FILES
----------------------------------------------------------------------
-PHPCBF CAN FIX 365 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 318 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
-PHPCBF CAN FIX 368 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 321 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


Expand Down
10 changes: 5 additions & 5 deletions tests/php73-compatibility.patch
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ index 5110131..5616fdf 100644
+tests/input/example-class.php 41 0
tests/input/forbidden-comments.php 14 0
tests/input/forbidden-functions.php 6 0
tests/input/inline_type_hint_assertions.php 7 0
tests/input/inline_type_hint_assertions.php 10 0
tests/input/LowCaseTypes.php 2 0
tests/input/namespaces-spacing.php 7 0
-tests/input/NamingCamelCase.php 9 0
Expand Down Expand Up @@ -52,11 +52,11 @@ index 5110131..5616fdf 100644
-tests/input/UselessConditions.php 21 0
+tests/input/UselessConditions.php 20 0
----------------------------------------------------------------------
-A TOTAL OF 432 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
+A TOTAL OF 387 ERRORS AND 0 WARNINGS WERE FOUND IN 43 FILES
-A TOTAL OF 435 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
+A TOTAL OF 390 ERRORS AND 0 WARNINGS WERE FOUND IN 43 FILES
----------------------------------------------------------------------
-PHPCBF CAN FIX 365 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 320 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
-PHPCBF CAN FIX 368 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 323 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


Expand Down
10 changes: 5 additions & 5 deletions tests/php74-compatibility.patch
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ index 5110131..dc2cf18 100644
+tests/input/example-class.php 44 0
tests/input/forbidden-comments.php 14 0
tests/input/forbidden-functions.php 6 0
tests/input/inline_type_hint_assertions.php 7 0
tests/input/inline_type_hint_assertions.php 10 0
tests/input/LowCaseTypes.php 2 0
tests/input/namespaces-spacing.php 7 0
-tests/input/NamingCamelCase.php 9 0
Expand Down Expand Up @@ -50,11 +50,11 @@ index 5110131..dc2cf18 100644
-tests/input/UselessConditions.php 21 0
+tests/input/UselessConditions.php 20 0
----------------------------------------------------------------------
-A TOTAL OF 432 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
+A TOTAL OF 396 ERRORS AND 0 WARNINGS WERE FOUND IN 43 FILES
-A TOTAL OF 435 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
+A TOTAL OF 399 ERRORS AND 0 WARNINGS WERE FOUND IN 43 FILES
----------------------------------------------------------------------
-PHPCBF CAN FIX 365 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 329 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
-PHPCBF CAN FIX 368 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 332 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


Expand Down
8 changes: 4 additions & 4 deletions tests/php80-compatibility.patch
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ index 5110131..f0a3fc9 100644
tests/input/useless-semicolon.php 2 0
tests/input/UselessConditions.php 21 0
----------------------------------------------------------------------
-A TOTAL OF 432 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
+A TOTAL OF 426 ERRORS AND 0 WARNINGS WERE FOUND IN 45 FILES
-A TOTAL OF 435 ERRORS AND 0 WARNINGS WERE FOUND IN 46 FILES
+A TOTAL OF 429 ERRORS AND 0 WARNINGS WERE FOUND IN 45 FILES
----------------------------------------------------------------------
-PHPCBF CAN FIX 365 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 359 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
-PHPCBF CAN FIX 368 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 362 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


Expand Down