Complex classes like Mangan 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 Mangan, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class Mangan implements LoggerAwareInterface |
||
49 | { |
||
50 | |||
51 | const DefaultConnectionId = 'mongodb'; |
||
52 | |||
53 | use MongoClientOptions; |
||
54 | |||
55 | /** |
||
56 | * Correct syntax is: |
||
57 | * mongodb://[username:password@]host1[:port1][,host2[:port2:],...] |
||
58 | * @example mongodb://localhost:27017 |
||
59 | * @var string host:port |
||
60 | * @since v1.0 |
||
61 | */ |
||
62 | public $connectionString = 'mongodb://localhost:27017'; |
||
63 | |||
64 | /** |
||
65 | * Default annotations values configuration. This should contain |
||
66 | * array with keys same as annotation class name, and key-value |
||
67 | * pairs corresponding to annotation properties. |
||
68 | * |
||
69 | * Example: |
||
70 | * ``` |
||
71 | * $annotationsDefaults = [ |
||
72 | * I18NAnnotation::class => [ |
||
73 | * 'allowAny' => true, |
||
74 | * 'allowDefault' => true |
||
75 | * ] |
||
76 | * ]; |
||
77 | * ``` |
||
78 | * |
||
79 | */ |
||
80 | public $annotationsDefaults = []; |
||
81 | |||
82 | /** |
||
83 | * Configuration of decorators for transformers |
||
84 | * Array key is decorator class name or interface, values are decorator class names. |
||
85 | * @var string[][] |
||
86 | */ |
||
87 | public $decorators = []; |
||
88 | |||
89 | /** |
||
90 | * Configuration for finalizers. |
||
91 | * @see https://github.com/Maslosoft/Mangan/issues/36 |
||
92 | * @var string[][] |
||
93 | */ |
||
94 | public $finalizers = []; |
||
95 | |||
96 | /** |
||
97 | * Configuration of property filters for transformers |
||
98 | * Array key is decorator class name or interface, values are filter class names. |
||
99 | * @var string[][] |
||
100 | */ |
||
101 | public $filters = []; |
||
102 | |||
103 | /** |
||
104 | * Mapping for validators. Key is validator proxy class name, value is concrete validator implementation |
||
105 | * @var string[] |
||
106 | */ |
||
107 | public $validators = []; |
||
108 | |||
109 | /** |
||
110 | * Sanitizers remapping for common scenarios. |
||
111 | * @var string[][] |
||
112 | */ |
||
113 | public $sanitizersMap = []; |
||
114 | |||
115 | /** |
||
116 | * Event handlers to attach on initialization. |
||
117 | * |
||
118 | * This should be list of class names implementing `EventHandlersInterface` |
||
119 | * or optionally list of arrays with configuration for `EventHandlersInterface` |
||
120 | * derived classes. |
||
121 | * |
||
122 | * @see EventHandlersInterface |
||
123 | * @var mixed[] |
||
124 | */ |
||
125 | public $eventHandlers = []; |
||
126 | |||
127 | /** |
||
128 | * Connection ID |
||
129 | * @var string |
||
130 | */ |
||
131 | public $connectionId = 'mongodb'; |
||
132 | |||
133 | /** |
||
134 | * @var string $dbName name of the Mongo database to use |
||
135 | * @since v1.0 |
||
136 | */ |
||
137 | public $dbName = null; |
||
138 | |||
139 | /** |
||
140 | * If set to TRUE all internal DB operations will use SAFE flag with data modification requests. |
||
141 | * |
||
142 | * When SAFE flag is set to TRUE driver will wait for the response from DB, and throw an exception |
||
143 | * if something went wrong, is set to false, driver will only send operation to DB but will not wait |
||
144 | * for response from DB. |
||
145 | * |
||
146 | * MongoDB default value for this flag is: FALSE. |
||
147 | * |
||
148 | * @var boolean $safeFlag state of SAFE flag (global scope) |
||
149 | */ |
||
150 | public $safeFlag = false; |
||
151 | |||
152 | /** |
||
153 | * If set to TRUE findAll* methods of models, will return {@see Cursor} instead of |
||
154 | * raw array of models. |
||
155 | * |
||
156 | * Generally you should want to have this set to TRUE as cursor use lazy-loading/instantiating of |
||
157 | * models, this is set to FALSE, by default to keep backwards compatibility. |
||
158 | * |
||
159 | * Note: {@see Cursor} does not implement ArrayAccess interface and cannot be used like an array, |
||
160 | * because offset access to cursor is highly ineffective and pointless. |
||
161 | * |
||
162 | * @var boolean $useCursor state of Use Cursor flag (global scope) |
||
163 | */ |
||
164 | public $useCursor = false; |
||
165 | |||
166 | /** |
||
167 | * Queries profiling. |
||
168 | * Defaults to false. This should be mainly enabled and used during development |
||
169 | * to find out the bottleneck of mongo queries. |
||
170 | * @var boolean whether to enable profiling the mongo queries being executed. |
||
171 | */ |
||
172 | public $enableProfiling = false; |
||
173 | |||
174 | /** |
||
175 | * Connection storage |
||
176 | * @var ConnectionStorage |
||
177 | */ |
||
178 | private $cs = null; |
||
179 | |||
180 | /** |
||
181 | * Embedi instance |
||
182 | * @var EmbeDi |
||
183 | */ |
||
184 | private $di = null; |
||
185 | |||
186 | /** |
||
187 | * Logger |
||
188 | * @var LoggerInterface |
||
189 | */ |
||
190 | private $_logger = null; |
||
191 | |||
192 | /** |
||
193 | * Profiller |
||
194 | * @var ProfilerInterface |
||
195 | */ |
||
196 | private $_profiler = null; |
||
197 | |||
198 | /** |
||
199 | * Version number holder |
||
200 | * @var string |
||
201 | */ |
||
202 | private static $_version = null; |
||
203 | |||
204 | /** |
||
205 | * Instances of mangan |
||
206 | * @var Mangan[] |
||
207 | */ |
||
208 | private static $mn = []; |
||
209 | |||
210 | /** |
||
211 | * Hash map of class name to id. This is to reduce overhead of Mangan::fromModel() |
||
212 | * @var string[] |
||
213 | */ |
||
214 | private static $classToId = []; |
||
215 | |||
216 | /** |
||
217 | * Create new mangan instance. |
||
218 | * |
||
219 | * **NOTE: While it's ok to use constructor to create Mangan, it is recommended to use |
||
220 | * Mangan::fly() to create/get instance, as creating new instance has some overhead.** |
||
221 | * |
||
222 | * @param string $connectionId |
||
223 | */ |
||
224 | 4 | public function __construct($connectionId = self::DefaultConnectionId) |
|
264 | |||
265 | public function __get($name) |
||
269 | |||
270 | public function __set($name, $value) |
||
274 | |||
275 | /** |
||
276 | * Get mangan version |
||
277 | * @return string |
||
278 | */ |
||
279 | public function getVersion() |
||
287 | |||
288 | /** |
||
289 | * Set PSR compliant logger |
||
290 | * @param LoggerInterface $logger |
||
291 | * @return Mangan |
||
292 | */ |
||
293 | public function setLogger(LoggerInterface $logger) |
||
298 | |||
299 | /** |
||
300 | * Get PSR compliant logger |
||
301 | * @return LoggerInterface |
||
302 | */ |
||
303 | 1 | public function getLogger() |
|
311 | |||
312 | /** |
||
313 | * Get profiler instance. This is guaranteed, if not configured will return NullProfiler. |
||
314 | * @see NullProfiler |
||
315 | * @return ProfilerInterface |
||
316 | */ |
||
317 | 108 | public function getProfiler() |
|
329 | |||
330 | /** |
||
331 | * Set profiler instance |
||
332 | * @param ProfilerInterface $profiler |
||
333 | * @return Mangan |
||
334 | */ |
||
335 | public function setProfiler(ProfilerInterface $profiler) |
||
340 | |||
341 | /** |
||
342 | * Get dependency injector. |
||
343 | * @return EmbeDi |
||
344 | */ |
||
345 | 22 | public function getDi() |
|
349 | |||
350 | /** |
||
351 | * Get flyweight instance of Mangan component. |
||
352 | * Only one instance will be created for each `$connectionId`. |
||
353 | * |
||
354 | * @new |
||
355 | * @param string $connectionId |
||
356 | * @return Mangan |
||
357 | */ |
||
358 | 232 | public static function fly($connectionId = self::DefaultConnectionId) |
|
370 | |||
371 | /** |
||
372 | * Get instance of Mangan configured for particular model |
||
373 | * @param AnnotatedInterface|string $model |
||
374 | * @return static |
||
375 | */ |
||
376 | 206 | public static function fromModel($model) |
|
397 | |||
398 | public function init() |
||
402 | |||
403 | /** |
||
404 | * Connect to DB if connection is already connected this method return connection status. |
||
405 | * |
||
406 | * @return bool Returns true if connected |
||
407 | * @throws ManganException |
||
408 | */ |
||
409 | public function connect() |
||
417 | |||
418 | /** |
||
419 | * Returns Mongo connection instance if not exists will create new |
||
420 | * |
||
421 | * @return MongoClient |
||
422 | * @throws ManganException |
||
423 | * @since v1.0 |
||
424 | */ |
||
425 | 1 | public function getConnection() |
|
452 | |||
453 | /** |
||
454 | * Set the connection by supplying `MongoClient` instance. |
||
455 | * |
||
456 | * Use this to set connection from external source. |
||
457 | * In most scenarios this does not need to be called. |
||
458 | * |
||
459 | * @param MongoClient $connection |
||
460 | */ |
||
461 | public function setConnection(MongoClient $connection) |
||
465 | |||
466 | /** |
||
467 | * Get MongoDB instance |
||
468 | * |
||
469 | * @return MongoDB Mongo DB instance |
||
470 | * @throws ManganException |
||
471 | */ |
||
472 | 147 | public function getDbInstance() |
|
495 | |||
496 | /** |
||
497 | * Set MongoDB instance by supplying database name. |
||
498 | * |
||
499 | * Use this to select db from external source. |
||
500 | * In most scenarios this does not need to be called. |
||
501 | * |
||
502 | * @param string $name |
||
503 | * @throws ManganException |
||
504 | */ |
||
505 | 1 | public function setDbInstance($name) |
|
509 | |||
510 | /** |
||
511 | * Closes the currently active Mongo connection. |
||
512 | * It does nothing if the connection is already closed. |
||
513 | */ |
||
514 | protected function close() |
||
523 | |||
524 | /** |
||
525 | * Change working database |
||
526 | * @param $name |
||
527 | */ |
||
528 | 1 | public function selectDb($name) |
|
532 | |||
533 | /** |
||
534 | * Drop current database |
||
535 | */ |
||
536 | public function dropDb() |
||
540 | |||
541 | } |
||
542 |