Complex classes like Log 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 Log, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Log extends AbstractLogger implements LoggerInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Priorities |
||
| 26 | */ |
||
| 27 | const PRIORITIES = [ |
||
| 28 | LogLevel::EMERGENCY => 0, |
||
| 29 | LogLevel::ALERT => 1, |
||
| 30 | LogLevel::CRITICAL => 2, |
||
| 31 | LogLevel::ERROR => 3, |
||
| 32 | LogLevel::WARNING => 4, |
||
| 33 | LogLevel::NOTICE => 5, |
||
| 34 | LogLevel::INFO => 6, |
||
| 35 | LogLevel::DEBUG => 7, |
||
| 36 | ]; |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * Adapters |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $adapter = []; |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * static context |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $context = []; |
||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * Initialize logger |
||
| 57 | * |
||
| 58 | * @param Iterable $config |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | public function __construct(? Iterable $config = null) |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Set options |
||
| 69 | * |
||
| 70 | * @param Iterable $config |
||
| 71 | * @return Log |
||
| 72 | */ |
||
| 73 | public function setOptions(? Iterable $config = null): Log |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * Has adapter |
||
| 101 | * |
||
| 102 | * @param string $name |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | public function hasAdapter(string $name): bool |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * Add adapter |
||
| 113 | * |
||
| 114 | * @param string $name |
||
| 115 | * @param string $class |
||
| 116 | * @param Iterable $config |
||
| 117 | * @return AdapterInterface |
||
| 118 | */ |
||
| 119 | public function addAdapter(string $name, string $class, ? Iterable $config = null) : AdapterInterface |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Inject adapter |
||
| 136 | * |
||
| 137 | * @param string $name |
||
| 138 | * @param AdapterInterface $adapter |
||
| 139 | * @return AdapterInterface |
||
| 140 | */ |
||
| 141 | public function injectAdapter(string $name, AdapterInterface $adapter) : AdapterInterface |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Get adapter |
||
| 154 | * |
||
| 155 | * @param string $name |
||
| 156 | * @return AdapterInterface |
||
| 157 | */ |
||
| 158 | public function getAdapter(string $name): AdapterInterface |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Get adapters |
||
| 170 | * |
||
| 171 | * @param array $adapters |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | public function getAdapters(array $adapters = []): array |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Log message |
||
| 194 | * |
||
| 195 | * @param string $level |
||
| 196 | * @param string $message |
||
| 197 | * @param array $context |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | public function log($level, $message, array $context = []): bool |
||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * Add static context |
||
| 221 | * |
||
| 222 | * @param string $name |
||
| 223 | * @param string $value |
||
| 224 | * @return Log |
||
| 225 | */ |
||
| 226 | public function addContext(string $name, string $value): Log |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Log message |
||
| 235 | * |
||
| 236 | * @param string $message |
||
| 237 | * @param string $format |
||
| 238 | * @param string $date_format |
||
| 239 | * @param string $level |
||
| 240 | * @param array $context |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | protected function _format(string $message, string $format, string $date_format, string $level, array $context = []): string |
||
| 294 | } |
||
| 295 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: