Complex classes like Select 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 Select, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Select |
||
| 8 | { |
||
| 9 | |||
| 10 | /** @var string */ |
||
| 11 | private $_tableName; |
||
| 12 | |||
| 13 | /** @var Connection */ |
||
| 14 | private $_con; |
||
| 15 | |||
| 16 | /** @var array */ |
||
| 17 | private $_select; |
||
| 18 | |||
| 19 | /** @var string|null */ |
||
| 20 | private $_alias; |
||
| 21 | |||
| 22 | /** @var array */ |
||
| 23 | private $_join = array(); |
||
| 24 | |||
| 25 | /** @var array */ |
||
| 26 | private $_where = array(); |
||
| 27 | |||
| 28 | private $_sort = array(); |
||
| 29 | |||
| 30 | private $_pageNum = null; |
||
| 31 | private $_pageSize = null; |
||
| 32 | |||
| 33 | private $_params = array(); |
||
| 34 | |||
| 35 | |||
| 36 | function __construct($tableName, Connection $connection, $select = null, $alias = null) { |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * @return null|string |
||
| 46 | */ |
||
| 47 | function getAlias() { |
||
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * @param array $select |
||
| 54 | * @param string $prefix |
||
| 55 | * @return Select |
||
| 56 | */ |
||
| 57 | function addSelect(array $select, $prefix = '') { |
||
| 63 | |||
| 64 | |||
| 65 | function reset() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param $table |
||
| 76 | * @param $on |
||
| 77 | * @param $alias |
||
| 78 | * @return Select |
||
| 79 | */ |
||
| 80 | function join($table, $on, $alias) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $table |
||
| 87 | * @param $on |
||
| 88 | * @param $alias |
||
| 89 | * @return Select |
||
| 90 | */ |
||
| 91 | function leftJoin($table, $on, $alias) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param array|string $key |
||
| 98 | * @param null|mixed $value |
||
| 99 | * @param null|string $operator |
||
| 100 | * @return Select |
||
| 101 | */ |
||
| 102 | function where($key, $value = null, $operator = null) { |
||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * @param $string |
||
| 119 | * @return Select |
||
| 120 | */ |
||
| 121 | function whereString($string) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param array|string $column |
||
| 128 | * @param null|string $dir |
||
| 129 | * @return Select |
||
| 130 | */ |
||
| 131 | function sort($column, $dir = null) { |
||
| 141 | |||
| 142 | private function getSortDir($dir) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param $pageNum int |
||
| 156 | * @param $pageSize int |
||
| 157 | * @return Select |
||
| 158 | */ |
||
| 159 | function paging($pageNum, $pageSize) { |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * @return string|int |
||
| 168 | */ |
||
| 169 | function getOne() { |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | function getRow() { |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | function getAll() { |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * @return PagedResult |
||
| 198 | */ |
||
| 199 | function pagedResult() { |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * @return int |
||
| 209 | */ |
||
| 210 | function getFoundRows() { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $name |
||
| 216 | * @param mixed $value |
||
| 217 | * @return Select |
||
| 218 | */ |
||
| 219 | function setParam($name, $value) { |
||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | private function build() { |
||
| 280 | |||
| 281 | } |
||
| 282 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.