Expression::evalExpression()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace Health\Checks\Php;
3
4
use Health\Checks\BaseCheck;
5
use Health\Checks\HealthCheckInterface;
6
7
class Expression extends BaseCheck implements HealthCheckInterface
8
{
9
10
    /**
11
     *
12
     * {@inheritdoc}
13
     * @see \Health\Checks\HealthCheckInterface::call()
14
     */
15
    public function call()
16
    {
17
        $builder = $this->getBuilder();
18
19
        $expression = $this->getParam('expression', '1==1');
20
        $result = $this->getParam('result', true);
21
22
        $expResult = $this->evalExpression($expression);
23
24
        if ((is_bool($result) && $result !== (bool) $expResult) || ! preg_match("|{$result}|", $expResult)) {
25
            $builder->down();
26
        }
27
28
        return $builder->withData('expected_result', $result)
29
            ->withData('expression_result', $expResult)
30
            ->build();
31
    }
32
33
    /**
34
     *
35
     * @param string $expression
36
     * @return mixed
37
     */
38
    private function evalExpression(string $expression)
39
    {
40
        return eval("return {$expression};");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
41
    }
42
}
43