Skip to content

Commit 37b0cb1

Browse files
committed
2 parents 77c5fa2 + 0334ea2 commit 37b0cb1

File tree

4 files changed

+119
-5
lines changed

4 files changed

+119
-5
lines changed

src/Standards/PSR12/Sniffs/ControlStructures/BooleanOperatorPlacementSniff.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
class BooleanOperatorPlacementSniff implements Sniff
1616
{
1717

18+
/**
19+
* Used to restrict the placement of the boolean operator
20+
* Allowed value are 'first' or 'last'
21+
*
22+
* @var string|null
23+
*/
24+
public $allowOnly = null;
25+
1826

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

76+
if ($this->allowOnly === 'first' || $this->allowOnly === 'last') {
77+
$position = $this->allowOnly;
78+
} else {
79+
$position = null;
80+
}
81+
6882
$operator = $parenOpener;
69-
$position = null;
7083
$error = false;
7184
$operators = [];
7285

src/Standards/PSR12/Tests/ControlStructures/BooleanOperatorPlacementUnitTest.inc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,49 @@ if (
6262
) {
6363
return $n;
6464
}
65+
66+
// phpcs:set PSR12.ControlStructures.BooleanOperatorPlacement allowOnly first
67+
if (
68+
$expr1
69+
&& $expr2
70+
&& ($expr3
71+
|| $expr4)
72+
&& $expr5
73+
) {
74+
// if body
75+
} elseif (
76+
$expr1 &&
77+
($expr3 || $expr4)
78+
&& $expr5
79+
) {
80+
// elseif body
81+
} elseif (
82+
$expr1
83+
&& ($expr3 || $expr4) &&
84+
$expr5
85+
) {
86+
// elseif body
87+
}
88+
89+
// phpcs:set PSR12.ControlStructures.BooleanOperatorPlacement allowOnly last
90+
if (
91+
$expr1
92+
&& $expr2
93+
&& ($expr3
94+
|| $expr4)
95+
&& $expr5
96+
) {
97+
// if body
98+
} elseif (
99+
$expr1 &&
100+
($expr3 || $expr4)
101+
&& $expr5
102+
) {
103+
// elseif body
104+
} elseif (
105+
$expr1
106+
&& ($expr3 || $expr4) &&
107+
$expr5
108+
) {
109+
// elseif body
110+
}

src/Standards/PSR12/Tests/ControlStructures/BooleanOperatorPlacementUnitTest.inc.fixed

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,53 @@ if (
6868
) {
6969
return $n;
7070
}
71+
72+
// phpcs:set PSR12.ControlStructures.BooleanOperatorPlacement allowOnly first
73+
if (
74+
$expr1
75+
&& $expr2
76+
&& ($expr3
77+
|| $expr4)
78+
&& $expr5
79+
) {
80+
// if body
81+
} elseif (
82+
$expr1
83+
&& ($expr3
84+
|| $expr4)
85+
&& $expr5
86+
) {
87+
// elseif body
88+
} elseif (
89+
$expr1
90+
&& ($expr3
91+
|| $expr4)
92+
&& $expr5
93+
) {
94+
// elseif body
95+
}
96+
97+
// phpcs:set PSR12.ControlStructures.BooleanOperatorPlacement allowOnly last
98+
if (
99+
$expr1 &&
100+
$expr2 &&
101+
($expr3 ||
102+
$expr4) &&
103+
$expr5
104+
) {
105+
// if body
106+
} elseif (
107+
$expr1 &&
108+
($expr3 ||
109+
$expr4) &&
110+
$expr5
111+
) {
112+
// elseif body
113+
} elseif (
114+
$expr1 &&
115+
($expr3 ||
116+
$expr4) &&
117+
$expr5
118+
) {
119+
// elseif body
120+
}

src/Standards/PSR12/Tests/ControlStructures/BooleanOperatorPlacementUnitTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ class BooleanOperatorPlacementUnitTest extends AbstractSniffUnitTest
2626
public function getErrorList()
2727
{
2828
return [
29-
10 => 1,
30-
16 => 1,
31-
28 => 1,
32-
34 => 1,
29+
10 => 1,
30+
16 => 1,
31+
28 => 1,
32+
34 => 1,
33+
75 => 1,
34+
81 => 1,
35+
90 => 1,
36+
98 => 1,
37+
104 => 1,
3338
];
3439

3540
}//end getErrorList()

0 commit comments

Comments
 (0)