Skip to content

Commit 3ee0fdf

Browse files
committed
booleanLogicalOperators option to get PHP behavior on logical operators
1 parent 3b1c99a commit 3ee0fdf

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/JsPhpize/Compiler/Compiler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,16 @@ protected function visitDyiade(Dyiade $dyiade, $indent)
155155
$rightHand = $this->visitNode($dyiade->rightHand, $indent);
156156
switch ($dyiade->operator) {
157157
case '||':
158+
if ($this->engine->getOption('booleanLogicalOperators')) {
159+
break;
160+
}
161+
158162
return $this->compileLazyDyiade($this->engine->getHelperName('or'), $leftHand, $rightHand);
159163
case '&&':
164+
if ($this->engine->getOption('booleanLogicalOperators')) {
165+
break;
166+
}
167+
160168
return $this->compileLazyDyiade($this->engine->getHelperName('and'), $leftHand, $rightHand);
161169
case '+':
162170
$arguments = [$leftHand, $rightHand];

tests/options.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,26 @@ public function testDisableConstants()
147147
]);
148148
self::assertSame('$FOO', trim($jsPhpize->compile('FOO'), " \n;"));
149149
}
150+
151+
/**
152+
* @throws \JsPhpize\Compiler\Exception
153+
* @throws \JsPhpize\Lexer\Exception
154+
* @throws \JsPhpize\Parser\Exception
155+
*/
156+
public function testBooleanLogicalOperators()
157+
{
158+
$jsPhpize = new JsPhpize();
159+
$this->assertSame(4, $jsPhpize->render('return 7 && 4'));
160+
$this->assertSame(7, $jsPhpize->render('return 7 || 4'));
161+
$this->assertSame(null, $jsPhpize->render('return null && false'));
162+
$this->assertSame(null, $jsPhpize->render('return 0 || null'));
163+
164+
$jsPhpize = new JsPhpize([
165+
'booleanLogicalOperators' => true,
166+
]);
167+
$this->assertSame(true, $jsPhpize->render('return 7 && 4'));
168+
$this->assertSame(true, $jsPhpize->render('return 7 || 4'));
169+
$this->assertSame(false, $jsPhpize->render('return null && false'));
170+
$this->assertSame(false, $jsPhpize->render('return 0 || null'));
171+
}
150172
}

0 commit comments

Comments
 (0)