| Total Complexity | 40 |
| Total Lines | 238 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | abstract class Base |
||
| 8 | { |
||
| 9 | |||
| 10 | |||
| 11 | protected $soteria; |
||
| 12 | protected $score = 0; |
||
| 13 | private $passScore = 0; |
||
| 14 | private $failScore = 0; |
||
| 15 | |||
| 16 | private $realScore = 0; |
||
| 17 | private $options = []; |
||
| 18 | private $results = null; |
||
| 19 | |||
| 20 | private $params = []; |
||
| 21 | |||
| 22 | private $name; |
||
| 23 | private $active; |
||
| 24 | private $reference; |
||
| 25 | |||
| 26 | private $output = null; |
||
| 27 | |||
| 28 | public function __construct() |
||
| 29 | { |
||
| 30 | $this->soteria = new SoteriaService(); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function getOutput() |
||
| 34 | { |
||
| 35 | return $this->output; |
||
| 36 | } |
||
| 37 | |||
| 38 | protected function setOutput($value) |
||
| 39 | { |
||
| 40 | $this->output = $value; |
||
| 41 | return $this; |
||
| 42 | } |
||
| 43 | |||
| 44 | public function setOptions($reference, $options = []) |
||
| 45 | { |
||
| 46 | $this->reference = $reference; |
||
| 47 | $this->options = $options; |
||
| 48 | return $this; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function inParams($name) |
||
| 52 | { |
||
| 53 | if (in_array($name, $this->getParams())) { |
||
| 54 | return true; |
||
| 55 | } |
||
| 56 | return false; |
||
| 57 | } |
||
| 58 | |||
| 59 | protected function getParams() |
||
| 60 | { |
||
| 61 | return $this->params; |
||
| 62 | } |
||
| 63 | |||
| 64 | protected function setParams($params) |
||
| 65 | { |
||
| 66 | $this->params = explode("|", $params); |
||
| 67 | return $this; |
||
| 68 | } |
||
| 69 | |||
| 70 | public function getRuleOption($name, $score) |
||
| 71 | { |
||
| 72 | if (!$this->hasRuleOption($name)) { |
||
| 73 | return $score; |
||
| 74 | } |
||
| 75 | |||
| 76 | return $this->options[$this->name][$name]; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function hasRuleOption($name) |
||
| 82 | } |
||
| 83 | |||
| 84 | public function getResult() |
||
| 85 | { |
||
| 86 | return $this->results; |
||
| 87 | } |
||
| 88 | |||
| 89 | protected function overRideScore($data) |
||
| 90 | { |
||
| 91 | $sanitise = $this->soteria->sanitise(); |
||
| 92 | $this->score = $this->getRealScore(); |
||
| 93 | |||
| 94 | if (isset($data[1])) { |
||
| 95 | |||
| 96 | $scoreData = explode(",", $data[1]); |
||
| 97 | |||
| 98 | if (count($scoreData) == 2) { |
||
| 99 | $sanitise->disinfect($scoreData[0], 'int'); |
||
| 100 | $this->score = (int)$sanitise->result()->getOutput(); |
||
| 101 | $sanitise->disinfect($scoreData[1], 'int'); |
||
| 102 | $this->passScore = (int)$sanitise->result()->getOutput(); |
||
| 103 | } else { |
||
| 104 | $sanitise->disinfect($scoreData[0], 'int'); |
||
| 105 | $this->score = (int)$sanitise->result()->getOutput(); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | $this->failScore = $this->score; |
||
| 109 | $this->score += (-1 * abs($this->passScore)); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function getFailScore() |
||
| 113 | { |
||
| 114 | return $this->failScore; |
||
| 115 | } |
||
| 116 | |||
| 117 | public function getRealScore() |
||
| 118 | { |
||
| 119 | if (!$this->hasRealScore()) { |
||
| 120 | return 0; |
||
| 121 | } |
||
| 122 | |||
| 123 | return $this->realScore; |
||
| 124 | } |
||
| 125 | |||
| 126 | private function hasRealScore() |
||
| 127 | { |
||
| 128 | return isset($this->realScore); |
||
| 129 | } |
||
| 130 | |||
| 131 | protected function pushResult($result) |
||
| 132 | { |
||
| 133 | array_unshift($this->results, $result); |
||
| 134 | } |
||
| 135 | |||
| 136 | protected function sanitizeReference() |
||
| 137 | { |
||
| 138 | return str_replace(" ", "", strip_tags(trim($this->getReference()))); |
||
| 139 | } |
||
| 140 | |||
| 141 | protected function getReference() |
||
| 142 | { |
||
| 143 | $this->setOutput($this->reference); |
||
| 144 | return $this->reference; |
||
| 145 | } |
||
| 146 | |||
| 147 | protected function setReference($reference) |
||
| 151 | } |
||
| 152 | |||
| 153 | protected function initWrapper($name) |
||
| 154 | { |
||
| 155 | $this->setName($name); |
||
| 156 | $this->setRules(); |
||
| 157 | } |
||
| 158 | |||
| 159 | protected function setName($name) |
||
| 160 | { |
||
| 161 | $this->name = $name; |
||
| 162 | return $this; |
||
| 163 | } |
||
| 164 | |||
| 165 | private function setRules() |
||
| 166 | { |
||
| 167 | $this->options = $this->getOption($this->getName()); |
||
| 168 | |||
| 169 | $this->setRealScore($this->getOption('score')); |
||
| 170 | $this->setActive($this->getOption('active')); |
||
| 171 | $this->setParams($this->getOption('params')); |
||
| 172 | } |
||
| 173 | |||
| 174 | public function getOption($name) |
||
| 175 | { |
||
| 176 | if (!$this->hasOption($name)) { |
||
| 177 | return null; |
||
| 178 | } |
||
| 179 | |||
| 180 | return $this->options[$name]; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function hasOption($name) |
||
| 184 | { |
||
| 185 | return isset($this->options[$name]); |
||
| 186 | } |
||
| 187 | |||
| 188 | private function getName() |
||
| 189 | { |
||
| 190 | return $this->name; |
||
| 191 | } |
||
| 192 | |||
| 193 | private function setRealScore($score) |
||
| 194 | { |
||
| 195 | |||
| 196 | if (is_array($score)) { |
||
| 197 | $this->realScore = (int)$score['fail']; |
||
| 198 | $this->passScore = (int)$score['pass']; |
||
| 199 | } else { |
||
| 200 | $this->realScore = (int)$score; |
||
| 201 | } |
||
| 202 | $this->failScore = $this->realScore; |
||
| 203 | $this->realScore += (-1 * abs($this->passScore)); |
||
| 204 | |||
| 205 | } |
||
| 206 | |||
| 207 | private function setActive($active) |
||
| 210 | } |
||
| 211 | |||
| 212 | protected function setResult() |
||
| 221 | } |
||
| 222 | |||
| 223 | private function getActive() |
||
| 224 | { |
||
| 225 | return $this->active; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function getScore() |
||
| 229 | { |
||
| 230 | if (!$this->hasScore()) { |
||
| 231 | return 0; |
||
| 232 | } |
||
| 233 | |||
| 234 | return $this->score; |
||
| 235 | } |
||
| 236 | |||
| 237 | protected function setScore($score) |
||
| 240 | } |
||
| 241 | |||
| 242 | private function hasScore() |
||
| 243 | { |
||
| 244 | return isset($this->score); |
||
| 245 | } |
||
| 246 | |||
| 247 | } |