Skip to content

Add sniff to ensure spaces around binary operators #126

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions lib/Doctrine/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>
<!-- Require spaces around binary operators -->
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
<properties>
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>
<!-- Forbid spaces before semicolon `;` -->
<rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>
<!-- Forbid superfluous whitespaces -->
Expand Down
13 changes: 7 additions & 6 deletions tests/expected_report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ FILE ERRORS WARNINGS
----------------------------------------------------------------------
tests/input/array_indentation.php 10 0
tests/input/assignment-operators.php 4 0
tests/input/binary_operators.php 9 0
tests/input/class-references.php 10 0
tests/input/concatenation_spacing.php 24 0
tests/input/concatenation_spacing.php 27 0
tests/input/constants-no-lsb.php 2 0
tests/input/constants-var.php 3 0
tests/input/doc-comment-spacing.php 10 0
Expand All @@ -19,25 +20,25 @@ 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
tests/input/not_spacing.php 8 0
tests/input/not_spacing.php 9 0
tests/input/null_coalesce_operator.php 3 0
tests/input/optimized-functions.php 1 0
tests/input/return_type_on_closures.php 21 0
tests/input/return_type_on_methods.php 17 0
tests/input/semicolon_spacing.php 3 0
tests/input/semicolon_spacing.php 4 0
tests/input/spread-operator.php 6 0
tests/input/static-closures.php 1 0
tests/input/superfluous-naming.php 11 0
tests/input/test-case.php 8 0
tests/input/trailing_comma_on_array.php 1 0
tests/input/trailing_comma_on_array.php 2 0
tests/input/traits-uses.php 11 0
tests/input/UnusedVariables.php 1 0
tests/input/useless-semicolon.php 2 0
tests/input/UselessConditions.php 20 0
----------------------------------------------------------------------
A TOTAL OF 264 ERRORS AND 0 WARNINGS WERE FOUND IN 31 FILES
A TOTAL OF 279 ERRORS AND 0 WARNINGS WERE FOUND IN 32 FILES
----------------------------------------------------------------------
PHPCBF CAN FIX 216 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX 231 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


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

declare(strict_types=1);

if ($foo === $bar) {
return $bar + $foo;
}

for ($i = 0; $i < 100; $i++) {
echo $i >= 10;
}
4 changes: 2 additions & 2 deletions tests/fixed/concatenation_spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

$string = $doctrine . $rulez . 'even' . 'more' . $string;

$string .=$doctrine;
$string .= $doctrine;
$string .=$doctrine;
$string .= $doctrine;
$string .= $doctrine;

$string = $doctrine .
$rulez .
Expand Down
2 changes: 1 addition & 1 deletion tests/fixed/not_spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
} elseif (! $test === 0) {
echo 0;
} else {
echo -1;
echo - 1;
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a bug. It's a unary minus, should not be affected by the rule.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

while (! true) {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixed/semicolon_spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

echo -1;
echo - 1;
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a bug. It's a unary minus, should not be affected by the rule.


echo 'foo';

Expand Down
2 changes: 1 addition & 1 deletion tests/fixed/trailing_comma_on_array.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

$array = [
$array = [
'key1' => 'value',
'key2' => 'value',
];
11 changes: 11 additions & 0 deletions tests/input/binary_operators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

if ($foo===$bar) {
return $bar+$foo;
}

for ($i=0; $i<100; $i++) {
echo $i >=10;
}