Generic/RequireStrictTypes: various bugfixes #3720
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Generic/RequireStrictTypes: add extra tests
These tests safeguard the following, which is already handled correctly by the sniff:
Generic/RequireStrictTypes: bug fix - limit token search to the statement
While hopefully unlikely in real life, the sniff should handle parse errors correctly.
Along the same lines, live coding should be handled correctly and a
declare()
statement should only be examined once the statement has been completed.This updates the sniff to handle both situations correctly.
Without these fixes, no error would be thrown in test case file 5 or 6 (false negative), while an error would be thrown in test case file 7 (false positive).
Includes unit tests.
Includes minor efficiency tweak to only start looking for a "next" token after the current token.
Generic/RequireStrictTypes: bug fix - allow for multi directive statements
PHP allows for multiple directives to be passed in one
declare()
statement.The sniff, however, did not allow for this, which could lead to false positives.
Fixed now.
Includes unit tests.
Generic/RequireStrictTypes: add warning for when value is 0
Strict types is disabled when the value for the
strict_types
directive is0
.As this sniff is supposed to be about enforcing the use of
strict_types
,strict_types
declarations which turn the feature off should be flagged as well.Implemented with a separate error code to allow for selectively turning this warning off.
Includes dedicated tests.
Future Scope
This sniff isn't very efficient as it will walk the whole file until a
declare()
statement is found or until the end of the file is reached.Declare statements for
strict_types
MUST be the first statement in a file, so IMO there are two options:declare()
statement and flag anystrict_types
declarations which are not the first statement in a file with a separate error code.In both cases, "view" files containing a mix of PHP and HTML should be taken into account, similarly, hashbang lines should be taken into account.