| Conditions | 11 |
| Paths | 192 |
| Total Lines | 67 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 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 |
||
| 69 | protected function getDefaultPredictionPredictor(int $limit, float $threshold, float $recencyFactor, array $fieldsThatMustBeEqual): array |
||
| 70 | { |
||
| 71 | $owner = $this->getOwner(); |
||
| 72 | $className = $owner->ClassName; |
||
| 73 | |||
| 74 | // set return variable |
||
| 75 | $predicts = []; |
||
| 76 | |||
| 77 | // get field names |
||
| 78 | $fieldNames = $this->getDefaultPredictionFieldNames($className); |
||
| 79 | |||
| 80 | // must be equal |
||
| 81 | $mustBeEqual = []; |
||
| 82 | foreach($fieldsThatMustBeEqual as $fieldThatMustBeEqual) { |
||
| 83 | if(isset($fieldNames[$fieldThatMustBeEqual])) { |
||
| 84 | $mustBeEqual[$fieldThatMustBeEqual] = $owner->{$fieldThatMustBeEqual}; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | // get last objects, based on limit; |
||
| 88 | $objects = $className::get() |
||
| 89 | ->sort(['ID' => 'DESC']) |
||
| 90 | ->exclude(['ID' => $owner->ID]) |
||
| 91 | ->limit($limit) |
||
| 92 | ; |
||
| 93 | if(count($mustBeEqual)) { |
||
| 94 | $objects = $objects->filter($mustBeEqual); |
||
| 95 | } |
||
| 96 | // print_r($objects->column('Purpose')); |
||
| 97 | // print_r($objects->column('Title')); |
||
| 98 | //store objects in memory |
||
| 99 | $objectArray = []; |
||
| 100 | foreach ($objects as $object) { |
||
| 101 | $objectArray[] = $object; |
||
| 102 | } |
||
| 103 | // put the latest one last so that we can give it more weight. |
||
| 104 | $objectArray = array_reverse($objectArray); |
||
| 105 | |||
| 106 | // get the field names |
||
| 107 | // print_r($fieldNames); |
||
| 108 | // loop through fields |
||
| 109 | foreach ($fieldNames as $fieldName) { |
||
| 110 | $valueArray = []; |
||
| 111 | // loop through objects |
||
| 112 | $max = 0; |
||
| 113 | foreach ($objectArray as $object) { |
||
| 114 | $value = $object->{$fieldName}; |
||
| 115 | if (! $value) { |
||
| 116 | $value = ''; |
||
| 117 | } |
||
| 118 | // give more weight to the last one used. |
||
| 119 | for ($y = 0; $y <= $max; ++$y) { |
||
| 120 | $valueArray[] = $value; |
||
| 121 | } |
||
| 122 | $max += $recencyFactor; |
||
| 123 | } |
||
| 124 | // print_r($fieldName); |
||
| 125 | // print_r($valueArray); |
||
| 126 | if (count($valueArray)) { |
||
| 127 | // work out if there is a value that comes back a lot, and, if so, add it to predicts |
||
| 128 | $possibleValue = $this->defaultPredictionPredictorBestContender($valueArray, $threshold); |
||
| 129 | if ($possibleValue) { |
||
| 130 | $predicts[$fieldName] = $possibleValue; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | // print_r($predicts); |
||
| 135 | return $predicts; |
||
| 136 | } |
||
| 188 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.