Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Connection extends IlluminateConnection |
||
26 | { |
||
27 | use DetectsDeadlocks, |
||
28 | DetectsLostConnections, |
||
29 | ManagesTransactions; |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $defaultConfig = [ |
||
37 | ArangoConnectionOptions::OPTION_ENDPOINT => 'tcp://localhost:8529', |
||
38 | ArangoConnectionOptions::OPTION_CONNECTION => 'Keep-Alive', |
||
39 | ArangoConnectionOptions::OPTION_AUTH_USER => null, |
||
40 | ArangoConnectionOptions::OPTION_AUTH_PASSWD => null, |
||
41 | 'tablePrefix' => '', |
||
42 | ]; |
||
43 | |||
44 | protected $config; |
||
45 | |||
46 | protected $arangoConnection; |
||
47 | |||
48 | protected $readArangoConnection; |
||
49 | |||
50 | protected $reconnector; |
||
51 | |||
52 | protected $database; |
||
53 | |||
54 | protected $schemaGrammar; |
||
55 | |||
56 | protected $queryGrammar; |
||
57 | |||
58 | protected $pretending; |
||
59 | |||
60 | protected $recordsModified; |
||
61 | |||
62 | protected $loggingQueries; |
||
63 | |||
64 | protected $queryLog; |
||
65 | |||
66 | protected $collectionHandler; |
||
67 | |||
68 | protected $viewHandler; |
||
69 | |||
70 | protected $documentHandler; |
||
71 | |||
72 | protected $graphHandler; |
||
73 | |||
74 | protected $userHandler; |
||
75 | |||
76 | /** |
||
77 | * The ArangoDB driver name. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $driverName = 'arangodb'; |
||
82 | |||
83 | /** |
||
84 | * Connection constructor. |
||
85 | * |
||
86 | * @param array $config |
||
87 | * @throws Exception |
||
88 | */ |
||
89 | public function __construct($config = []) |
||
109 | |||
110 | /** |
||
111 | * Get a schema builder instance for the connection. |
||
112 | * |
||
113 | * @return \LaravelFreelancerNL\Aranguent\Schema\Builder |
||
114 | */ |
||
115 | public function getSchemaBuilder() |
||
123 | |||
124 | /** |
||
125 | * Get the default query grammar instance. |
||
126 | * |
||
127 | * @return QueryGrammar |
||
128 | */ |
||
129 | protected function getDefaultQueryGrammar() |
||
133 | |||
134 | /** |
||
135 | * Get the default post processor instance. |
||
136 | * |
||
137 | * @return Processor |
||
138 | */ |
||
139 | protected function getDefaultPostProcessor() |
||
143 | |||
144 | /** |
||
145 | * Run a select statement against the database and returns a generator. |
||
146 | * ($useReadPdo is a dummy to adhere to the interface). |
||
147 | * |
||
148 | * @param string $query |
||
149 | * @param array $bindings |
||
150 | * @param bool $useReadPdo |
||
151 | * @param array|null $transactionCollections |
||
152 | * @return Iterator|null |
||
153 | * @throws Exception |
||
154 | */ |
||
155 | public function cursor($query, $bindings = [], $useReadPdo = null, $transactionCollections = null) |
||
172 | |||
173 | /** |
||
174 | * Execute an AQL statement and return the boolean result. |
||
175 | * |
||
176 | * @param string|FluentAQL $query |
||
177 | * @param array $bindings |
||
178 | * @param array|null $transactionCollections |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function statement($query, $bindings = [], $transactionCollections = null) |
||
209 | |||
210 | /** |
||
211 | * Run an AQL statement and get the number of rows affected. |
||
212 | * |
||
213 | * @param string|FluentAQL $query |
||
214 | * @param array $bindings |
||
215 | * @param array|null $transactionCollections |
||
216 | * @return int |
||
217 | */ |
||
218 | public function affectingStatement($query, $bindings = [], $transactionCollections = null) |
||
250 | |||
251 | /** |
||
252 | * Run a raw, unprepared query against the connection. |
||
253 | * |
||
254 | * @param string $query |
||
255 | * @return bool |
||
256 | */ |
||
257 | public function unprepared($query) |
||
282 | |||
283 | /** |
||
284 | * Returns the query execution plan. The query will not be executed. |
||
285 | * |
||
286 | * @param string $query |
||
287 | * @param array $bindings |
||
288 | * @return array |
||
289 | * @throws Exception |
||
290 | */ |
||
291 | public function explain($query, $bindings = []) |
||
297 | |||
298 | /** |
||
299 | * Run a select statement against the database. |
||
300 | * |
||
301 | * @param string|FluentAQL $query |
||
302 | * @param array $bindings |
||
303 | * @param bool $useReadPdo |
||
304 | * @param null|array $transactionCollections |
||
305 | * @return array |
||
306 | */ |
||
307 | public function select($query, $bindings = [], $useReadPdo = true, $transactionCollections = null) |
||
311 | |||
312 | /** |
||
313 | * Run an AQL query against the database and return the results. |
||
314 | * |
||
315 | * @param string|FluentAQL $query |
||
316 | * @param array $bindings |
||
317 | * @param bool $useReadPdo |
||
318 | * @param null|array $transactionCollections |
||
319 | * @return array |
||
320 | */ |
||
321 | public function execute($query, $bindings = [], $useReadPdo = true, $transactionCollections = null) |
||
345 | |||
346 | /** |
||
347 | * Run an insert statement against the database. |
||
348 | * |
||
349 | * @param string|FluentAQL $query |
||
350 | * @param array $bindings |
||
351 | * @param array|null $transactionCollections |
||
352 | * @return bool |
||
353 | */ |
||
354 | public function insert($query, $bindings = [], $transactionCollections = null) |
||
358 | |||
359 | /** |
||
360 | * Run an update statement against the database. |
||
361 | * |
||
362 | * @param string|FluentAQL $query |
||
363 | * @param array $bindings |
||
364 | * @param array|null $transactionCollections |
||
365 | * @return int |
||
366 | */ |
||
367 | public function update($query, $bindings = [], $transactionCollections = null) |
||
371 | |||
372 | /** |
||
373 | * Run a delete statement against the database. |
||
374 | * |
||
375 | * @param string $query |
||
376 | * @param array $bindings |
||
377 | * @param array|null $transactionCollections |
||
378 | * @return int |
||
379 | */ |
||
380 | public function delete($query, $bindings = [], $transactionCollections = null) |
||
384 | |||
385 | /** |
||
386 | * Get a new query builder instance. |
||
387 | * |
||
388 | * @return QueryBuilder |
||
389 | */ |
||
390 | public function query() |
||
396 | |||
397 | /** |
||
398 | * Get the collection prefix for the connection. |
||
399 | * |
||
400 | * @return string |
||
401 | */ |
||
402 | public function getTablePrefix() |
||
406 | |||
407 | /** |
||
408 | * Disconnect from the underlying ArangoDB connection. |
||
409 | * |
||
410 | * @return void |
||
411 | */ |
||
412 | public function disconnect() |
||
418 | |||
419 | /** |
||
420 | * Reconnect to the database if a Arango connection is missing. |
||
421 | * |
||
422 | * @return void |
||
423 | */ |
||
424 | protected function reconnectIfMissingConnection() |
||
430 | |||
431 | public function getArangoConnection() |
||
435 | |||
436 | /** |
||
437 | * @param $query |
||
438 | * @param $bindings |
||
439 | * @param $connection |
||
440 | * @return Statement |
||
441 | * @throws Exception |
||
442 | */ |
||
443 | public function newArangoStatement($query, $bindings): Statement |
||
450 | |||
451 | public function getCollectionHandler() |
||
460 | |||
461 | public function getDocumentHandler() |
||
470 | |||
471 | public function getUserHandler() |
||
480 | |||
481 | public function getGraphHandler() |
||
490 | |||
491 | public function getViewHandler() |
||
500 | |||
501 | public function setDatabaseName($database) |
||
506 | |||
507 | public function getDatabaseName() |
||
511 | } |
||
512 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.