Complex classes like Nip_Profiler 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 Nip_Profiler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Nip_Profiler |
||
|
|
|||
| 11 | { |
||
| 12 | |||
| 13 | public $enabled = false; |
||
| 14 | |||
| 15 | public $profiles = []; |
||
| 16 | public $runningProfiles = []; |
||
| 17 | public $filterElapsedSecs = null; |
||
| 18 | /** |
||
| 19 | * @var AbstractAdapter[] |
||
| 20 | */ |
||
| 21 | protected $writers = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Singleton |
||
| 25 | * |
||
| 26 | * @return Nip_Profiler |
||
| 27 | */ |
||
| 28 | public static function instance() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param bool $enabled |
||
| 40 | * @return $this |
||
| 41 | */ |
||
| 42 | public function setEnabled($enabled = false) |
||
| 47 | |||
| 48 | public function clear() |
||
| 54 | |||
| 55 | |||
| 56 | public function start($profileName = false) |
||
| 71 | |||
| 72 | public function checkEnabled() |
||
| 76 | |||
| 77 | public function newProfileID($name) |
||
| 85 | |||
| 86 | public function getProfiles($type = null, $showUnfinished = false) |
||
| 108 | |||
| 109 | public function newProfile($id) |
||
| 113 | |||
| 114 | public function addRunningProces($profileID) |
||
| 118 | |||
| 119 | public function end($profileID = false) |
||
| 130 | |||
| 131 | protected function endPreckeck($profileID) |
||
| 142 | |||
| 143 | public function getLastRunningProces() |
||
| 147 | |||
| 148 | public function endProfile($profileID) |
||
| 160 | |||
| 161 | public function getProfile($profileID) |
||
| 175 | |||
| 176 | protected function applyFilters($profile) |
||
| 180 | |||
| 181 | public function secondsFilter($profile) |
||
| 189 | |||
| 190 | public function deleteProfile($profile) |
||
| 198 | |||
| 199 | public function outputWriters($profile) |
||
| 205 | |||
| 206 | public function lastProcessID() |
||
| 212 | |||
| 213 | public function setFilterElapsedSecs($minimumSeconds = null) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param AbstractAdapter $writer |
||
| 226 | */ |
||
| 227 | public function addWriter(AbstractAdapter $writer) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param $name |
||
| 234 | * @return AbstractAdapter |
||
| 235 | */ |
||
| 236 | public function newWriter($name) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param $name |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function newWriterClass($name) |
||
| 251 | } |
||
| 252 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.