| Conditions | 9 |
| Paths | 12 |
| Total Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 78 | public function grow(array $samples, array $targets): void |
||
| 79 | { |
||
| 80 | $this->featureCount = count($samples[0]); |
||
| 81 | $depth = 1; |
||
| 82 | $this->root = $this->split($samples, $targets); |
||
| 83 | $stack = [[$this->root, $depth]]; |
||
| 84 | |||
| 85 | while ($stack) { |
||
|
|
|||
| 86 | [$current, $depth] = array_pop($stack) ?? []; |
||
| 87 | |||
| 88 | [$left, $right] = $current->groups(); |
||
| 89 | |||
| 90 | $current->cleanup(); |
||
| 91 | |||
| 92 | $depth++; |
||
| 93 | |||
| 94 | if ($left === [] || $right === []) { |
||
| 95 | $node = $this->terminate(array_merge($left[1], $right[1])); |
||
| 96 | |||
| 97 | $current->attachLeft($node); |
||
| 98 | $current->attachRight($node); |
||
| 99 | |||
| 100 | continue 1; |
||
| 101 | } |
||
| 102 | |||
| 103 | if ($depth >= $this->maxDepth) { |
||
| 104 | $current->attachLeft($this->terminate($left[1])); |
||
| 105 | $current->attachRight($this->terminate($right[1])); |
||
| 106 | |||
| 107 | continue 1; |
||
| 108 | } |
||
| 109 | |||
| 110 | if (count($left[1]) > $this->maxLeafSize) { |
||
| 111 | $node = $this->split($left[0], $left[1]); |
||
| 112 | |||
| 113 | if ($node->purityIncrease() + 1e-8 > $this->minPurityIncrease) { |
||
| 114 | $current->attachLeft($node); |
||
| 115 | |||
| 116 | $stack[] = [$node, $depth]; |
||
| 117 | } else { |
||
| 118 | $current->attachLeft($this->terminate($left[1])); |
||
| 119 | } |
||
| 120 | } else { |
||
| 121 | $current->attachLeft($this->terminate($left[1])); |
||
| 122 | } |
||
| 123 | |||
| 124 | if (count($right[1]) > $this->maxLeafSize) { |
||
| 125 | $node = $this->split($right[0], $right[1]); |
||
| 126 | |||
| 127 | if ($node->purityIncrease() + 1e-8 > $this->minPurityIncrease) { |
||
| 128 | $current->attachRight($node); |
||
| 129 | |||
| 130 | $stack[] = [$node, $depth]; |
||
| 131 | } else { |
||
| 132 | $current->attachRight($this->terminate($right[1])); |
||
| 133 | } |
||
| 134 | } else { |
||
| 135 | $current->attachRight($this->terminate($right[1])); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 177 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.