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 DatabaseSqlite 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 DatabaseSqlite, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class DatabaseSqlite extends Database { |
||
29 | /** @var bool Whether full text is enabled */ |
||
30 | private static $fulltextEnabled = null; |
||
31 | |||
32 | /** @var string Directory */ |
||
33 | protected $dbDir; |
||
34 | /** @var string File name for SQLite database file */ |
||
35 | protected $dbPath; |
||
36 | /** @var string Transaction mode */ |
||
37 | protected $trxMode; |
||
38 | |||
39 | /** @var int The number of rows affected as an integer */ |
||
40 | protected $mAffectedRows; |
||
41 | /** @var resource */ |
||
42 | protected $mLastResult; |
||
43 | |||
44 | /** @var $mConn PDO */ |
||
45 | protected $mConn; |
||
46 | |||
47 | /** @var FSLockManager (hopefully on the same server as the DB) */ |
||
48 | protected $lockMgr; |
||
49 | |||
50 | /** |
||
51 | * Additional params include: |
||
52 | * - dbDirectory : directory containing the DB and the lock file directory |
||
53 | * [defaults to $wgSQLiteDataDir] |
||
54 | * - dbFilePath : use this to force the path of the DB file |
||
55 | * - trxMode : one of (deferred, immediate, exclusive) |
||
56 | * @param array $p |
||
57 | */ |
||
58 | function __construct( array $p ) { |
||
102 | |||
103 | /** |
||
104 | * @param string $filename |
||
105 | * @param array $p Options map; supports: |
||
106 | * - flags : (same as __construct counterpart) |
||
107 | * - trxMode : (same as __construct counterpart) |
||
108 | * - dbDirectory : (same as __construct counterpart) |
||
109 | * @return DatabaseSqlite |
||
110 | * @since 1.25 |
||
111 | */ |
||
112 | public static function newStandaloneInstance( $filename, array $p = [] ) { |
||
119 | |||
120 | /** |
||
121 | * @return string |
||
122 | */ |
||
123 | function getType() { |
||
126 | |||
127 | /** |
||
128 | * @todo Check if it should be true like parent class |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | function implicitGroupby() { |
||
135 | |||
136 | /** Open an SQLite database and return a resource handle to it |
||
137 | * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases |
||
138 | * |
||
139 | * @param string $server |
||
140 | * @param string $user |
||
141 | * @param string $pass |
||
142 | * @param string $dbName |
||
143 | * |
||
144 | * @throws DBConnectionError |
||
145 | * @return bool |
||
146 | */ |
||
147 | function open( $server, $user, $pass, $dbName ) { |
||
158 | |||
159 | /** |
||
160 | * Opens a database file |
||
161 | * |
||
162 | * @param string $fileName |
||
163 | * @throws DBConnectionError |
||
164 | * @return PDO|bool SQL connection or false if failed |
||
165 | */ |
||
166 | protected function openFile( $fileName ) { |
||
198 | |||
199 | public function selectDB( $db ) { |
||
202 | |||
203 | /** |
||
204 | * @return string SQLite DB file path |
||
205 | * @since 1.25 |
||
206 | */ |
||
207 | public function getDbFilePath() { |
||
210 | |||
211 | /** |
||
212 | * Does not actually close the connection, just destroys the reference for GC to do its work |
||
213 | * @return bool |
||
214 | */ |
||
215 | protected function closeConnection() { |
||
220 | |||
221 | /** |
||
222 | * Generates a database file name. Explicitly public for installer. |
||
223 | * @param string $dir Directory where database resides |
||
224 | * @param string $dbName Database name |
||
225 | * @return string |
||
226 | */ |
||
227 | public static function generateFileName( $dir, $dbName ) { |
||
230 | |||
231 | /** |
||
232 | * Check if the searchindext table is FTS enabled. |
||
233 | * @return bool False if not enabled. |
||
234 | */ |
||
235 | function checkForEnabledSearch() { |
||
248 | |||
249 | /** |
||
250 | * Returns version of currently supported SQLite fulltext search module or false if none present. |
||
251 | * @return string |
||
252 | */ |
||
253 | static function getFulltextSearchModule() { |
||
269 | |||
270 | /** |
||
271 | * Attaches external database to our connection, see http://sqlite.org/lang_attach.html |
||
272 | * for details. |
||
273 | * |
||
274 | * @param string $name Database name to be used in queries like |
||
275 | * SELECT foo FROM dbname.table |
||
276 | * @param bool|string $file Database file name. If omitted, will be generated |
||
277 | * using $name and configured data directory |
||
278 | * @param string $fname Calling function name |
||
279 | * @return ResultWrapper |
||
280 | */ |
||
281 | function attachDatabase( $name, $file = false, $fname = __METHOD__ ) { |
||
289 | |||
290 | function isWriteQuery( $sql ) { |
||
293 | |||
294 | /** |
||
295 | * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result |
||
296 | * |
||
297 | * @param string $sql |
||
298 | * @return bool|ResultWrapper |
||
299 | */ |
||
300 | protected function doQuery( $sql ) { |
||
312 | |||
313 | /** |
||
314 | * @param ResultWrapper|mixed $res |
||
315 | */ |
||
316 | function freeResult( $res ) { |
||
323 | |||
324 | /** |
||
325 | * @param ResultWrapper|array $res |
||
326 | * @return stdClass|bool |
||
327 | */ |
||
328 | function fetchObject( $res ) { |
||
350 | |||
351 | /** |
||
352 | * @param ResultWrapper|mixed $res |
||
353 | * @return array|bool |
||
354 | */ |
||
355 | function fetchRow( $res ) { |
||
370 | |||
371 | /** |
||
372 | * The PDO::Statement class implements the array interface so count() will work |
||
373 | * |
||
374 | * @param ResultWrapper|array $res |
||
375 | * @return int |
||
376 | */ |
||
377 | function numRows( $res ) { |
||
382 | |||
383 | /** |
||
384 | * @param ResultWrapper $res |
||
385 | * @return int |
||
386 | */ |
||
387 | function numFields( $res ) { |
||
397 | |||
398 | /** |
||
399 | * @param ResultWrapper $res |
||
400 | * @param int $n |
||
401 | * @return bool |
||
402 | */ |
||
403 | function fieldName( $res, $n ) { |
||
413 | |||
414 | /** |
||
415 | * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks |
||
416 | * |
||
417 | * @param string $name |
||
418 | * @param string $format |
||
419 | * @return string |
||
420 | */ |
||
421 | function tableName( $name, $format = 'quoted' ) { |
||
429 | |||
430 | /** |
||
431 | * This must be called after nextSequenceVal |
||
432 | * |
||
433 | * @return int |
||
434 | */ |
||
435 | function insertId() { |
||
439 | |||
440 | /** |
||
441 | * @param ResultWrapper|array $res |
||
442 | * @param int $row |
||
443 | */ |
||
444 | function dataSeek( $res, $row ) { |
||
457 | |||
458 | /** |
||
459 | * @return string |
||
460 | */ |
||
461 | function lastError() { |
||
469 | |||
470 | /** |
||
471 | * @return string |
||
472 | */ |
||
473 | function lastErrno() { |
||
482 | |||
483 | /** |
||
484 | * @return int |
||
485 | */ |
||
486 | function affectedRows() { |
||
489 | |||
490 | /** |
||
491 | * Returns information about an index |
||
492 | * Returns false if the index does not exist |
||
493 | * - if errors are explicitly ignored, returns NULL on failure |
||
494 | * |
||
495 | * @param string $table |
||
496 | * @param string $index |
||
497 | * @param string $fname |
||
498 | * @return array|false |
||
499 | */ |
||
500 | function indexInfo( $table, $index, $fname = __METHOD__ ) { |
||
513 | |||
514 | /** |
||
515 | * @param string $table |
||
516 | * @param string $index |
||
517 | * @param string $fname |
||
518 | * @return bool|null |
||
519 | */ |
||
520 | function indexUnique( $table, $index, $fname = __METHOD__ ) { |
||
540 | |||
541 | /** |
||
542 | * Filter the options used in SELECT statements |
||
543 | * |
||
544 | * @param array $options |
||
545 | * @return array |
||
546 | */ |
||
547 | function makeSelectOptions( $options ) { |
||
556 | |||
557 | /** |
||
558 | * @param array $options |
||
559 | * @return string |
||
560 | */ |
||
561 | protected function makeUpdateOptionsArray( $options ) { |
||
567 | |||
568 | /** |
||
569 | * @param array $options |
||
570 | * @return array |
||
571 | */ |
||
572 | static function fixIgnore( $options ) { |
||
582 | |||
583 | /** |
||
584 | * @param array $options |
||
585 | * @return string |
||
586 | */ |
||
587 | function makeInsertOptions( $options ) { |
||
592 | |||
593 | /** |
||
594 | * Based on generic method (parent) with some prior SQLite-sepcific adjustments |
||
595 | * @param string $table |
||
596 | * @param array $a |
||
597 | * @param string $fname |
||
598 | * @param array $options |
||
599 | * @return bool |
||
600 | */ |
||
601 | function insert( $table, $a, $fname = __METHOD__, $options = [] ) { |
||
620 | |||
621 | /** |
||
622 | * @param string $table |
||
623 | * @param array $uniqueIndexes Unused |
||
624 | * @param string|array $rows |
||
625 | * @param string $fname |
||
626 | * @return bool|ResultWrapper |
||
627 | */ |
||
628 | function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) { |
||
647 | |||
648 | /** |
||
649 | * Returns the size of a text field, or -1 for "unlimited" |
||
650 | * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though. |
||
651 | * |
||
652 | * @param string $table |
||
653 | * @param string $field |
||
654 | * @return int |
||
655 | */ |
||
656 | function textFieldSize( $table, $field ) { |
||
659 | |||
660 | /** |
||
661 | * @return bool |
||
662 | */ |
||
663 | function unionSupportsOrderAndLimit() { |
||
666 | |||
667 | /** |
||
668 | * @param string $sqls |
||
669 | * @param bool $all Whether to "UNION ALL" or not |
||
670 | * @return string |
||
671 | */ |
||
672 | function unionQueries( $sqls, $all ) { |
||
677 | |||
678 | /** |
||
679 | * @return bool |
||
680 | */ |
||
681 | function wasDeadlock() { |
||
684 | |||
685 | /** |
||
686 | * @return bool |
||
687 | */ |
||
688 | function wasErrorReissuable() { |
||
691 | |||
692 | /** |
||
693 | * @return bool |
||
694 | */ |
||
695 | function wasReadOnlyError() { |
||
698 | |||
699 | /** |
||
700 | * @return string Wikitext of a link to the server software's web site |
||
701 | */ |
||
702 | public function getSoftwareLink() { |
||
705 | |||
706 | /** |
||
707 | * @return string Version information from the database |
||
708 | */ |
||
709 | function getServerVersion() { |
||
714 | |||
715 | /** |
||
716 | * Get information about a given field |
||
717 | * Returns false if the field does not exist. |
||
718 | * |
||
719 | * @param string $table |
||
720 | * @param string $field |
||
721 | * @return SQLiteField|bool False on failure |
||
722 | */ |
||
723 | function fieldInfo( $table, $field ) { |
||
735 | |||
736 | protected function doBegin( $fname = '' ) { |
||
744 | |||
745 | /** |
||
746 | * @param string $s |
||
747 | * @return string |
||
748 | */ |
||
749 | function strencode( $s ) { |
||
752 | |||
753 | /** |
||
754 | * @param string $b |
||
755 | * @return Blob |
||
756 | */ |
||
757 | function encodeBlob( $b ) { |
||
760 | |||
761 | /** |
||
762 | * @param Blob|string $b |
||
763 | * @return string |
||
764 | */ |
||
765 | function decodeBlob( $b ) { |
||
772 | |||
773 | /** |
||
774 | * @param string|int|null|bool|Blob $s |
||
775 | * @return string|int |
||
776 | */ |
||
777 | function addQuotes( $s ) { |
||
803 | |||
804 | /** |
||
805 | * @return string |
||
806 | */ |
||
807 | View Code Duplication | function buildLike() { |
|
815 | |||
816 | /** |
||
817 | * @param string $field Field or column to cast |
||
818 | * @return string |
||
819 | * @since 1.28 |
||
820 | */ |
||
821 | public function buildStringCast( $field ) { |
||
824 | |||
825 | /** |
||
826 | * No-op version of deadlockLoop |
||
827 | * |
||
828 | * @return mixed |
||
829 | */ |
||
830 | public function deadlockLoop( /*...*/ ) { |
||
836 | |||
837 | /** |
||
838 | * @param string $s |
||
839 | * @return string |
||
840 | */ |
||
841 | protected function replaceVars( $s ) { |
||
893 | |||
894 | public function lock( $lockName, $method, $timeout = 5 ) { |
||
903 | |||
904 | public function unlock( $lockName, $method ) { |
||
907 | |||
908 | /** |
||
909 | * Build a concatenation list to feed into a SQL query |
||
910 | * |
||
911 | * @param string[] $stringList |
||
912 | * @return string |
||
913 | */ |
||
914 | function buildConcat( $stringList ) { |
||
917 | |||
918 | View Code Duplication | public function buildGroupConcatField( |
|
925 | |||
926 | /** |
||
927 | * @param string $oldName |
||
928 | * @param string $newName |
||
929 | * @param bool $temporary |
||
930 | * @param string $fname |
||
931 | * @return bool|ResultWrapper |
||
932 | * @throws RuntimeException |
||
933 | */ |
||
934 | function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__ ) { |
||
988 | |||
989 | /** |
||
990 | * List all tables on the database |
||
991 | * |
||
992 | * @param string $prefix Only show tables with this prefix, e.g. mw_ |
||
993 | * @param string $fname Calling function name |
||
994 | * |
||
995 | * @return array |
||
996 | */ |
||
997 | function listTables( $prefix = null, $fname = __METHOD__ ) { |
||
1019 | |||
1020 | /** |
||
1021 | * Override due to no CASCADE support |
||
1022 | * |
||
1023 | * @param string $tableName |
||
1024 | * @param string $fName |
||
1025 | * @return bool|ResultWrapper |
||
1026 | * @throws DBReadOnlyError |
||
1027 | */ |
||
1028 | View Code Duplication | public function dropTable( $tableName, $fName = __METHOD__ ) { |
|
1036 | |||
1037 | protected function requiresDatabaseUser() { |
||
1040 | |||
1041 | /** |
||
1042 | * @return string |
||
1043 | */ |
||
1044 | public function __toString() { |
||
1047 | } |
||
1048 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: