Completed
Push — master ( 82d1fc...6f9908 )
by Ivannis Suárez
02:50 queued 29s
created

SmartExpressionToStringConverter::visitValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Visitor\Tests\Fixtures;
12
13
use Cubiche\Core\Visitor\Visitor;
14
15
/**
16
 * Smart Expression to String Converter Class.
17
 *
18
 * @author Karel Osorio Ramírez <[email protected]>
19
 */
20
class SmartExpressionToStringConverter extends ExpressionToStringConverter
21
{
22
    /**
23
     * @param Operator $op
24
     *
25
     * @return string
26
     */
27
    public function visitOperator(Operator $op, $parentOperator = null)
28
    {
29
        $currentOperator = $op->operator();
30
        $expression = $op->firstOperand()->accept($this, $currentOperator).
31
            $currentOperator.$op->secondOperand()->accept($this, $currentOperator);
32
33
        return $this->requireParentheses($currentOperator, $parentOperator) ? '('.$expression.')' : $expression;
34
    }
35
36
    /**
37
     * @param string $currentOperator
38
     * @param string $parentOperator
39
     *
40
     * @return bool
41
     */
42
    protected function requireParentheses($currentOperator, $parentOperator)
43
    {
44
        return $currentOperator === '+' && $parentOperator === '*';
45
    }
46
}
47