Complex classes like Segment 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 Segment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Segment implements SegmentInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * |
||
| 22 | * The session manager. |
||
| 23 | * |
||
| 24 | * @var Session |
||
| 25 | * |
||
| 26 | */ |
||
| 27 | protected $session; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * |
||
| 31 | * The segment name. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | * |
||
| 35 | */ |
||
| 36 | protected $name; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * |
||
| 40 | * Constructor. |
||
| 41 | * |
||
| 42 | * @param Session $session The session manager. |
||
| 43 | * |
||
| 44 | * @param string $name The segment name. |
||
| 45 | * |
||
| 46 | */ |
||
| 47 | 22 | public function __construct(Session $session, $name) |
|
| 48 | { |
||
| 49 | 22 | $this->session = $session; |
|
| 50 | 22 | $this->name = $name; |
|
| 51 | 22 | } |
|
| 52 | |||
| 53 | /** |
||
| 54 | * |
||
| 55 | * Returns the value of a key in the segment. |
||
| 56 | * |
||
| 57 | * @param string $key The key in the segment. |
||
| 58 | * |
||
| 59 | * @param mixed $alt An alternative value to return if the key is not set. |
||
| 60 | * |
||
| 61 | * @return mixed |
||
| 62 | * |
||
| 63 | */ |
||
| 64 | 11 | public function get($key, $alt = null) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * |
||
| 74 | * Returns the entire segment. |
||
| 75 | * |
||
| 76 | * @return mixed |
||
| 77 | * |
||
| 78 | */ |
||
| 79 | 1 | public function getSegment() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * |
||
| 89 | * Sets the value of a key in the segment. |
||
| 90 | * |
||
| 91 | * @param string $key The key to set. |
||
| 92 | * |
||
| 93 | * @param mixed $val The value to set it to. |
||
| 94 | * |
||
| 95 | */ |
||
| 96 | 14 | public function set($key, $val) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * |
||
| 104 | * Append a value to a numeric key in the segment. |
||
| 105 | * |
||
| 106 | * @param mixed $val The value to append. |
||
| 107 | * |
||
| 108 | */ |
||
| 109 | 1 | public function add($val) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * |
||
| 117 | * Clear all data from the segment. |
||
| 118 | * |
||
| 119 | * @return null |
||
| 120 | * |
||
| 121 | */ |
||
| 122 | 2 | public function clear() |
|
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Remove a key from the segment, or remove the entire segment (including key) from the session |
||
| 132 | * |
||
| 133 | * @param null $key |
||
| 134 | */ |
||
| 135 | 2 | public function remove($key = null) { |
|
| 146 | |||
| 147 | /** |
||
| 148 | * |
||
| 149 | * Sets a flash value for the *next* request. |
||
| 150 | * |
||
| 151 | * @param string $key The key for the flash value. |
||
| 152 | * |
||
| 153 | * @param mixed $val The flash value itself. |
||
| 154 | * |
||
| 155 | */ |
||
| 156 | 3 | public function setFlash($key, $val) |
|
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * |
||
| 165 | * Append a flash value with a numeric key for the *next* request. |
||
| 166 | * |
||
| 167 | * @param mixed $val The flash value itself. |
||
| 168 | * |
||
| 169 | */ |
||
| 170 | 1 | public function addFlash($val) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * |
||
| 178 | * Gets the flash value for a key in the *current* request. |
||
| 179 | * |
||
| 180 | * @param string $key The key for the flash value. |
||
| 181 | * |
||
| 182 | * @param mixed $alt An alternative value to return if the key is not set. |
||
| 183 | * |
||
| 184 | * @return mixed The flash value itself. |
||
| 185 | * |
||
| 186 | */ |
||
| 187 | 3 | public function getFlash($key, $alt = null) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * |
||
| 197 | * Gets all the flash values in the *current* request. |
||
| 198 | * |
||
| 199 | * @param mixed $alt An alternative value to return if no flash values are set. |
||
| 200 | * |
||
| 201 | * @return mixed The flash values themselves. |
||
| 202 | * |
||
| 203 | */ |
||
| 204 | 1 | public function getAllCurrentFlash($alt = array()) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * |
||
| 214 | * Clears flash values for *only* the next request. |
||
| 215 | * |
||
| 216 | * @return null |
||
| 217 | * |
||
| 218 | */ |
||
| 219 | 2 | public function clearFlash() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * |
||
| 228 | * Gets the flash value for a key in the *next* request. |
||
| 229 | * |
||
| 230 | * @param string $key The key for the flash value. |
||
| 231 | * |
||
| 232 | * @param mixed $alt An alternative value to return if the key is not set. |
||
| 233 | * |
||
| 234 | * @return mixed The flash value itself. |
||
| 235 | * |
||
| 236 | */ |
||
| 237 | 2 | public function getFlashNext($key, $alt = null) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * |
||
| 247 | * Gets all flash values for the *next* request. |
||
| 248 | * |
||
| 249 | * @param mixed $alt An alternative value to return if no flash values set. |
||
| 250 | * |
||
| 251 | * @return mixed The flash values themselves. |
||
| 252 | * |
||
| 253 | */ |
||
| 254 | 1 | public function getAllFlashNext($alt = array()) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * |
||
| 264 | * Sets a flash value for the *next* request *and* the current one. |
||
| 265 | * |
||
| 266 | * @param string $key The key for the flash value. |
||
| 267 | * |
||
| 268 | * @param mixed $val The flash value itself. |
||
| 269 | * |
||
| 270 | */ |
||
| 271 | 2 | public function setFlashNow($key, $val) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * |
||
| 280 | * Append a flash value with a numeric key for the *next* request *and* the current one. |
||
| 281 | * |
||
| 282 | * @param mixed $val The flash value itself. |
||
| 283 | * |
||
| 284 | */ |
||
| 285 | 1 | public function addFlashNow($val) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * |
||
| 294 | * Clears flash values for *both* the next request *and* the current one. |
||
| 295 | * |
||
| 296 | * @return null |
||
| 297 | * |
||
| 298 | */ |
||
| 299 | 2 | public function clearFlashNow() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * |
||
| 309 | * Retains all the current flash values for the next request; values that |
||
| 310 | * already exist for the next request take precedence. |
||
| 311 | * |
||
| 312 | * @return null |
||
| 313 | * |
||
| 314 | */ |
||
| 315 | 2 | public function keepFlash() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * |
||
| 327 | * Loads the segment only if the session has already been started, or if |
||
| 328 | * a session is available (in which case it resumes the session first). |
||
| 329 | * |
||
| 330 | * @return bool |
||
| 331 | * |
||
| 332 | */ |
||
| 333 | 21 | protected function resumeSession() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * |
||
| 345 | * Sets the segment properties to $_SESSION references. |
||
| 346 | * |
||
| 347 | * @return null |
||
| 348 | * |
||
| 349 | */ |
||
| 350 | 18 | protected function load() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * |
||
| 367 | * Resumes a previous session, or starts a new one, and loads the segment. |
||
| 368 | * |
||
| 369 | * @return null |
||
| 370 | * |
||
| 371 | */ |
||
| 372 | 17 | protected function resumeOrStartSession() |
|
| 379 | } |
||
| 380 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: