File tree Expand file tree Collapse file tree
rules-tests/CodeQuality/Rector/BooleanNot/SimplifyDeMorganBinaryRector/Fixture
rules/CodeQuality/Rector/BooleanNot Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \CodeQuality \Rector \BooleanNot \SimplifyDeMorganBinaryRector \Fixture ;
4+
5+ class BinaryAnd
6+ {
7+ public static function validateAssetDownload ($ assetId , array $ limitToAssets ): bool
8+ {
9+ return !(!empty ($ limitToAssets ) && !in_array ($ assetId , $ limitToAssets ));
10+ }
11+ }
12+
13+ ?>
14+ -----
15+ <?php
16+
17+ namespace Rector \Tests \CodeQuality \Rector \BooleanNot \SimplifyDeMorganBinaryRector \Fixture ;
18+
19+ class BinaryAnd
20+ {
21+ public static function validateAssetDownload ($ assetId , array $ limitToAssets ): bool
22+ {
23+ return empty ($ limitToAssets ) || in_array ($ assetId , $ limitToAssets );
24+ }
25+ }
26+
27+ ?>
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ function deMorgan()
2626
2727 $ c = 1000 ;
2828 $ d = $ a !== null && $ b >= $ c ;
29- $ d = !( $ a === null && $ b < $ c) ;
29+ $ d = $ a !== null || $ b >= $ c ;
3030
3131 $ e = $ a && $ b === $ c ;
3232 $ f = $ a <= $ b && $ c > $ d ;
Original file line number Diff line number Diff line change 66
77use PhpParser \Node ;
88use PhpParser \Node \Expr \BinaryOp ;
9+ use PhpParser \Node \Expr \BinaryOp \BooleanAnd ;
910use PhpParser \Node \Expr \BinaryOp \BooleanOr ;
1011use PhpParser \Node \Expr \BooleanNot ;
1112use Rector \NodeManipulator \BinaryOpManipulator ;
@@ -58,10 +59,14 @@ public function getNodeTypes(): array
5859 */
5960 public function refactor (Node $ node ): ?BinaryOp
6061 {
61- if (! $ node ->expr instanceof BooleanOr) {
62- return null ;
62+ if ($ node ->expr instanceof BooleanOr) {
63+ return $ this -> binaryOpManipulator -> inverseBooleanOr ( $ node -> expr ) ;
6364 }
6465
65- return $ this ->binaryOpManipulator ->inverseBooleanOr ($ node ->expr );
66+ if ($ node ->expr instanceof BooleanAnd) {
67+ return $ this ->binaryOpManipulator ->inverseBooleanAnd ($ node ->expr );
68+ }
69+
70+ return null ;
6671 }
6772}
Original file line number Diff line number Diff line change @@ -76,6 +76,23 @@ public function inverseBooleanOr(BooleanOr $booleanOr): ?BinaryOp
7676 return new $ inversedNodeClass ($ firstInversedExpr , $ secondInversedExpr );
7777 }
7878
79+ public function inverseBooleanAnd (BooleanAnd $ booleanAnd ): ?BinaryOp
80+ {
81+ // no nesting
82+ if ($ booleanAnd ->left instanceof BooleanAnd) {
83+ return null ;
84+ }
85+
86+ if ($ booleanAnd ->right instanceof BooleanAnd) {
87+ return null ;
88+ }
89+
90+ $ firstInversedExpr = $ this ->inverseNode ($ booleanAnd ->left );
91+ $ secondInversedExpr = $ this ->inverseNode ($ booleanAnd ->right );
92+
93+ return new BooleanOr ($ firstInversedExpr , $ secondInversedExpr );
94+ }
95+
7996 public function invertCondition (BinaryOp $ binaryOp ): ?BinaryOp
8097 {
8198 // no nesting
You can’t perform that action at this time.
0 commit comments