Completed
Push — master ( 95fc13...87396e )
by Arkadiusz
02:45
created

DecisionTreeLeaf::__toString()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 25
nc 15
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Phpml\Classification\DecisionTree;
5
6
class DecisionTreeLeaf
7
{
8
    const OPERATOR_EQ = '=';
9
    /**
10
     * @var string
11
     */
12
    public $value;
13
14
    /**
15
     * @var int
16
     */
17
    public $columnIndex;
18
19
    /**
20
     * @var DecisionTreeLeaf
21
     */
22
    public $leftLeaf = null;
23
24
    /**
25
     * @var DecisionTreeLeaf
26
     */
27
    public $rightLeaf= null;
28
29
    /**
30
     * @var array
31
     */
32
    public $records = [];
33
34
    /**
35
     * Class value represented by the leaf, this value is non-empty
36
     * only for terminal leaves
37
     *
38
     * @var string
39
     */
40
    public $classValue = '';
41
42
    /**
43
     * @var bool
44
     */
45
    public $isTerminal = false;
46
47
    /**
48
     * @var float
49
     */
50
    public $giniIndex = 0;
51
52
    /**
53
     * @var int
54
     */
55
    public $level = 0;
56
57
    /**
58
     * @param array $record
59
     * @return bool
60
     */
61
    public function evaluate($record)
62
    {
63
        $recordField = $record[$this->columnIndex];
64
        if (preg_match("/^([<>=]{1,2})\s*(.*)/", $this->value, $matches)) {
65
            $op = $matches[1];
66
            $value= floatval($matches[2]);
67
            $recordField = strval($recordField);
68
            eval("\$result = $recordField $op $value;");
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
69
            return $result;
0 ignored issues
show
Bug introduced by
The variable $result does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
70
        }
71
        return $recordField == $this->value;
72
    }
73
74
    public function __toString()
75
    {
76
        if ($this->isTerminal) {
77
            $value = "<b>$this->classValue</b>";
78
        } else {
79
            $value = $this->value;
80
            $col = "col_$this->columnIndex";
81
            if (! preg_match("/^[<>=]{1,2}/", $value)) {
82
                $value = "=$value";
83
            }
84
            $value = "<b>$col $value</b><br>Gini: ". number_format($this->giniIndex, 2);
85
        }
86
        $str = "<table ><tr><td colspan=3 align=center style='border:1px solid;'>
87
				$value</td></tr>";
88
        if ($this->leftLeaf || $this->rightLeaf) {
89
            $str .='<tr>';
90
            if ($this->leftLeaf) {
91
                $str .="<td valign=top><b>| Yes</b><br>$this->leftLeaf</td>";
92
            } else {
93
                $str .='<td></td>';
94
            }
95
            $str .='<td>&nbsp;</td>';
96
            if ($this->rightLeaf) {
97
                $str .="<td valign=top align=right><b>No |</b><br>$this->rightLeaf</td>";
98
            } else {
99
                $str .='<td></td>';
100
            }
101
            $str .= '</tr>';
102
        }
103
        $str .= '</table>';
104
        return $str;
105
    }
106
}
107