Complex classes like NumberType 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 NumberType, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class NumberType extends AbstractType |
||
| 14 | { |
||
| 15 | |||
| 16 | const REGEX_RANGE = '(?:([[<])(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?,(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?([\\]>]))?'; |
||
| 17 | const REGEX_DEFAULT = '(?:=(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)))?'; |
||
| 18 | |||
| 19 | private static $formats = array( |
||
| 20 | 'float' => 'float', |
||
| 21 | 'double' => 'double', |
||
| 22 | ); |
||
| 23 | private $format; |
||
| 24 | private $default = null; |
||
| 25 | private $maximum = null; |
||
| 26 | private $exclusiveMaximum = null; |
||
| 27 | private $minimum = null; |
||
| 28 | private $exclusiveMinimum = null; |
||
| 29 | private $enum = array(); |
||
| 30 | private $multipleOf = null; |
||
| 31 | |||
| 32 | protected function parseDefinition($definition) |
||
| 43 | |||
| 44 | private function parseFormat($definition, $match) |
||
| 45 | { |
||
| 46 | if (!isset(self::$formats[strtolower($match[1])])) { |
||
| 47 | throw new \SwaggerGen\Exception("Not a number: '{$definition}'"); |
||
| 48 | } |
||
| 49 | $this->format = self::$formats[strtolower($match[1])]; |
||
| 50 | } |
||
| 51 | |||
| 52 | private function parseRange($definition, $match) |
||
| 53 | { |
||
| 54 | if (!empty($match[2])) { |
||
| 55 | if ($match[3] === '' && $match[4] === '') { |
||
| 56 | throw new \SwaggerGen\Exception("Empty number range: '{$definition}'"); |
||
| 57 | } |
||
| 58 | |||
| 59 | $this->exclusiveMinimum = isset($match[2]) ? ($match[2] == '<') : null; |
||
| 60 | $this->minimum = $match[3] === '' ? null : doubleval($match[3]); |
||
| 61 | $this->maximum = $match[4] === '' ? null : doubleval($match[4]); |
||
| 62 | $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
||
| 63 | if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
||
| 64 | self::swap($this->minimum, $this->maximum); |
||
| 65 | self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | private function parseDefault($definition, $match) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param string $command The comment command |
||
| 77 | * @param string $data Any data added after the command |
||
| 78 | * @return \SwaggerGen\Swagger\Type\AbstractType|boolean |
||
| 79 | */ |
||
| 80 | public function handleCommand($command, $data = null) |
||
| 81 | { |
||
| 82 | switch (strtolower($command)) { |
||
| 83 | case 'default': |
||
| 84 | $this->default = $this->validateDefault($data); |
||
| 85 | return $this; |
||
| 86 | |||
| 87 | case 'enum': |
||
| 88 | $words = self::wordSplit($data); |
||
| 89 | foreach ($words as &$word) { |
||
| 90 | $word = $this->validateDefault($word); |
||
| 91 | } |
||
| 92 | $this->enum = array_merge($this->enum, $words); |
||
| 93 | return $this; |
||
| 94 | |||
| 95 | case 'step': |
||
| 96 | if (($step = doubleval($data)) > 0) { |
||
| 97 | $this->multipleOf = $step; |
||
| 98 | } |
||
| 99 | return $this; |
||
| 100 | } |
||
| 101 | |||
| 102 | return parent::handleCommand($command, $data); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function toArray() |
||
| 106 | { |
||
| 107 | return self::arrayFilterNull(array_merge(array( |
||
| 108 | 'type' => 'number', |
||
| 109 | 'format' => $this->format, |
||
| 110 | 'default' => $this->default, |
||
| 111 | 'minimum' => $this->minimum, |
||
| 112 | 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
||
| 113 | 'maximum' => $this->maximum, |
||
| 114 | 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
||
| 115 | 'enum' => $this->enum, |
||
| 116 | 'multipleOf' => $this->multipleOf, |
||
| 117 | ), parent::toArray())); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function __toString() |
||
| 124 | |||
| 125 | private function validateDefault($value) |
||
| 126 | { |
||
| 127 | if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) { |
||
| 144 | |||
| 145 | } |
||
| 146 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.