Complex classes like RunnableSelect 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 RunnableSelect, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class RunnableSelect extends Select implements IteratorAggregate { |
||
| 18 | /** @var array */ |
||
| 19 | private $values = array(); |
||
| 20 | /** @var bool */ |
||
| 21 | private $preserveTypes = false; |
||
| 22 | /** @var int */ |
||
| 23 | private $foundRows = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param array $values |
||
| 27 | * @return $this |
||
| 28 | */ |
||
| 29 | public function bindValues(array $values) { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param string $key |
||
| 36 | * @param string|int|bool|float|null $value |
||
| 37 | * @return $this |
||
| 38 | */ |
||
| 39 | public function bindValue($key, $value) { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return $this |
||
| 46 | */ |
||
| 47 | public function clearValues() { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param bool $preserveTypes |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | public function setPreserveTypes($preserveTypes = true) { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param Closure $callback |
||
| 63 | * @return array[] |
||
| 64 | */ |
||
| 65 | public function fetchRows(Closure $callback = null) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param Closure $callback |
||
| 71 | * @return array[]|\Generator |
||
| 72 | */ |
||
| 73 | public function fetchRowsLazy(Closure $callback = null) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param Closure|null $callback |
||
| 79 | * @return mixed[] |
||
| 80 | * @throws \Exception |
||
| 81 | */ |
||
| 82 | public function fetchRow(Closure $callback = null) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $className |
||
| 88 | * @param Closure $callback |
||
| 89 | * @return object[] |
||
| 90 | * @throws \Exception |
||
| 91 | */ |
||
| 92 | public function fetchObjects($className, Closure $callback = null) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $className |
||
| 98 | * @param Closure $callback |
||
| 99 | * @return object[]|Generator |
||
| 100 | */ |
||
| 101 | public function fetchObjectsLazy($className, Closure $callback = null) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param string $className |
||
| 107 | * @param Closure|null $callback |
||
| 108 | * @return object[] |
||
| 109 | * @throws \Exception |
||
| 110 | */ |
||
| 111 | public function fetchObject($className, Closure $callback = null) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param bool $treatValueAsArray |
||
| 117 | * @return mixed[] |
||
| 118 | */ |
||
| 119 | public function fetchKeyValue($treatValueAsArray = false) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param array $fields |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | public function fetchGroups(array $fields) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return string[] |
||
| 157 | */ |
||
| 158 | public function fetchArray() { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param mixed $default |
||
| 166 | * @return null|bool|string|int|float |
||
| 167 | */ |
||
| 168 | public function fetchValue($default = null) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public function getFoundRows() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param callback $fn |
||
| 187 | * @return mixed |
||
| 188 | * @throws \Exception |
||
| 189 | */ |
||
| 190 | private function createTempStatement($fn) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return QueryStatement |
||
| 205 | */ |
||
| 206 | private function createStatement() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return Traversable|array[]|\Generator |
||
| 219 | */ |
||
| 220 | public function getIterator() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param callable $callback |
||
| 226 | * @param int $mode |
||
| 227 | * @param mixed $arg0 |
||
| 228 | * @return mixed |
||
| 229 | * @throws \Exception |
||
| 230 | */ |
||
| 231 | private function fetchAll($callback, $mode, $arg0 = null) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param callable $callback |
||
| 260 | * @param int $mode |
||
| 261 | * @param mixed $arg0 |
||
| 262 | * @return Generator|YieldPolyfillIterator|mixed[] |
||
| 263 | */ |
||
| 264 | private function fetchLazy($callback, $mode, $arg0 = null) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param callable $callback |
||
| 280 | * @param int $mode |
||
| 281 | * @param mixed $arg0 |
||
| 282 | * @return mixed |
||
| 283 | * @throws \Exception |
||
| 284 | */ |
||
| 285 | private function fetch($callback, $mode, $arg0 = null) { |
||
| 305 | } |
||
| 306 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):