Complex classes like Service 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 Service, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class Service |
||
| 49 | { |
||
| 50 | private $treeAdapter; |
||
| 51 | |||
| 52 | public function __construct(TreeInterface $treeAdapter) |
||
| 53 | { |
||
| 54 | $this->treeAdapter = $treeAdapter; |
||
| 55 | } |
||
| 56 | |||
| 57 | private function getTreeAdapter(): TreeInterface |
||
| 58 | { |
||
| 59 | return $this->treeAdapter; |
||
| 60 | } |
||
| 61 | |||
| 62 | public function createRoot(array $data): void |
||
| 63 | { |
||
| 64 | $errors = array(); |
||
| 65 | |||
| 66 | $label = $data['label'] ?? ''; |
||
| 67 | if (0 == strlen($label)) { |
||
| 68 | $errors[] = 'Root Name cannot be empty.'; |
||
| 69 | } elseif (15 < strlen($label)) { |
||
| 70 | $errors[] = 'Root Name is too long. Max length is 15 characters.'; |
||
| 71 | } |
||
| 72 | |||
| 73 | $scope = $data['scope'] ?? ''; |
||
| 74 | if (0 === strlen($scope)) { |
||
| 75 | $errors[] = 'Scope Name cannot be empty.'; |
||
| 76 | } elseif (!preg_match('|^[1-9][0-9]*$|', $scope)) { |
||
| 77 | $errors[] = 'Scope Name must be integer.'; |
||
| 78 | } elseif (15 < strlen($scope)) { |
||
| 79 | $errors[] = 'Scope Name is too long. Max length is 15 characters.'; |
||
| 80 | } |
||
| 81 | |||
| 82 | if (count($errors)) { |
||
| 83 | throw new ValidationError($errors); |
||
| 84 | } |
||
| 85 | |||
| 86 | $data = array( |
||
| 87 | 'name' => $label, |
||
| 88 | ); |
||
| 89 | |||
| 90 | try { |
||
| 91 | $this->getTreeAdapter() |
||
| 92 | ->createRootNode($data, $scope); |
||
| 93 | } catch (ValidationException $e) { |
||
| 94 | throw new ValidationError([$e->getMessage()]); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | public function createNode(array $data): void |
||
| 99 | { |
||
| 100 | $errors = array(); |
||
| 101 | |||
| 102 | $targetId = $_POST['target_node_id'] ?? ''; |
||
| 103 | if (0 === strlen($targetId)) { |
||
| 104 | $errors[] = 'Target Node cannot be empty.'; |
||
| 105 | } |
||
| 106 | |||
| 107 | $label = $data['label'] ?? ''; |
||
| 108 | if (0 == strlen($label)) { |
||
| 109 | $errors[] = 'Name cannot be empty.'; |
||
| 110 | } elseif (15 < strlen($label)) { |
||
| 111 | $errors[] = 'Name is too long. Max length is 15 characters.'; |
||
| 112 | } |
||
| 113 | |||
| 114 | $placement = $data['placement'] ?? ''; |
||
| 115 | if (0 == strlen($placement)) { |
||
| 116 | $errors[] = 'Placement cannot be empty.'; |
||
| 117 | } |
||
| 118 | |||
| 119 | $data = array( |
||
| 120 | 'name' => $label, |
||
| 121 | ); |
||
| 122 | |||
| 123 | if (count($errors)) { |
||
| 124 | throw new ValidationError($errors); |
||
| 125 | } |
||
| 126 | |||
| 127 | try { |
||
| 128 | $this->getTreeAdapter() |
||
| 129 | ->addNode($targetId, $data, $placement); |
||
| 130 | } catch (ValidationException $e) { |
||
| 131 | throw new ValidationError([$e->getMessage()]); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | public function deleteNode(array $data): void |
||
| 136 | { |
||
| 137 | $errors = array(); |
||
| 138 | |||
| 139 | $id = $data['id'] ?? ''; |
||
| 140 | |||
| 141 | if (0 == strlen($id)) { |
||
| 142 | $errors[] = 'Id is missing. Cannot perform delete operation.'; |
||
| 143 | } |
||
| 144 | |||
| 145 | if (count($errors)) { |
||
| 146 | throw new ValidationError($errors); |
||
| 147 | } |
||
| 148 | |||
| 149 | $this->getTreeAdapter() |
||
| 150 | ->deleteBranch($id); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function updateNode(array $data): void |
||
| 154 | { |
||
| 155 | $errors = array(); |
||
| 156 | |||
| 157 | $nodeId = $_POST['node_id'] ?? ''; |
||
| 158 | if (0 === strlen($nodeId)) { |
||
| 159 | $errors[] = 'Node cannot be empty.'; |
||
| 160 | } |
||
| 161 | |||
| 162 | $label = $data['label'] ?? ''; |
||
| 163 | if (0 == strlen($label)) { |
||
| 164 | $errors[] = 'Name cannot be empty.'; |
||
| 165 | } elseif (15 < strlen($label)) { |
||
| 166 | $errors[] = 'Name is too long. Max length is 15 characters.'; |
||
| 167 | } |
||
| 168 | |||
| 169 | $data = array( |
||
| 170 | 'name' => $label, |
||
| 171 | ); |
||
| 172 | |||
| 173 | if (count($errors)) { |
||
| 174 | throw new ValidationError($errors); |
||
| 175 | } |
||
| 176 | |||
| 177 | try { |
||
| 178 | $this->getTreeAdapter() |
||
| 179 | ->updateNode($nodeId, $data); |
||
| 180 | } catch (ValidationException $e) { |
||
| 181 | throw new ValidationError([$e->getMessage()]); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | public function moveNode(array $data): void |
||
| 186 | { |
||
| 187 | $errors = array(); |
||
| 188 | |||
| 189 | $sourceId = $data['source_node_id'] ?? ''; |
||
| 190 | if (0 == strlen($sourceId)) { |
||
| 191 | $errors[] = 'Source Node cannot be empty.'; |
||
| 192 | } |
||
| 193 | |||
| 194 | $targetId = $data['target_node_id'] ?? ''; |
||
| 195 | if (0 == strlen($targetId)) { |
||
| 196 | $errors[] = 'Target Node cannot be empty.'; |
||
| 197 | } |
||
| 198 | |||
| 199 | $placement = $data['placement'] ?? ''; |
||
| 200 | if (0 == strlen($placement)) { |
||
| 201 | $errors[] = 'Placement cannot be empty.'; |
||
| 202 | } |
||
| 203 | |||
| 204 | if (count($errors)) { |
||
| 205 | throw new ValidationError($errors); |
||
| 206 | } |
||
| 207 | |||
| 208 | try { |
||
| 209 | $this->getTreeAdapter() |
||
| 210 | ->moveNode($sourceId, $targetId, $placement); |
||
| 211 | } catch (ValidationException $e) { |
||
| 212 | throw new ValidationError([$e->getMessage()]); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | public function getRoots(): array |
||
| 217 | { |
||
| 218 | return $this->getTreeAdapter() |
||
| 219 | ->getRoots(); |
||
| 220 | } |
||
| 221 | |||
| 222 | public function getDescendants($nodeId): array |
||
| 223 | { |
||
| 224 | return $this->getTreeAdapter() |
||
| 225 | ->getDescendantsQueryBuilder() |
||
| 226 | ->get($nodeId); |
||
| 227 | } |
||
| 228 | |||
| 229 | public function findDescendants(array $criteria): array |
||
| 230 | { |
||
| 231 | $queryBuilder = $this->getTreeAdapter() |
||
| 232 | ->getDescendantsQueryBuilder(); |
||
| 233 | |||
| 234 | $errors = array(); |
||
| 235 | |||
| 236 | $nodeId = $criteria['node_id'] ?? ''; |
||
| 237 | if (0 === strlen($nodeId)) { |
||
| 238 | $errors[] = 'Node cannot be empty.'; |
||
| 239 | } |
||
| 240 | |||
| 241 | $excludeFirstNLevel = $criteria['exclude_first_n_level'] ?? null; |
||
| 242 | if (null !== $excludeFirstNLevel) { |
||
| 243 | if (!preg_match('|^[0-9]*$|', $excludeFirstNLevel)) { |
||
| 244 | $errors[] = 'Exclude First N Level must be positive integer,'; |
||
| 245 | } else { |
||
| 246 | $queryBuilder->excludeFirstNLevel((int) $excludeFirstNLevel); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | $levelLimit = $criteria['level_limit'] ?? null; |
||
| 251 | if (null !== $levelLimit) { |
||
| 252 | if (!preg_match('|^[0-9]*$|', $levelLimit)) { |
||
| 253 | $errors[] = 'Level limit must be positive integer,'; |
||
| 254 | } else { |
||
| 255 | $queryBuilder->levelLimit((int) $levelLimit); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | $excludeBranch = $criteria['exclude_node_id'] ?? null; |
||
| 260 | if (null !== $excludeBranch) { |
||
| 261 | $queryBuilder->excludeBranch($excludeBranch); |
||
| 262 | } |
||
| 263 | |||
| 264 | if (count($errors)) { |
||
| 265 | throw new ValidationError($errors); |
||
| 266 | } |
||
| 267 | |||
| 268 | return $queryBuilder->get($nodeId); |
||
| 269 | } |
||
| 270 | |||
| 271 | public function findAncestors(array $criteria): array |
||
| 272 | { |
||
| 273 | $queryBuilder = $this->getTreeAdapter() |
||
| 274 | ->getAncestorsQueryBuilder(); |
||
| 275 | |||
| 276 | $errors = array(); |
||
| 277 | |||
| 278 | $nodeId = $criteria['node_id'] ?? ''; |
||
| 279 | if (0 === strlen($nodeId)) { |
||
| 280 | $errors[] = 'Node cannot be empty.'; |
||
| 281 | } |
||
| 282 | |||
| 283 | $excludeFirstNLevel = $criteria['exclude_first_n_level'] ?? null; |
||
| 284 | if (null !== $excludeFirstNLevel) { |
||
| 285 | if (!preg_match('|^[0-9]*$|', $excludeFirstNLevel)) { |
||
| 286 | $errors[] = 'Exclude First N Level must be positive integer,'; |
||
| 287 | } else { |
||
| 288 | $queryBuilder->excludeFirstNLevel((int) $excludeFirstNLevel); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | $excludeLastNLevel = $criteria['exclude_last_n_level'] ?? null; |
||
| 293 | if (null !== $excludeLastNLevel) { |
||
| 294 | if (!preg_match('|^[0-9]*$|', $excludeLastNLevel)) { |
||
| 295 | $errors[] = 'Exclude Last N Level must be positive integer,'; |
||
| 296 | } else { |
||
| 297 | $queryBuilder->excludeLastNLevel((int) $excludeLastNLevel); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | if (count($errors)) { |
||
| 302 | throw new ValidationError($errors); |
||
| 303 | } |
||
| 304 | |||
| 305 | return $queryBuilder->get($nodeId); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 690 | </html> |