Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AbstractSql 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 AbstractSql, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | abstract class AbstractSql |
||
36 | { |
||
37 | const TICKETS_TABLE = 'tickets'; |
||
38 | const PERFORMERS_TABLE = 'performers'; |
||
39 | const TICKETS_X_PERFORMERS_TABLE = 'tickets_x_performers'; |
||
40 | const CODE_LENGTH = 6; |
||
41 | |||
42 | /** |
||
43 | * @var Connection |
||
44 | */ |
||
45 | protected $dbConn; |
||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $upcomingCount = 3; |
||
50 | |||
51 | /** |
||
52 | * @var Logger |
||
53 | */ |
||
54 | protected $logger; |
||
55 | |||
56 | /** |
||
57 | * DataSource constructor. |
||
58 | * |
||
59 | * @param $dbConn |
||
60 | */ |
||
61 | public function __construct(Connection $dbConn) |
||
65 | |||
66 | /** |
||
67 | * @return Connection |
||
68 | */ |
||
69 | public function getDbConn() |
||
73 | |||
74 | /** |
||
75 | * @return int |
||
76 | */ |
||
77 | public function getUpcomingCount() |
||
81 | |||
82 | /** |
||
83 | * @param int $upcomingCount |
||
84 | */ |
||
85 | public function setUpcomingCount($upcomingCount) |
||
89 | |||
90 | /** |
||
91 | * @return Logger |
||
92 | */ |
||
93 | public function getLogger() |
||
101 | |||
102 | /** |
||
103 | * @param Logger $logger |
||
104 | */ |
||
105 | public function setLogger(Logger $logger) |
||
109 | |||
110 | /** |
||
111 | * @param $songId |
||
112 | * |
||
113 | * @return array |
||
114 | */ |
||
115 | public function fetchSongRowById($songId) |
||
124 | |||
125 | /** |
||
126 | * Fill out names of linked object ids for given ticket |
||
127 | * |
||
128 | * @param $ticket |
||
129 | * |
||
130 | * @return mixed |
||
131 | */ |
||
132 | public function expandTicketData($ticket) |
||
144 | |||
145 | /** |
||
146 | * expandTicketData for multiple tickets |
||
147 | * |
||
148 | * @param $tickets |
||
149 | * |
||
150 | * @return mixed |
||
151 | */ |
||
152 | public function expandTicketsData($tickets) |
||
160 | |||
161 | |||
162 | /** |
||
163 | * Fill out names of linked object ids for given song |
||
164 | * |
||
165 | * @param $song |
||
166 | * @return mixed |
||
167 | */ |
||
168 | public function expandSongData($song) |
||
198 | |||
199 | public function isPotentialCodeNumber($searchString) |
||
206 | |||
207 | /** |
||
208 | * Get performed, pending data for all performers |
||
209 | * |
||
210 | * @return array |
||
211 | */ |
||
212 | public function generatePerformerStats() |
||
226 | |||
227 | /** |
||
228 | * Save band to ticket |
||
229 | * |
||
230 | * @param $ticketId |
||
231 | * @param array $band ['instrumentCode' => 'name'] FIXME update |
||
232 | */ |
||
233 | public function storeBandToTicket($ticketId, $band) |
||
264 | |||
265 | public function fetchPerformerIdByName($performerName, $createMissing = false) |
||
280 | |||
281 | /** |
||
282 | * Fetch all performers on a song with their stats |
||
283 | * |
||
284 | * @deprecated Use fetchPerformersWithInstrumentByTicketId() |
||
285 | * |
||
286 | * @param $ticketId |
||
287 | * |
||
288 | * @return array[] |
||
289 | */ |
||
290 | public function fetchPerformersByTicketId($ticketId) |
||
309 | |||
310 | public function fetchPerformersWithInstrumentByTicketId($ticketId) |
||
340 | |||
341 | /** |
||
342 | * @param $searchString |
||
343 | * @param $howMany |
||
344 | * |
||
345 | * @return array |
||
346 | */ |
||
347 | public function findSongsBySearchString($searchString, $howMany = 10) |
||
396 | |||
397 | /** |
||
398 | * @param $id |
||
399 | * |
||
400 | * @return int |
||
401 | */ |
||
402 | public function markTicketUsedById($id) |
||
407 | |||
408 | /** |
||
409 | * @param $id |
||
410 | * |
||
411 | * @return int |
||
412 | */ |
||
413 | public function deleteTicketById($id) |
||
418 | |||
419 | /** |
||
420 | * @param $id |
||
421 | * @param $offset |
||
422 | * |
||
423 | * @return mixed |
||
424 | */ |
||
425 | public function updateTicketOffsetById($id, $offset) |
||
445 | |||
446 | /** |
||
447 | * @param $songKey |
||
448 | * |
||
449 | * @return array |
||
450 | */ |
||
451 | public function fetchSongByKey($songKey) |
||
462 | |||
463 | /** |
||
464 | * @param bool $includePrivate |
||
465 | * |
||
466 | * @return array|mixed |
||
467 | * |
||
468 | * @throws \Doctrine\DBAL\DBALException |
||
469 | */ |
||
470 | public function fetchUpcomingTickets($includePrivate = false) |
||
485 | |||
486 | /** |
||
487 | * Fetch all non-deleted tickets in offset order |
||
488 | * |
||
489 | * @return array|mixed |
||
490 | * |
||
491 | * @throws \Doctrine\DBAL\DBALException |
||
492 | */ |
||
493 | View Code Duplication | public function fetchUndeletedTickets() |
|
503 | |||
504 | /** |
||
505 | * Fetch all performed tickets in offset order |
||
506 | * |
||
507 | * @return array |
||
508 | * |
||
509 | * @throws \Doctrine\DBAL\DBALException |
||
510 | */ |
||
511 | View Code Duplication | public function fetchPerformedTickets() |
|
521 | |||
522 | /** |
||
523 | * @param $title |
||
524 | * @param $songId |
||
525 | * |
||
526 | * @return int|false Row ID |
||
527 | */ |
||
528 | public function storeNewTicket($title, $songId) |
||
545 | |||
546 | /** |
||
547 | * Normalise datatypes returned in song query |
||
548 | * |
||
549 | * @param $song |
||
550 | * |
||
551 | * @return mixed |
||
552 | */ |
||
553 | public function normaliseSongRecord($song) |
||
573 | |||
574 | /** |
||
575 | * @param $id |
||
576 | * @param $fields |
||
577 | * |
||
578 | * @return int Number of updated rows |
||
579 | */ |
||
580 | public function updateTicketById($id, $fields) |
||
588 | |||
589 | /** |
||
590 | * Fetch core ticket data (not band, etc) by ticket id |
||
591 | * |
||
592 | * @param $id |
||
593 | * |
||
594 | * @return array |
||
595 | */ |
||
596 | public function fetchTicketById($id) |
||
603 | |||
604 | /** |
||
605 | * Get current value of a named setting, NULL if missing |
||
606 | * |
||
607 | * @param $key |
||
608 | * @return mixed|null |
||
609 | */ |
||
610 | public function fetchSetting($key) |
||
617 | |||
618 | |||
619 | public function settingExists($key) |
||
626 | |||
627 | public function updateSetting($k, $v) |
||
637 | |||
638 | /** |
||
639 | * Return SQL in appropriate dialect to concatenate the listed values |
||
640 | * |
||
641 | * @param array $fields |
||
642 | * |
||
643 | * @return string |
||
644 | */ |
||
645 | abstract protected function concatenateEscapedFields($fields); |
||
646 | |||
647 | /** |
||
648 | * Return ticket array with fields converted to correct datatype |
||
649 | * |
||
650 | * @param $ticket |
||
651 | * |
||
652 | * @return mixed |
||
653 | */ |
||
654 | protected function normaliseTicketRecord($ticket) |
||
669 | |||
670 | /** |
||
671 | * Delete all song & performer data |
||
672 | * |
||
673 | * @throws \Doctrine\DBAL\DBALException |
||
674 | */ |
||
675 | public function resetAllSessionData() |
||
691 | |||
692 | /** |
||
693 | * Delete all catalogue data |
||
694 | * |
||
695 | * @throws \Doctrine\DBAL\DBALException |
||
696 | */ |
||
697 | public function resetCatalogue() |
||
718 | |||
719 | /** |
||
720 | * Store an instrument to DB |
||
721 | * |
||
722 | * @param Instrument $instrument |
||
723 | */ |
||
724 | public function storeInstrument(Instrument $instrument) |
||
731 | |||
732 | /** |
||
733 | * Store a platform to DB |
||
734 | * |
||
735 | * @param Platform $platform |
||
736 | */ |
||
737 | public function storePlatform(Platform $platform) |
||
744 | |||
745 | /** |
||
746 | * Store a source to DB |
||
747 | * |
||
748 | * @param Source $source |
||
749 | */ |
||
750 | public function storeSource(Source $source) |
||
757 | |||
758 | /** |
||
759 | * Store a song to DB |
||
760 | * |
||
761 | * @param Song $song |
||
762 | */ |
||
763 | public function storeSong(Song $song) |
||
770 | |||
771 | /** |
||
772 | * @param string $sourceName |
||
773 | * |
||
774 | * @return null|Source |
||
775 | */ |
||
776 | View Code Duplication | public function fetchSourceByName($sourceName) |
|
793 | |||
794 | |||
795 | /** |
||
796 | * @param $sourceId |
||
797 | * @return null|Source |
||
798 | */ |
||
799 | View Code Duplication | public function fetchSourceById($sourceId) |
|
816 | |||
817 | View Code Duplication | public function fetchPlatformByName($platformName) |
|
834 | |||
835 | /** |
||
836 | * @param $songId |
||
837 | * @param array $platformIds |
||
838 | */ |
||
839 | View Code Duplication | public function storeSongPlatformLinks($songId, array $platformIds) |
|
847 | |||
848 | /** |
||
849 | * @param $name |
||
850 | * @return null|Instrument |
||
851 | */ |
||
852 | View Code Duplication | public function fetchInstrumentByName($name) |
|
869 | |||
870 | View Code Duplication | protected function fetchInstrumentByAbbreviation($abbreviation) |
|
887 | |||
888 | |||
889 | /** |
||
890 | * @param $songId |
||
891 | * @param array $instrumentIds |
||
892 | * @throws \Doctrine\DBAL\Exception\InvalidArgumentException |
||
893 | */ |
||
894 | View Code Duplication | public function storeSongInstrumentLinks($songId, array $instrumentIds) |
|
902 | |||
903 | /** |
||
904 | * @param $songId |
||
905 | * @return Instrument[] |
||
906 | */ |
||
907 | View Code Duplication | public function fetchInstrumentsForSongId($songId) |
|
925 | |||
926 | /** |
||
927 | * @param $songId |
||
928 | * @return Platform[] |
||
929 | */ |
||
930 | View Code Duplication | public function fetchPlatformsForSongId($songId) |
|
948 | |||
949 | /** |
||
950 | * @param $row |
||
951 | * @return Instrument |
||
952 | */ |
||
953 | protected function buildInstrumentFromDbRow($row) |
||
963 | |||
964 | /** |
||
965 | * @param $row |
||
966 | * @return Platform |
||
967 | */ |
||
968 | protected function buildPlatformFromDbRow($row) |
||
976 | |||
977 | /** |
||
978 | * @param $row |
||
979 | * @return Source |
||
980 | */ |
||
981 | protected function buildSourceFromDbRow($row) |
||
989 | |||
990 | /** |
||
991 | * @param Song $song |
||
992 | * @return array |
||
993 | */ |
||
994 | protected function songToDbRow(Song $song) |
||
1008 | |||
1009 | /** |
||
1010 | * @param Source $source |
||
1011 | * @return array |
||
1012 | */ |
||
1013 | protected function sourceToDbRow(Source $source) |
||
1023 | |||
1024 | /** |
||
1025 | * @param Instrument $instrument |
||
1026 | * @return array |
||
1027 | */ |
||
1028 | protected function instrumentToDbRow(Instrument $instrument) |
||
1040 | |||
1041 | /** |
||
1042 | * @param Platform $platform |
||
1043 | * @return array |
||
1044 | */ |
||
1045 | protected function platformToDbRow(Platform $platform) |
||
1055 | } |
||
1056 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..