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 DatabaseBase { | 
            ||
| 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 ) { | 
            ||
| 105 | |||
| 106 | /**  | 
            ||
| 107 | * @param string $filename  | 
            ||
| 108 | * @param array $p Options map; supports:  | 
            ||
| 109 | * - flags : (same as __construct counterpart)  | 
            ||
| 110 | * - trxMode : (same as __construct counterpart)  | 
            ||
| 111 | * - dbDirectory : (same as __construct counterpart)  | 
            ||
| 112 | * @return DatabaseSqlite  | 
            ||
| 113 | * @since 1.25  | 
            ||
| 114 | */  | 
            ||
| 115 | 	public static function newStandaloneInstance( $filename, array $p = [] ) { | 
            ||
| 122 | |||
| 123 | /**  | 
            ||
| 124 | * @return string  | 
            ||
| 125 | */  | 
            ||
| 126 | 	function getType() { | 
            ||
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * @todo Check if it should be true like parent class  | 
            ||
| 132 | *  | 
            ||
| 133 | * @return bool  | 
            ||
| 134 | */  | 
            ||
| 135 | 	function implicitGroupby() { | 
            ||
| 138 | |||
| 139 | /** Open an SQLite database and return a resource handle to it  | 
            ||
| 140 | * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases  | 
            ||
| 141 | *  | 
            ||
| 142 | * @param string $server  | 
            ||
| 143 | * @param string $user  | 
            ||
| 144 | * @param string $pass  | 
            ||
| 145 | * @param string $dbName  | 
            ||
| 146 | *  | 
            ||
| 147 | * @throws DBConnectionError  | 
            ||
| 148 | * @return PDO  | 
            ||
| 149 | */  | 
            ||
| 150 | 	function open( $server, $user, $pass, $dbName ) { | 
            ||
| 161 | |||
| 162 | /**  | 
            ||
| 163 | * Opens a database file  | 
            ||
| 164 | *  | 
            ||
| 165 | * @param string $fileName  | 
            ||
| 166 | * @throws DBConnectionError  | 
            ||
| 167 | * @return PDO|bool SQL connection or false if failed  | 
            ||
| 168 | */  | 
            ||
| 169 | 	protected function openFile( $fileName ) { | 
            ||
| 201 | |||
| 202 | /**  | 
            ||
| 203 | * @return string SQLite DB file path  | 
            ||
| 204 | * @since 1.25  | 
            ||
| 205 | */  | 
            ||
| 206 | 	public function getDbFilePath() { | 
            ||
| 209 | |||
| 210 | /**  | 
            ||
| 211 | * Does not actually close the connection, just destroys the reference for GC to do its work  | 
            ||
| 212 | * @return bool  | 
            ||
| 213 | */  | 
            ||
| 214 | 	protected function closeConnection() { | 
            ||
| 219 | |||
| 220 | /**  | 
            ||
| 221 | * Generates a database file name. Explicitly public for installer.  | 
            ||
| 222 | * @param string $dir Directory where database resides  | 
            ||
| 223 | * @param string $dbName Database name  | 
            ||
| 224 | * @return string  | 
            ||
| 225 | */  | 
            ||
| 226 | 	public static function generateFileName( $dir, $dbName ) { | 
            ||
| 229 | |||
| 230 | /**  | 
            ||
| 231 | * Check if the searchindext table is FTS enabled.  | 
            ||
| 232 | * @return bool False if not enabled.  | 
            ||
| 233 | */  | 
            ||
| 234 | 	function checkForEnabledSearch() { | 
            ||
| 247 | |||
| 248 | /**  | 
            ||
| 249 | * Returns version of currently supported SQLite fulltext search module or false if none present.  | 
            ||
| 250 | * @return string  | 
            ||
| 251 | */  | 
            ||
| 252 | 	static function getFulltextSearchModule() { | 
            ||
| 268 | |||
| 269 | /**  | 
            ||
| 270 | * Attaches external database to our connection, see http://sqlite.org/lang_attach.html  | 
            ||
| 271 | * for details.  | 
            ||
| 272 | *  | 
            ||
| 273 | * @param string $name Database name to be used in queries like  | 
            ||
| 274 | * SELECT foo FROM dbname.table  | 
            ||
| 275 | * @param bool|string $file Database file name. If omitted, will be generated  | 
            ||
| 276 | * using $name and configured data directory  | 
            ||
| 277 | * @param string $fname Calling function name  | 
            ||
| 278 | * @return ResultWrapper  | 
            ||
| 279 | */  | 
            ||
| 280 | 	function attachDatabase( $name, $file = false, $fname = __METHOD__ ) { | 
            ||
| 288 | |||
| 289 | 	function isWriteQuery( $sql ) { | 
            ||
| 292 | |||
| 293 | /**  | 
            ||
| 294 | * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result  | 
            ||
| 295 | *  | 
            ||
| 296 | * @param string $sql  | 
            ||
| 297 | * @return bool|ResultWrapper  | 
            ||
| 298 | */  | 
            ||
| 299 | 	protected function doQuery( $sql ) { | 
            ||
| 311 | |||
| 312 | /**  | 
            ||
| 313 | * @param ResultWrapper|mixed $res  | 
            ||
| 314 | */  | 
            ||
| 315 | 	function freeResult( $res ) { | 
            ||
| 322 | |||
| 323 | /**  | 
            ||
| 324 | * @param ResultWrapper|array $res  | 
            ||
| 325 | * @return stdClass|bool  | 
            ||
| 326 | */  | 
            ||
| 327 | 	function fetchObject( $res ) { | 
            ||
| 349 | |||
| 350 | /**  | 
            ||
| 351 | * @param ResultWrapper|mixed $res  | 
            ||
| 352 | * @return array|bool  | 
            ||
| 353 | */  | 
            ||
| 354 | 	function fetchRow( $res ) { | 
            ||
| 369 | |||
| 370 | /**  | 
            ||
| 371 | * The PDO::Statement class implements the array interface so count() will work  | 
            ||
| 372 | *  | 
            ||
| 373 | * @param ResultWrapper|array $res  | 
            ||
| 374 | * @return int  | 
            ||
| 375 | */  | 
            ||
| 376 | 	function numRows( $res ) { | 
            ||
| 381 | |||
| 382 | /**  | 
            ||
| 383 | * @param ResultWrapper $res  | 
            ||
| 384 | * @return int  | 
            ||
| 385 | */  | 
            ||
| 386 | 	function numFields( $res ) { | 
            ||
| 396 | |||
| 397 | /**  | 
            ||
| 398 | * @param ResultWrapper $res  | 
            ||
| 399 | * @param int $n  | 
            ||
| 400 | * @return bool  | 
            ||
| 401 | */  | 
            ||
| 402 | 	function fieldName( $res, $n ) { | 
            ||
| 412 | |||
| 413 | /**  | 
            ||
| 414 | * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks  | 
            ||
| 415 | *  | 
            ||
| 416 | * @param string $name  | 
            ||
| 417 | * @param string $format  | 
            ||
| 418 | * @return string  | 
            ||
| 419 | */  | 
            ||
| 420 | 	function tableName( $name, $format = 'quoted' ) { | 
            ||
| 428 | |||
| 429 | /**  | 
            ||
| 430 | * Index names have DB scope  | 
            ||
| 431 | *  | 
            ||
| 432 | * @param string $index  | 
            ||
| 433 | * @return string  | 
            ||
| 434 | */  | 
            ||
| 435 | 	protected function indexName( $index ) { | 
            ||
| 438 | |||
| 439 | /**  | 
            ||
| 440 | * This must be called after nextSequenceVal  | 
            ||
| 441 | *  | 
            ||
| 442 | * @return int  | 
            ||
| 443 | */  | 
            ||
| 444 | 	function insertId() { | 
            ||
| 448 | |||
| 449 | /**  | 
            ||
| 450 | * @param ResultWrapper|array $res  | 
            ||
| 451 | * @param int $row  | 
            ||
| 452 | */  | 
            ||
| 453 | 	function dataSeek( $res, $row ) { | 
            ||
| 466 | |||
| 467 | /**  | 
            ||
| 468 | * @return string  | 
            ||
| 469 | */  | 
            ||
| 470 | 	function lastError() { | 
            ||
| 478 | |||
| 479 | /**  | 
            ||
| 480 | * @return string  | 
            ||
| 481 | */  | 
            ||
| 482 | 	function lastErrno() { | 
            ||
| 491 | |||
| 492 | /**  | 
            ||
| 493 | * @return int  | 
            ||
| 494 | */  | 
            ||
| 495 | 	function affectedRows() { | 
            ||
| 498 | |||
| 499 | /**  | 
            ||
| 500 | * Returns information about an index  | 
            ||
| 501 | * Returns false if the index does not exist  | 
            ||
| 502 | * - if errors are explicitly ignored, returns NULL on failure  | 
            ||
| 503 | *  | 
            ||
| 504 | * @param string $table  | 
            ||
| 505 | * @param string $index  | 
            ||
| 506 | * @param string $fname  | 
            ||
| 507 | * @return array  | 
            ||
| 508 | */  | 
            ||
| 509 | 	function indexInfo( $table, $index, $fname = __METHOD__ ) { | 
            ||
| 525 | |||
| 526 | /**  | 
            ||
| 527 | * @param string $table  | 
            ||
| 528 | * @param string $index  | 
            ||
| 529 | * @param string $fname  | 
            ||
| 530 | * @return bool|null  | 
            ||
| 531 | */  | 
            ||
| 532 | 	function indexUnique( $table, $index, $fname = __METHOD__ ) { | 
            ||
| 552 | |||
| 553 | /**  | 
            ||
| 554 | * Filter the options used in SELECT statements  | 
            ||
| 555 | *  | 
            ||
| 556 | * @param array $options  | 
            ||
| 557 | * @return array  | 
            ||
| 558 | */  | 
            ||
| 559 | 	function makeSelectOptions( $options ) { | 
            ||
| 568 | |||
| 569 | /**  | 
            ||
| 570 | * @param array $options  | 
            ||
| 571 | * @return string  | 
            ||
| 572 | */  | 
            ||
| 573 | 	protected function makeUpdateOptionsArray( $options ) { | 
            ||
| 579 | |||
| 580 | /**  | 
            ||
| 581 | * @param array $options  | 
            ||
| 582 | * @return array  | 
            ||
| 583 | */  | 
            ||
| 584 | 	static function fixIgnore( $options ) { | 
            ||
| 594 | |||
| 595 | /**  | 
            ||
| 596 | * @param array $options  | 
            ||
| 597 | * @return string  | 
            ||
| 598 | */  | 
            ||
| 599 | 	function makeInsertOptions( $options ) { | 
            ||
| 604 | |||
| 605 | /**  | 
            ||
| 606 | * Based on generic method (parent) with some prior SQLite-sepcific adjustments  | 
            ||
| 607 | * @param string $table  | 
            ||
| 608 | * @param array $a  | 
            ||
| 609 | * @param string $fname  | 
            ||
| 610 | * @param array $options  | 
            ||
| 611 | * @return bool  | 
            ||
| 612 | */  | 
            ||
| 613 | 	function insert( $table, $a, $fname = __METHOD__, $options = [] ) { | 
            ||
| 632 | |||
| 633 | /**  | 
            ||
| 634 | * @param string $table  | 
            ||
| 635 | * @param array $uniqueIndexes Unused  | 
            ||
| 636 | * @param string|array $rows  | 
            ||
| 637 | * @param string $fname  | 
            ||
| 638 | * @return bool|ResultWrapper  | 
            ||
| 639 | */  | 
            ||
| 640 | 	function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) { | 
            ||
| 659 | |||
| 660 | /**  | 
            ||
| 661 | * Returns the size of a text field, or -1 for "unlimited"  | 
            ||
| 662 | * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though.  | 
            ||
| 663 | *  | 
            ||
| 664 | * @param string $table  | 
            ||
| 665 | * @param string $field  | 
            ||
| 666 | * @return int  | 
            ||
| 667 | */  | 
            ||
| 668 | 	function textFieldSize( $table, $field ) { | 
            ||
| 671 | |||
| 672 | /**  | 
            ||
| 673 | * @return bool  | 
            ||
| 674 | */  | 
            ||
| 675 | 	function unionSupportsOrderAndLimit() { | 
            ||
| 678 | |||
| 679 | /**  | 
            ||
| 680 | * @param string $sqls  | 
            ||
| 681 | * @param bool $all Whether to "UNION ALL" or not  | 
            ||
| 682 | * @return string  | 
            ||
| 683 | */  | 
            ||
| 684 | 	function unionQueries( $sqls, $all ) { | 
            ||
| 689 | |||
| 690 | /**  | 
            ||
| 691 | * @return bool  | 
            ||
| 692 | */  | 
            ||
| 693 | 	function wasDeadlock() { | 
            ||
| 696 | |||
| 697 | /**  | 
            ||
| 698 | * @return bool  | 
            ||
| 699 | */  | 
            ||
| 700 | 	function wasErrorReissuable() { | 
            ||
| 703 | |||
| 704 | /**  | 
            ||
| 705 | * @return bool  | 
            ||
| 706 | */  | 
            ||
| 707 | 	function wasReadOnlyError() { | 
            ||
| 710 | |||
| 711 | /**  | 
            ||
| 712 | * @return string Wikitext of a link to the server software's web site  | 
            ||
| 713 | */  | 
            ||
| 714 | 	public function getSoftwareLink() { | 
            ||
| 717 | |||
| 718 | /**  | 
            ||
| 719 | * @return string Version information from the database  | 
            ||
| 720 | */  | 
            ||
| 721 | 	function getServerVersion() { | 
            ||
| 726 | |||
| 727 | /**  | 
            ||
| 728 | * Get information about a given field  | 
            ||
| 729 | * Returns false if the field does not exist.  | 
            ||
| 730 | *  | 
            ||
| 731 | * @param string $table  | 
            ||
| 732 | * @param string $field  | 
            ||
| 733 | * @return SQLiteField|bool False on failure  | 
            ||
| 734 | */  | 
            ||
| 735 | 	function fieldInfo( $table, $field ) { | 
            ||
| 747 | |||
| 748 | 	protected function doBegin( $fname = '' ) { | 
            ||
| 756 | |||
| 757 | /**  | 
            ||
| 758 | * @param string $s  | 
            ||
| 759 | * @return string  | 
            ||
| 760 | */  | 
            ||
| 761 | 	function strencode( $s ) { | 
            ||
| 764 | |||
| 765 | /**  | 
            ||
| 766 | * @param string $b  | 
            ||
| 767 | * @return Blob  | 
            ||
| 768 | */  | 
            ||
| 769 | 	function encodeBlob( $b ) { | 
            ||
| 772 | |||
| 773 | /**  | 
            ||
| 774 | * @param Blob|string $b  | 
            ||
| 775 | * @return string  | 
            ||
| 776 | */  | 
            ||
| 777 | 	function decodeBlob( $b ) { | 
            ||
| 784 | |||
| 785 | /**  | 
            ||
| 786 | * @param Blob|string $s  | 
            ||
| 787 | * @return string  | 
            ||
| 788 | */  | 
            ||
| 789 | 	function addQuotes( $s ) { | 
            ||
| 815 | |||
| 816 | /**  | 
            ||
| 817 | * @return string  | 
            ||
| 818 | */  | 
            ||
| 819 | View Code Duplication | 	function buildLike() { | 
            |
| 827 | |||
| 828 | /**  | 
            ||
| 829 | * @param string $field Field or column to cast  | 
            ||
| 830 | * @return string  | 
            ||
| 831 | * @since 1.28  | 
            ||
| 832 | */  | 
            ||
| 833 | 	public function buildStringCast( $field ) { | 
            ||
| 836 | |||
| 837 | /**  | 
            ||
| 838 | * No-op version of deadlockLoop  | 
            ||
| 839 | *  | 
            ||
| 840 | * @return mixed  | 
            ||
| 841 | */  | 
            ||
| 842 | 	public function deadlockLoop( /*...*/ ) { | 
            ||
| 848 | |||
| 849 | /**  | 
            ||
| 850 | * @param string $s  | 
            ||
| 851 | * @return string  | 
            ||
| 852 | */  | 
            ||
| 853 | 	protected function replaceVars( $s ) { | 
            ||
| 905 | |||
| 906 | 	public function lock( $lockName, $method, $timeout = 5 ) { | 
            ||
| 915 | |||
| 916 | 	public function unlock( $lockName, $method ) { | 
            ||
| 919 | |||
| 920 | /**  | 
            ||
| 921 | * Build a concatenation list to feed into a SQL query  | 
            ||
| 922 | *  | 
            ||
| 923 | * @param string[] $stringList  | 
            ||
| 924 | * @return string  | 
            ||
| 925 | */  | 
            ||
| 926 | 	function buildConcat( $stringList ) { | 
            ||
| 929 | |||
| 930 | View Code Duplication | public function buildGroupConcatField(  | 
            |
| 937 | |||
| 938 | /**  | 
            ||
| 939 | * @param string $oldName  | 
            ||
| 940 | * @param string $newName  | 
            ||
| 941 | * @param bool $temporary  | 
            ||
| 942 | * @param string $fname  | 
            ||
| 943 | * @return bool|ResultWrapper  | 
            ||
| 944 | * @throws RuntimeException  | 
            ||
| 945 | */  | 
            ||
| 946 | 	function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__ ) { | 
            ||
| 1000 | |||
| 1001 | /**  | 
            ||
| 1002 | * List all tables on the database  | 
            ||
| 1003 | *  | 
            ||
| 1004 | * @param string $prefix Only show tables with this prefix, e.g. mw_  | 
            ||
| 1005 | * @param string $fname Calling function name  | 
            ||
| 1006 | *  | 
            ||
| 1007 | * @return array  | 
            ||
| 1008 | */  | 
            ||
| 1009 | 	function listTables( $prefix = null, $fname = __METHOD__ ) { | 
            ||
| 1031 | |||
| 1032 | /**  | 
            ||
| 1033 | * Override due to no CASCADE support  | 
            ||
| 1034 | *  | 
            ||
| 1035 | * @param string $tableName  | 
            ||
| 1036 | * @param string $fName  | 
            ||
| 1037 | * @return bool|ResultWrapper  | 
            ||
| 1038 | * @throws DBReadOnlyError  | 
            ||
| 1039 | */  | 
            ||
| 1040 | View Code Duplication | 	public function dropTable( $tableName, $fName = __METHOD__ ) { | 
            |
| 1048 | |||
| 1049 | 	protected function requiresDatabaseUser() { | 
            ||
| 1052 | |||
| 1053 | /**  | 
            ||
| 1054 | * @return string  | 
            ||
| 1055 | */  | 
            ||
| 1056 | 	public function __toString() { | 
            ||
| 1059 | }  | 
            ||
| 1060 | 
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: