Skip to content

Commit

Permalink
Merge branch 'allowOnly' of https://github.com/VincentLanglet/PHP_Cod…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Jan 6, 2020
2 parents 77c5fa2 + 0334ea2 commit 37b0cb1
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
class BooleanOperatorPlacementSniff implements Sniff
{

/**
* Used to restrict the placement of the boolean operator
* Allowed value are 'first' or 'last'
*
* @var string|null
*/
public $allowOnly = null;


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -65,8 +73,13 @@ public function process(File $phpcsFile, $stackPtr)
T_BOOLEAN_OR,
];

if ($this->allowOnly === 'first' || $this->allowOnly === 'last') {
$position = $this->allowOnly;
} else {
$position = null;
}

$operator = $parenOpener;
$position = null;
$error = false;
$operators = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,49 @@ if (
) {
return $n;
}

// phpcs:set PSR12.ControlStructures.BooleanOperatorPlacement allowOnly first
if (
$expr1
&& $expr2
&& ($expr3
|| $expr4)
&& $expr5
) {
// if body
} elseif (
$expr1 &&
($expr3 || $expr4)
&& $expr5
) {
// elseif body
} elseif (
$expr1
&& ($expr3 || $expr4) &&
$expr5
) {
// elseif body
}

// phpcs:set PSR12.ControlStructures.BooleanOperatorPlacement allowOnly last
if (
$expr1
&& $expr2
&& ($expr3
|| $expr4)
&& $expr5
) {
// if body
} elseif (
$expr1 &&
($expr3 || $expr4)
&& $expr5
) {
// elseif body
} elseif (
$expr1
&& ($expr3 || $expr4) &&
$expr5
) {
// elseif body
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,53 @@ if (
) {
return $n;
}

// phpcs:set PSR12.ControlStructures.BooleanOperatorPlacement allowOnly first
if (
$expr1
&& $expr2
&& ($expr3
|| $expr4)
&& $expr5
) {
// if body
} elseif (
$expr1
&& ($expr3
|| $expr4)
&& $expr5
) {
// elseif body
} elseif (
$expr1
&& ($expr3
|| $expr4)
&& $expr5
) {
// elseif body
}

// phpcs:set PSR12.ControlStructures.BooleanOperatorPlacement allowOnly last
if (
$expr1 &&
$expr2 &&
($expr3 ||
$expr4) &&
$expr5
) {
// if body
} elseif (
$expr1 &&
($expr3 ||
$expr4) &&
$expr5
) {
// elseif body
} elseif (
$expr1 &&
($expr3 ||
$expr4) &&
$expr5
) {
// elseif body
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ class BooleanOperatorPlacementUnitTest extends AbstractSniffUnitTest
public function getErrorList()
{
return [
10 => 1,
16 => 1,
28 => 1,
34 => 1,
10 => 1,
16 => 1,
28 => 1,
34 => 1,
75 => 1,
81 => 1,
90 => 1,
98 => 1,
104 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 37b0cb1

Please sign in to comment.