Complex classes like IntegerType 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 IntegerType, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class IntegerType extends AbstractType |
||
14 | { |
||
15 | |||
16 | const REGEX_RANGE = '(?:([[<])(-?\d*)?,(-?\d*)?([\\]>]))?'; |
||
17 | const REGEX_DEFAULT = '(?:=(-?\d+))?'; |
||
18 | |||
19 | private static $formats = array( |
||
20 | 'int32' => 'int32', |
||
21 | 'integer' => 'int32', |
||
22 | 'int' => 'int32', |
||
23 | 'int64' => 'int64', |
||
24 | 'long' => 'int64', |
||
25 | ); |
||
26 | private $format; |
||
27 | private $default = null; |
||
28 | private $maximum = null; |
||
29 | private $exclusiveMaximum = null; |
||
30 | private $minimum = null; |
||
31 | private $exclusiveMinimum = null; |
||
32 | private $enum = array(); |
||
33 | private $multipleOf = null; |
||
34 | |||
35 | protected function parseDefinition($definition) |
||
48 | |||
49 | /** |
||
50 | * @param string $definition |
||
51 | * @param string[] $match |
||
52 | */ |
||
53 | private function parseFormat($definition, $match) |
||
54 | { |
||
55 | $type = strtolower($match[1]); |
||
56 | if (!isset(self::$formats[$type])) { |
||
57 | throw new \SwaggerGen\Exception("Not an integer: '{$definition}'"); |
||
58 | } |
||
59 | $this->format = self::$formats[$type]; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param string $definition |
||
64 | * @param string[] $match |
||
65 | */ |
||
66 | private function parseRange($definition, $match) |
||
67 | { |
||
68 | if (!empty($match[2])) { |
||
69 | if ($match[3] === '' && $match[4] === '') { |
||
70 | throw new \SwaggerGen\Exception("Empty integer range: '{$definition}'"); |
||
71 | } |
||
72 | |||
73 | $this->exclusiveMinimum = isset($match[2]) ? ($match[2] == '<') : null; |
||
74 | $this->minimum = $match[3] === '' ? null : intval($match[3]); |
||
75 | $this->maximum = $match[4] === '' ? null : intval($match[4]); |
||
76 | $this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
||
77 | if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
||
78 | self::swap($this->minimum, $this->maximum); |
||
79 | self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | |||
84 | private function parseDefault($definition, $match) |
||
88 | |||
89 | /** |
||
90 | * @param string $command The comment command |
||
91 | * @param string $data Any data added after the command |
||
92 | * @return \SwaggerGen\Swagger\Type\AbstractType|boolean |
||
93 | */ |
||
94 | public function handleCommand($command, $data = null) |
||
95 | { |
||
96 | switch (strtolower($command)) { |
||
97 | case 'default': |
||
98 | $this->default = $this->validateDefault($data); |
||
99 | return $this; |
||
100 | |||
101 | case 'enum': |
||
102 | $words = self::wordSplit($data); |
||
103 | foreach ($words as &$word) { |
||
104 | $word = $this->validateDefault($word); |
||
105 | } |
||
106 | $this->enum = array_merge($this->enum, $words); |
||
107 | return $this; |
||
108 | |||
109 | case 'step': |
||
110 | if (($step = intval($data)) > 0) { |
||
111 | $this->multipleOf = $step; |
||
112 | } |
||
113 | return $this; |
||
114 | } |
||
115 | |||
116 | return parent::handleCommand($command, $data); |
||
117 | } |
||
118 | |||
119 | public function toArray() |
||
120 | { |
||
121 | return self::arrayFilterNull(array_merge(array( |
||
122 | 'type' => 'integer', |
||
123 | 'format' => $this->format, |
||
124 | 'default' => $this->default, |
||
125 | 'minimum' => $this->minimum, |
||
126 | 'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
||
127 | 'maximum' => $this->maximum, |
||
128 | 'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
||
129 | 'enum' => $this->enum, |
||
130 | 'multipleOf' => $this->multipleOf, |
||
131 | ), parent::toArray())); |
||
132 | } |
||
133 | |||
134 | public function __toString() |
||
138 | |||
139 | private function validateDefault($value) |
||
140 | { |
||
141 | if (preg_match('~^-?\d+$~', $value) !== 1) { |
||
158 | |||
159 | } |
||
160 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.