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 | |||
| 35 | /** @var string File name for SQLite database file */ |
||
| 36 | protected $dbPath; |
||
| 37 | |||
| 38 | /** @var string Transaction mode */ |
||
| 39 | protected $trxMode; |
||
| 40 | |||
| 41 | /** @var int The number of rows affected as an integer */ |
||
| 42 | protected $mAffectedRows; |
||
| 43 | |||
| 44 | /** @var resource */ |
||
| 45 | protected $mLastResult; |
||
| 46 | |||
| 47 | /** @var PDO */ |
||
| 48 | protected $mConn; |
||
| 49 | |||
| 50 | /** @var FSLockManager (hopefully on the same server as the DB) */ |
||
| 51 | protected $lockMgr; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Additional params include: |
||
| 55 | * - dbDirectory : directory containing the DB and the lock file directory |
||
| 56 | * [defaults to $wgSQLiteDataDir] |
||
| 57 | * - dbFilePath : use this to force the path of the DB file |
||
| 58 | * - trxMode : one of (deferred, immediate, exclusive) |
||
| 59 | * @param array $p |
||
| 60 | */ |
||
| 61 | function __construct( array $p ) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $filename |
||
| 99 | * @param array $p Options map; supports: |
||
| 100 | * - flags : (same as __construct counterpart) |
||
| 101 | * - trxMode : (same as __construct counterpart) |
||
| 102 | * - dbDirectory : (same as __construct counterpart) |
||
| 103 | * @return DatabaseSqlite |
||
| 104 | * @since 1.25 |
||
| 105 | */ |
||
| 106 | public static function newStandaloneInstance( $filename, array $p = [] ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | function getType() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @todo Check if it should be true like parent class |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | function implicitGroupby() { |
||
| 129 | |||
| 130 | /** Open an SQLite database and return a resource handle to it |
||
| 131 | * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases |
||
| 132 | * |
||
| 133 | * @param string $server |
||
| 134 | * @param string $user |
||
| 135 | * @param string $pass |
||
| 136 | * @param string $dbName |
||
| 137 | * |
||
| 138 | * @throws DBConnectionError |
||
| 139 | * @return PDO |
||
| 140 | */ |
||
| 141 | function open( $server, $user, $pass, $dbName ) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Opens a database file |
||
| 155 | * |
||
| 156 | * @param string $fileName |
||
| 157 | * @throws DBConnectionError |
||
| 158 | * @return PDO|bool SQL connection or false if failed |
||
| 159 | */ |
||
| 160 | protected function openFile( $fileName ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return string SQLite DB file path |
||
| 195 | * @since 1.25 |
||
| 196 | */ |
||
| 197 | public function getDbFilePath() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Does not actually close the connection, just destroys the reference for GC to do its work |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | protected function closeConnection() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Generates a database file name. Explicitly public for installer. |
||
| 213 | * @param string $dir Directory where database resides |
||
| 214 | * @param string $dbName Database name |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public static function generateFileName( $dir, $dbName ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Check if the searchindext table is FTS enabled. |
||
| 223 | * @return bool False if not enabled. |
||
| 224 | */ |
||
| 225 | function checkForEnabledSearch() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Returns version of currently supported SQLite fulltext search module or false if none present. |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | static function getFulltextSearchModule() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Attaches external database to our connection, see http://sqlite.org/lang_attach.html |
||
| 262 | * for details. |
||
| 263 | * |
||
| 264 | * @param string $name Database name to be used in queries like |
||
| 265 | * SELECT foo FROM dbname.table |
||
| 266 | * @param bool|string $file Database file name. If omitted, will be generated |
||
| 267 | * using $name and configured data directory |
||
| 268 | * @param string $fname Calling function name |
||
| 269 | * @return ResultWrapper |
||
| 270 | */ |
||
| 271 | function attachDatabase( $name, $file = false, $fname = __METHOD__ ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @see DatabaseBase::isWriteQuery() |
||
| 282 | * |
||
| 283 | * @param string $sql |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | function isWriteQuery( $sql ) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result |
||
| 292 | * |
||
| 293 | * @param string $sql |
||
| 294 | * @return bool|ResultWrapper |
||
| 295 | */ |
||
| 296 | protected function doQuery( $sql ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param ResultWrapper|mixed $res |
||
| 311 | */ |
||
| 312 | function freeResult( $res ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param ResultWrapper|array $res |
||
| 322 | * @return stdClass|bool |
||
| 323 | */ |
||
| 324 | function fetchObject( $res ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param ResultWrapper|mixed $res |
||
| 349 | * @return array|bool |
||
| 350 | */ |
||
| 351 | function fetchRow( $res ) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * The PDO::Statement class implements the array interface so count() will work |
||
| 369 | * |
||
| 370 | * @param ResultWrapper|array $res |
||
| 371 | * @return int |
||
| 372 | */ |
||
| 373 | function numRows( $res ) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param ResultWrapper $res |
||
| 381 | * @return int |
||
| 382 | */ |
||
| 383 | function numFields( $res ) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param ResultWrapper $res |
||
| 396 | * @param int $n |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | function fieldName( $res, $n ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks |
||
| 412 | * |
||
| 413 | * @param string $name |
||
| 414 | * @param string $format |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | function tableName( $name, $format = 'quoted' ) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Index names have DB scope |
||
| 428 | * |
||
| 429 | * @param string $index |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | protected function indexName( $index ) { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * This must be called after nextSequenceVal |
||
| 438 | * |
||
| 439 | * @return int |
||
| 440 | */ |
||
| 441 | function insertId() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @param ResultWrapper|array $res |
||
| 448 | * @param int $row |
||
| 449 | */ |
||
| 450 | function dataSeek( $res, $row ) { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | function lastError() { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | function lastErrno() { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @return int |
||
| 491 | */ |
||
| 492 | function affectedRows() { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Returns information about an index |
||
| 498 | * Returns false if the index does not exist |
||
| 499 | * - if errors are explicitly ignored, returns NULL on failure |
||
| 500 | * |
||
| 501 | * @param string $table |
||
| 502 | * @param string $index |
||
| 503 | * @param string $fname |
||
| 504 | * @return array |
||
| 505 | */ |
||
| 506 | function indexInfo( $table, $index, $fname = __METHOD__ ) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @param string $table |
||
| 525 | * @param string $index |
||
| 526 | * @param string $fname |
||
| 527 | * @return bool|null |
||
| 528 | */ |
||
| 529 | function indexUnique( $table, $index, $fname = __METHOD__ ) { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Filter the options used in SELECT statements |
||
| 552 | * |
||
| 553 | * @param array $options |
||
| 554 | * @return array |
||
| 555 | */ |
||
| 556 | function makeSelectOptions( $options ) { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @param array $options |
||
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | protected function makeUpdateOptionsArray( $options ) { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @param array $options |
||
| 579 | * @return array |
||
| 580 | */ |
||
| 581 | static function fixIgnore( $options ) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @param array $options |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | function makeInsertOptions( $options ) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Based on generic method (parent) with some prior SQLite-sepcific adjustments |
||
| 604 | * @param string $table |
||
| 605 | * @param array $a |
||
| 606 | * @param string $fname |
||
| 607 | * @param array $options |
||
| 608 | * @return bool |
||
| 609 | */ |
||
| 610 | function insert( $table, $a, $fname = __METHOD__, $options = [] ) { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @param string $table |
||
| 632 | * @param array $uniqueIndexes Unused |
||
| 633 | * @param string|array $rows |
||
| 634 | * @param string $fname |
||
| 635 | * @return bool|ResultWrapper |
||
| 636 | */ |
||
| 637 | function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) { |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Returns the size of a text field, or -1 for "unlimited" |
||
| 659 | * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though. |
||
| 660 | * |
||
| 661 | * @param string $table |
||
| 662 | * @param string $field |
||
| 663 | * @return int |
||
| 664 | */ |
||
| 665 | function textFieldSize( $table, $field ) { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @return bool |
||
| 671 | */ |
||
| 672 | function unionSupportsOrderAndLimit() { |
||
| 675 | |||
| 676 | /** |
||
| 677 | * @param string $sqls |
||
| 678 | * @param bool $all Whether to "UNION ALL" or not |
||
| 679 | * @return string |
||
| 680 | */ |
||
| 681 | function unionQueries( $sqls, $all ) { |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @return bool |
||
| 689 | */ |
||
| 690 | function wasDeadlock() { |
||
| 693 | |||
| 694 | /** |
||
| 695 | * @return bool |
||
| 696 | */ |
||
| 697 | function wasErrorReissuable() { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * @return bool |
||
| 703 | */ |
||
| 704 | function wasReadOnlyError() { |
||
| 707 | |||
| 708 | /** |
||
| 709 | * @return string Wikitext of a link to the server software's web site |
||
| 710 | */ |
||
| 711 | public function getSoftwareLink() { |
||
| 714 | |||
| 715 | /** |
||
| 716 | * @return string Version information from the database |
||
| 717 | */ |
||
| 718 | function getServerVersion() { |
||
| 723 | |||
| 724 | /** |
||
| 725 | * @return string User-friendly database information |
||
| 726 | */ |
||
| 727 | public function getServerInfo() { |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Get information about a given field |
||
| 735 | * Returns false if the field does not exist. |
||
| 736 | * |
||
| 737 | * @param string $table |
||
| 738 | * @param string $field |
||
| 739 | * @return SQLiteField|bool False on failure |
||
| 740 | */ |
||
| 741 | function fieldInfo( $table, $field ) { |
||
| 753 | |||
| 754 | protected function doBegin( $fname = '' ) { |
||
| 762 | |||
| 763 | /** |
||
| 764 | * @param string $s |
||
| 765 | * @return string |
||
| 766 | */ |
||
| 767 | function strencode( $s ) { |
||
| 770 | |||
| 771 | /** |
||
| 772 | * @param string $b |
||
| 773 | * @return Blob |
||
| 774 | */ |
||
| 775 | function encodeBlob( $b ) { |
||
| 778 | |||
| 779 | /** |
||
| 780 | * @param Blob|string $b |
||
| 781 | * @return string |
||
| 782 | */ |
||
| 783 | function decodeBlob( $b ) { |
||
| 790 | |||
| 791 | /** |
||
| 792 | * @param Blob|string $s |
||
| 793 | * @return string |
||
| 794 | */ |
||
| 795 | function addQuotes( $s ) { |
||
| 822 | |||
| 823 | /** |
||
| 824 | * @return string |
||
| 825 | */ |
||
| 826 | View Code Duplication | function buildLike() { |
|
| 834 | |||
| 835 | /** |
||
| 836 | * @param string $field Field or column to cast |
||
| 837 | * @return string |
||
| 838 | * @since 1.28 |
||
| 839 | */ |
||
| 840 | public function buildStringCast( $field ) { |
||
| 843 | |||
| 844 | /** |
||
| 845 | * @return string |
||
| 846 | */ |
||
| 847 | public function getSearchEngine() { |
||
| 850 | |||
| 851 | /** |
||
| 852 | * No-op version of deadlockLoop |
||
| 853 | * |
||
| 854 | * @return mixed |
||
| 855 | */ |
||
| 856 | public function deadlockLoop( /*...*/ ) { |
||
| 862 | |||
| 863 | /** |
||
| 864 | * @param string $s |
||
| 865 | * @return string |
||
| 866 | */ |
||
| 867 | protected function replaceVars( $s ) { |
||
| 919 | |||
| 920 | public function lock( $lockName, $method, $timeout = 5 ) { |
||
| 929 | |||
| 930 | public function unlock( $lockName, $method ) { |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Build a concatenation list to feed into a SQL query |
||
| 936 | * |
||
| 937 | * @param string[] $stringList |
||
| 938 | * @return string |
||
| 939 | */ |
||
| 940 | function buildConcat( $stringList ) { |
||
| 943 | |||
| 944 | View Code Duplication | public function buildGroupConcatField( |
|
| 951 | |||
| 952 | /** |
||
| 953 | * @throws MWException |
||
| 954 | * @param string $oldName |
||
| 955 | * @param string $newName |
||
| 956 | * @param bool $temporary |
||
| 957 | * @param string $fname |
||
| 958 | * @return bool|ResultWrapper |
||
| 959 | */ |
||
| 960 | function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__ ) { |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * List all tables on the database |
||
| 1016 | * |
||
| 1017 | * @param string $prefix Only show tables with this prefix, e.g. mw_ |
||
| 1018 | * @param string $fname Calling function name |
||
| 1019 | * |
||
| 1020 | * @return array |
||
| 1021 | */ |
||
| 1022 | function listTables( $prefix = null, $fname = __METHOD__ ) { |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * @return string |
||
| 1047 | */ |
||
| 1048 | public function __toString() { |
||
| 1051 | |||
| 1052 | } // end DatabaseSqlite class |
||
| 1053 | |||
| 1095 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: