Skip to content

Commit

Permalink
booleanLogicalOperators option to get PHP behavior on logical operators
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Oct 18, 2018
1 parent 3b1c99a commit 3ee0fdf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/JsPhpize/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,16 @@ protected function visitDyiade(Dyiade $dyiade, $indent)
$rightHand = $this->visitNode($dyiade->rightHand, $indent);
switch ($dyiade->operator) {
case '||':
if ($this->engine->getOption('booleanLogicalOperators')) {
break;
}

return $this->compileLazyDyiade($this->engine->getHelperName('or'), $leftHand, $rightHand);
case '&&':
if ($this->engine->getOption('booleanLogicalOperators')) {
break;
}

return $this->compileLazyDyiade($this->engine->getHelperName('and'), $leftHand, $rightHand);
case '+':
$arguments = [$leftHand, $rightHand];
Expand Down
22 changes: 22 additions & 0 deletions tests/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,26 @@ public function testDisableConstants()
]);
self::assertSame('$FOO', trim($jsPhpize->compile('FOO'), " \n;"));
}

/**
* @throws \JsPhpize\Compiler\Exception
* @throws \JsPhpize\Lexer\Exception
* @throws \JsPhpize\Parser\Exception
*/
public function testBooleanLogicalOperators()
{
$jsPhpize = new JsPhpize();
$this->assertSame(4, $jsPhpize->render('return 7 && 4'));
$this->assertSame(7, $jsPhpize->render('return 7 || 4'));
$this->assertSame(null, $jsPhpize->render('return null && false'));
$this->assertSame(null, $jsPhpize->render('return 0 || null'));

$jsPhpize = new JsPhpize([
'booleanLogicalOperators' => true,
]);
$this->assertSame(true, $jsPhpize->render('return 7 && 4'));
$this->assertSame(true, $jsPhpize->render('return 7 || 4'));
$this->assertSame(false, $jsPhpize->render('return null && false'));
$this->assertSame(false, $jsPhpize->render('return 0 || null'));
}
}

0 comments on commit 3ee0fdf

Please sign in to comment.