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

Replace inline docblock declarations with assertions #47

Merged
merged 5 commits into from
Apr 30, 2019
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 @@ -28,7 +28,7 @@
"require": {
"php": "^7.1",
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
"slevomat/coding-standard": "^5.0",
"slevomat/coding-standard": "dev-master#63a8186b129ee96d1277e68c80cf87c8cdb356d1",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@malarzm @deeky666 I'm likely raising a separate PR for stable slevomat release pinning by @kukulich.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC pinning a commit could lead to weird side effects where we can end up with vendor requirements from current dev-master while code in pinned commit could require something else.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes: this is not for a stable release yet. I'd need a SemVer selector for the release to be tagged.

"squizlabs/php_codesniffer": "^3.4.0"
},
"config": {
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@
<rule ref="SlevomatCodingStandard.PHP.UselessParentheses"/>
<!-- Forbid useless semicolon `;` -->
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
<!-- Require /* @var type $foo */ and similar simple inline annotations to be replaced by assert() -->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this sniff support /* @var ArrayIterator $iterator */ with one * too like in this comment? IDEs like PHPStorm support those too, even though it doesn't autocomplete on "/*". In case it supports the single-*, some tests for those would be nice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not. However it is reported (and fixed) by

<rule ref="SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration"/>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that both can be used? The first one fixing a /* and the other one handles it for the assert()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A CS rule enforcing /** style would be preferable: please note that current static analysis tools fail to parse /* AFAIK

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InvalidDocCommentDeclaration fixes /* to /** in first round and in second round RequireExplictionAssertion changes doccomment to assertion :)

Copy link

@DASPRiD DASPRiD Apr 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A CS rule enforcing /** style would be preferable: please note that current static analysis tools fail to parse /* AFAIK

Last time I used NetBeans (granted, a while ago), it only supported vardoc syntax (/*) but not /**.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That changed a long time ago: at the moment, the ecosystem is pushing for /**, and enabling that sniff could put a tombstone on this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ocramius Not sure I undrestand you well but the InvalidInlineDocCommentDeclaration is already enabled ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kukulich yeah, this was mostly for @DASPRiD 👍

<rule ref="SlevomatCodingStandard.PHP.RequireExplicitAssertion"/>
<!-- 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
5 changes: 3 additions & 2 deletions tests/expected_report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tests/input/EarlyReturn.php 6 0
tests/input/example-class.php 33 0
tests/input/forbidden-comments.php 8 0
tests/input/forbidden-functions.php 6 0
tests/input/inline_type_hint_assertions.php 6 0
tests/input/LowCaseTypes.php 2 0
tests/input/namespaces-spacing.php 7 0
tests/input/new_with_parentheses.php 18 0
Expand All @@ -34,9 +35,9 @@ tests/input/UnusedVariables.php 1 0
tests/input/useless-semicolon.php 2 0
tests/input/UselessConditions.php 20 0
----------------------------------------------------------------------
A TOTAL OF 258 ERRORS AND 0 WARNINGS WERE FOUND IN 30 FILES
A TOTAL OF 264 ERRORS AND 0 WARNINGS WERE FOUND IN 31 FILES
----------------------------------------------------------------------
PHPCBF CAN FIX 210 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX 216 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


26 changes: 26 additions & 0 deletions tests/fixed/inline_type_hint_assertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

$simpleType = expression();
assert($simpleType instanceof Type);

$typeDeclaredAfterExpression = expression();
assert($typeDeclaredAfterExpression instanceof Type);

$typeDeclaredViaAssertion = expression();
assert($typeDeclaredViaAssertion instanceof Type);

$unionType = expression();
assert($unionType instanceof Type1 || $unionType instanceof Type2);

$intersectionType = expression();
assert($intersectionType instanceof Type1 && $intersectionType instanceof Type2);

$nullableType = expression();
assert($nullableType instanceof Type || $nullableType === null);

$multipleScalarTypes = expression();
assert(is_int($multipleScalarTypes) || is_float($multipleScalarTypes) || is_bool($multipleScalarTypes) || is_string($multipleScalarTypes) || is_array($multipleScalarTypes) || $multipleScalarTypes === null);

/** @var Potato $variableThatIsNowhereToBeFound */
26 changes: 26 additions & 0 deletions tests/input/inline_type_hint_assertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/** @var Type $simpleType */
$simpleType = expression();

$typeDeclaredAfterExpression = expression();
/** @var Type $typeDeclaredAfterExpression */

$typeDeclaredViaAssertion = expression();
assert($typeDeclaredViaAssertion instanceof Type);

/** @var Type1|Type2 $unionType */
$unionType = expression();

/** @var Type1&Type2 $intersectionType */
$intersectionType = expression();

/** @var Type|null $nullableType */
$nullableType = expression();

/** @var int|float|bool|string|array|null $multipleScalarTypes */
$multipleScalarTypes = expression();

/** @var Potato $variableThatIsNowhereToBeFound */