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 Installer 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 Installer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class Installer |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * System object. |
||
| 47 | * |
||
| 48 | * @var System |
||
| 49 | */ |
||
| 50 | protected $system; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Array with user rights. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $mainRights = [ |
||
| 58 | [ |
||
| 59 | 'name' => 'add_user', |
||
| 60 | 'description' => 'Right to add user accounts', |
||
| 61 | ], |
||
| 62 | [ |
||
| 63 | 'name' => 'edit_user', |
||
| 64 | 'description' => 'Right to edit user accounts', |
||
| 65 | ], |
||
| 66 | [ |
||
| 67 | 'name' => 'delete_user', |
||
| 68 | 'description' => 'Right to delete user accounts', |
||
| 69 | ], |
||
| 70 | //4 => "add_faq", |
||
| 71 | [ |
||
| 72 | 'name' => 'add_faq', |
||
| 73 | 'description' => 'Right to add faq entries', |
||
| 74 | ], |
||
| 75 | //5 => "edit_faq", |
||
| 76 | [ |
||
| 77 | 'name' => 'edit_faq', |
||
| 78 | 'description' => 'Right to edit faq entries', |
||
| 79 | ], |
||
| 80 | //6 => "delete_faq", |
||
| 81 | [ |
||
| 82 | 'name' => 'delete_faq', |
||
| 83 | 'description' => 'Right to delete faq entries', |
||
| 84 | ], |
||
| 85 | //7 => "viewlog", |
||
| 86 | [ |
||
| 87 | 'name' => 'viewlog', |
||
| 88 | 'description' => 'Right to view logfiles', |
||
| 89 | ], |
||
| 90 | //8 => "adminlog", |
||
| 91 | [ |
||
| 92 | 'name' => 'adminlog', |
||
| 93 | 'description' => 'Right to view admin log', |
||
| 94 | ], |
||
| 95 | //9 => "delcomment", |
||
| 96 | [ |
||
| 97 | 'name' => 'delcomment', |
||
| 98 | 'description' => 'Right to delete comments', |
||
| 99 | ], |
||
| 100 | //10 => "addnews", |
||
| 101 | [ |
||
| 102 | 'name' => 'addnews', |
||
| 103 | 'description' => 'Right to add news', |
||
| 104 | ], |
||
| 105 | //11 => "editnews", |
||
| 106 | [ |
||
| 107 | 'name' => 'editnews', |
||
| 108 | 'description' => 'Right to edit news', |
||
| 109 | ], |
||
| 110 | //12 => "delnews", |
||
| 111 | [ |
||
| 112 | 'name' => 'delnews', |
||
| 113 | 'description' => 'Right to delete news', |
||
| 114 | ], |
||
| 115 | //13 => "addcateg", |
||
| 116 | [ |
||
| 117 | 'name' => 'addcateg', |
||
| 118 | 'description' => 'Right to add categories', |
||
| 119 | ], |
||
| 120 | //14 => "editcateg", |
||
| 121 | [ |
||
| 122 | 'name' => 'editcateg', |
||
| 123 | 'description' => 'Right to edit categories', |
||
| 124 | ], |
||
| 125 | //15 => "delcateg", |
||
| 126 | [ |
||
| 127 | 'name' => 'delcateg', |
||
| 128 | 'description' => 'Right to delete categories', |
||
| 129 | ], |
||
| 130 | //16 => "passwd", |
||
| 131 | [ |
||
| 132 | 'name' => 'passwd', |
||
| 133 | 'description' => 'Right to change passwords', |
||
| 134 | ], |
||
| 135 | //17 => "editconfig", |
||
| 136 | [ |
||
| 137 | 'name' => 'editconfig', |
||
| 138 | 'description' => 'Right to edit configuration', |
||
| 139 | ], |
||
| 140 | //18 => "viewadminlink", |
||
| 141 | [ |
||
| 142 | 'name' => 'viewadminlink', |
||
| 143 | 'description' => 'Right to see the link to the admin section' |
||
| 144 | ], |
||
| 145 | //19 => "backup delatt", // Duplicate, removed with 2.7.3 |
||
| 146 | //[ |
||
| 147 | // 'name' => 'delatt', |
||
| 148 | // 'description' => 'Right to delete attachments' |
||
| 149 | //], |
||
| 150 | //20 => "backup", |
||
| 151 | [ |
||
| 152 | 'name' => 'backup', |
||
| 153 | 'description' => 'Right to save backups', |
||
| 154 | ], |
||
| 155 | //21 => "restore", |
||
| 156 | [ |
||
| 157 | 'name' => 'restore', |
||
| 158 | 'description' => 'Right to load backups', |
||
| 159 | ], |
||
| 160 | //22 => "delquestion", |
||
| 161 | [ |
||
| 162 | 'name' => 'delquestion', |
||
| 163 | 'description' => 'Right to delete questions', |
||
| 164 | ], |
||
| 165 | //23 => 'addglossary', |
||
| 166 | [ |
||
| 167 | 'name' => 'addglossary', |
||
| 168 | 'description' => 'Right to add glossary entries', |
||
| 169 | ], |
||
| 170 | //24 => 'editglossary', |
||
| 171 | [ |
||
| 172 | 'name' => 'editglossary', |
||
| 173 | 'description' => 'Right to edit glossary entries', |
||
| 174 | ], |
||
| 175 | //25 => 'delglossary' |
||
| 176 | [ |
||
| 177 | 'name' => 'delglossary', |
||
| 178 | 'description' => 'Right to delete glossary entries', |
||
| 179 | ], |
||
| 180 | //26 => 'changebtrevs' |
||
| 181 | [ |
||
| 182 | 'name' => 'changebtrevs', |
||
| 183 | 'description' => 'Right to edit revisions', |
||
| 184 | ], |
||
| 185 | //27 => "addgroup", |
||
| 186 | [ |
||
| 187 | 'name' => 'addgroup', |
||
| 188 | 'description' => 'Right to add group accounts', |
||
| 189 | ], |
||
| 190 | //28 => "editgroup", |
||
| 191 | [ |
||
| 192 | 'name' => 'editgroup', |
||
| 193 | 'description' => 'Right to edit group accounts', |
||
| 194 | ], |
||
| 195 | //29 => "delgroup", |
||
| 196 | [ |
||
| 197 | 'name' => 'delgroup', |
||
| 198 | 'description' => 'Right to delete group accounts', |
||
| 199 | ], |
||
| 200 | //30 => "addtranslation", |
||
| 201 | [ |
||
| 202 | 'name' => 'addtranslation', |
||
| 203 | 'description' => 'Right to add translation', |
||
| 204 | ], |
||
| 205 | //31 => "edittranslation", |
||
| 206 | [ |
||
| 207 | 'name' => 'edittranslation', |
||
| 208 | 'description' => 'Right to edit translations', |
||
| 209 | ], |
||
| 210 | //32 => "deltranslation", |
||
| 211 | [ |
||
| 212 | 'name' => 'deltranslation', |
||
| 213 | 'description' => 'Right to delete translations', |
||
| 214 | ], |
||
| 215 | // 33 => 'approverec' |
||
| 216 | [ |
||
| 217 | 'name' => 'approverec', |
||
| 218 | 'description' => 'Right to approve records', |
||
| 219 | ], |
||
| 220 | // 34 => 'addattachment' |
||
| 221 | [ |
||
| 222 | 'name' => 'addattachment', |
||
| 223 | 'description' => 'Right to add attachments', |
||
| 224 | ], |
||
| 225 | // 35 => 'editattachment' |
||
| 226 | [ |
||
| 227 | 'name' => 'editattachment', |
||
| 228 | 'description' => 'Right to edit attachments', |
||
| 229 | ], |
||
| 230 | // 36 => 'delattachment' |
||
| 231 | [ |
||
| 232 | 'name' => 'delattachment', |
||
| 233 | 'description' => 'Right to delete attachments', |
||
| 234 | ], |
||
| 235 | // 37 => 'dlattachment' |
||
| 236 | [ |
||
| 237 | 'name' => 'dlattachment', |
||
| 238 | 'description' => 'Right to download attachments', |
||
| 239 | ], |
||
| 240 | // 38 => 'reports' |
||
| 241 | [ |
||
| 242 | 'name' => 'reports', |
||
| 243 | 'description' => 'Right to generate reports', |
||
| 244 | ], |
||
| 245 | // 39 => 'addfaq' |
||
| 246 | [ |
||
| 247 | 'name' => 'addfaq', |
||
| 248 | 'description' => 'Right to add FAQs in frontend', |
||
| 249 | ], |
||
| 250 | // 40 => 'addquestion' |
||
| 251 | [ |
||
| 252 | 'name' => 'addquestion', |
||
| 253 | 'description' => 'Right to add questions in frontend', |
||
| 254 | ], |
||
| 255 | // 41 => 'addcomment' |
||
| 256 | [ |
||
| 257 | 'name' => 'addcomment', |
||
| 258 | 'description' => 'Right to add comments in frontend', |
||
| 259 | ], |
||
| 260 | // 42 => 'editinstances' |
||
| 261 | [ |
||
| 262 | 'name' => 'editinstances', |
||
| 263 | 'description' => 'Right to edit multi-site instances', |
||
| 264 | ], |
||
| 265 | // 43 => 'addinstances' |
||
| 266 | [ |
||
| 267 | 'name' => 'addinstances', |
||
| 268 | 'description' => 'Right to add multi-site instances', |
||
| 269 | ], |
||
| 270 | // 44 => 'delinstances' |
||
| 271 | [ |
||
| 272 | 'name' => 'delinstances', |
||
| 273 | 'description' => 'Right to delete multi-site instances', |
||
| 274 | ], |
||
| 275 | [ |
||
| 276 | 'name' => 'export', |
||
| 277 | 'description' => 'Right to export the complete FAQ', |
||
| 278 | ], |
||
| 279 | [ |
||
| 280 | 'name' => 'view_faqs', |
||
| 281 | 'description' => 'Right to view FAQs' |
||
| 282 | ], |
||
| 283 | [ |
||
| 284 | 'name' => 'view_categories', |
||
| 285 | 'description' => 'Right to view categories' |
||
| 286 | |||
| 287 | ], |
||
| 288 | [ |
||
| 289 | 'name' => 'view_sections', |
||
| 290 | 'description' => 'Right to view sections' |
||
| 291 | |||
| 292 | ], |
||
| 293 | [ |
||
| 294 | 'name' => 'view_news', |
||
| 295 | 'description' => 'Right to view news' |
||
| 296 | |||
| 297 | ], |
||
| 298 | [ |
||
| 299 | 'name' => 'add_section', |
||
| 300 | 'description' => 'Right to add sections' |
||
| 301 | |||
| 302 | ], |
||
| 303 | [ |
||
| 304 | 'name' => 'edit_section', |
||
| 305 | 'description' => 'Right to edit sections' |
||
| 306 | |||
| 307 | ], |
||
| 308 | [ |
||
| 309 | 'name' => 'delete_section', |
||
| 310 | 'description' => 'Right to delete sections' |
||
| 311 | |||
| 312 | ], |
||
| 313 | [ |
||
| 314 | 'name' => 'administrate_sections', |
||
| 315 | 'description' => 'Right to administrate sections' |
||
| 316 | |||
| 317 | ], |
||
| 318 | [ |
||
| 319 | 'name' => 'administrate_groups', |
||
| 320 | 'description' => 'Right to administrate groups' |
||
| 321 | |||
| 322 | ], |
||
| 323 | ]; |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Configuration array. |
||
| 327 | * |
||
| 328 | * @var array |
||
| 329 | */ |
||
| 330 | protected $_mainConfig = [ |
||
| 331 | 'main.currentVersion' => null, |
||
| 332 | 'main.currentApiVersion' => null, |
||
| 333 | 'main.language' => '__PHPMYFAQ_LANGUAGE__', |
||
| 334 | 'main.languageDetection' => 'true', |
||
| 335 | 'main.phpMyFAQToken' => null, |
||
| 336 | 'main.referenceURL' => '__PHPMYFAQ_REFERENCE_URL__', |
||
| 337 | 'main.administrationMail' => '[email protected]', |
||
| 338 | 'main.contactInformations' => '', |
||
| 339 | 'main.enableAdminLog' => 'true', |
||
| 340 | 'main.enableRewriteRules' => 'false', |
||
| 341 | 'main.enableUserTracking' => 'true', |
||
| 342 | 'main.metaDescription' => 'phpMyFAQ should be the answer for all questions in life', |
||
| 343 | 'main.metaKeywords' => '', |
||
| 344 | 'main.metaPublisher' => '__PHPMYFAQ_PUBLISHER__', |
||
| 345 | 'main.send2friendText' => '', |
||
| 346 | 'main.titleFAQ' => 'phpMyFAQ Codename Phobos', |
||
| 347 | 'main.urlValidateInterval' => '86400', |
||
| 348 | 'main.enableWysiwygEditor' => 'true', |
||
| 349 | 'main.enableWysiwygEditorFrontend' => 'false', |
||
| 350 | 'main.enableMarkdownEditor' => 'false', |
||
| 351 | 'main.templateSet' => 'default', |
||
| 352 | 'main.optionalMailAddress' => 'false', |
||
| 353 | 'main.dateFormat' => 'Y-m-d H:i', |
||
| 354 | 'main.maintenanceMode' => 'false', |
||
| 355 | 'main.enableGravatarSupport' => 'false', |
||
| 356 | 'main.enableRssFeeds' => 'true', |
||
| 357 | 'main.enableGzipCompression' => 'true', |
||
| 358 | 'main.enableLinkVerification' => 'true', |
||
| 359 | 'main.customPdfHeader' => '', |
||
| 360 | 'main.customPdfHFooter' => '', |
||
| 361 | 'main.enableSmartAnswering' => 'true', |
||
| 362 | 'main.enableCategoryRestrictions' => 'true', |
||
| 363 | 'main.enableSendToFriend' => 'true', |
||
| 364 | 'main.privacyURL' => '', |
||
| 365 | 'main.enableAutoUpdateHint' => 'true', |
||
| 366 | |||
| 367 | 'records.numberOfRecordsPerPage' => '10', |
||
| 368 | 'records.numberOfShownNewsEntries' => '3', |
||
| 369 | 'records.defaultActivation' => 'false', |
||
| 370 | 'records.defaultAllowComments' => 'false', |
||
| 371 | 'records.enableVisibilityQuestions' => 'false', |
||
| 372 | 'records.numberOfRelatedArticles' => '5', |
||
| 373 | 'records.orderby' => 'id', |
||
| 374 | 'records.sortby' => 'DESC', |
||
| 375 | 'records.orderingPopularFaqs' => 'visits', |
||
| 376 | 'records.disableAttachments' => 'true', |
||
| 377 | 'records.maxAttachmentSize' => '100000', |
||
| 378 | 'records.attachmentsPath' => 'attachments', |
||
| 379 | 'records.attachmentsStorageType' => '0', |
||
| 380 | 'records.enableAttachmentEncryption' => 'false', |
||
| 381 | 'records.defaultAttachmentEncKey' => '', |
||
| 382 | 'records.enableCloseQuestion' => 'false', |
||
| 383 | 'records.enableDeleteQuestion' => 'false', |
||
| 384 | 'records.autosaveActive' => 'false', |
||
| 385 | 'records.autosaveSecs' => '180', |
||
| 386 | 'records.randomSort' => 'false', |
||
| 387 | 'records.allowCommentsForGuests' => 'true', |
||
| 388 | 'records.allowQuestionsForGuests' => 'true', |
||
| 389 | 'records.allowNewFaqsForGuests' => 'true', |
||
| 390 | 'records.hideEmptyCategories' => 'false', |
||
| 391 | 'records.allowDownloadsForGuests' => 'false', |
||
| 392 | 'records.numberMaxStoredRevisions' => '10', |
||
| 393 | 'records.enableAutoRevisions' => 'false', |
||
| 394 | |||
| 395 | 'search.numberSearchTerms' => '10', |
||
| 396 | 'search.relevance' => 'thema,content,keywords', |
||
| 397 | 'search.enableRelevance' => 'false', |
||
| 398 | 'search.enableHighlighting' => 'true', |
||
| 399 | 'search.searchForSolutionId' => 'true', |
||
| 400 | 'search.enableElasticsearch' => 'false', |
||
| 401 | |||
| 402 | 'security.permLevel' => 'basic', |
||
| 403 | 'security.ipCheck' => 'false', |
||
| 404 | 'security.enableLoginOnly' => 'false', |
||
| 405 | 'security.bannedIPs' => '', |
||
| 406 | 'security.ssoSupport' => 'false', |
||
| 407 | 'security.ssoLogoutRedirect' => '', |
||
| 408 | 'security.useSslForLogins' => 'false', |
||
| 409 | 'security.useSslOnly' => 'false', |
||
| 410 | 'security.forcePasswordUpdate' => 'false', |
||
| 411 | 'security.enableRegistration' => 'true', |
||
| 412 | |||
| 413 | 'spam.checkBannedWords' => 'true', |
||
| 414 | 'spam.enableCaptchaCode' => null, |
||
| 415 | 'spam.enableSafeEmail' => 'true', |
||
| 416 | 'spam.manualActivation' => 'true', |
||
| 417 | |||
| 418 | 'socialnetworks.enableTwitterSupport' => 'false', |
||
| 419 | 'socialnetworks.twitterConsumerKey' => '', |
||
| 420 | 'socialnetworks.twitterConsumerSecret' => '', |
||
| 421 | 'socialnetworks.twitterAccessTokenKey' => '', |
||
| 422 | 'socialnetworks.twitterAccessTokenSecret' => '', |
||
| 423 | 'socialnetworks.enableFacebookSupport' => 'false', |
||
| 424 | 'socialnetworks.disableAll' => 'false', |
||
| 425 | |||
| 426 | 'seo.metaTagsHome' => 'index, follow', |
||
| 427 | 'seo.metaTagsFaqs' => 'index, follow', |
||
| 428 | 'seo.metaTagsCategories' => 'index, follow', |
||
| 429 | 'seo.metaTagsPages' => 'index, follow', |
||
| 430 | 'seo.metaTagsAdmin' => 'noindex, nofollow', |
||
| 431 | 'seo.enableXMLSitemap' => 'true', |
||
| 432 | |||
| 433 | 'mail.remoteSMTP' => 'false', |
||
| 434 | 'mail.remoteSMTPServer' => '', |
||
| 435 | 'mail.remoteSMTPUsername' => '', |
||
| 436 | 'mail.remoteSMTPPassword' => '', |
||
| 437 | |||
| 438 | 'ldap.ldapSupport' => 'false', |
||
| 439 | 'ldap.ldap_mapping.name' => 'cn', |
||
| 440 | 'ldap.ldap_mapping.username' => 'samAccountName', |
||
| 441 | 'ldap.ldap_mapping.mail' => 'mail', |
||
| 442 | 'ldap.ldap_mapping.memberOf' => '', |
||
| 443 | 'ldap.ldap_use_domain_prefix' => 'true', |
||
| 444 | 'ldap.ldap_options.LDAP_OPT_PROTOCOL_VERSION' => '3', |
||
| 445 | 'ldap.ldap_options.LDAP_OPT_REFERRALS' => '0', |
||
| 446 | 'ldap.ldap_use_memberOf' => 'false', |
||
| 447 | 'ldap.ldap_use_sasl' => 'false', |
||
| 448 | 'ldap.ldap_use_multiple_servers' => 'false', |
||
| 449 | 'ldap.ldap_use_anonymous_login' => 'false', |
||
| 450 | 'ldap.ldap_use_dynamic_login' => 'false', |
||
| 451 | 'ldap.ldap_dynamic_login_attribute' => 'uid' |
||
| 452 | ]; |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Constructor. |
||
| 456 | * |
||
| 457 | */ |
||
| 458 | public function __construct() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Check absolutely necessary stuff and die. |
||
| 472 | */ |
||
| 473 | public function checkBasicStuff() |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Checks for the minimum PHP requirement and if the database credentials file is readable. |
||
| 520 | * |
||
| 521 | * @param string $databaseType |
||
| 522 | * |
||
| 523 | * @return void |
||
| 524 | */ |
||
| 525 | public function checkPreUpgrade(string $databaseType) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Checks the minimum required PHP version, defined in System. |
||
| 560 | * |
||
| 561 | * @return bool |
||
| 562 | */ |
||
| 563 | public function checkMinimumPhpVersion() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Checks if the file permissions are okay. |
||
| 574 | */ |
||
| 575 | public function checkFilesystemPermissions() |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Checks some non critical settings and print some hints. |
||
| 604 | * |
||
| 605 | * @todo We should return an array of messages |
||
| 606 | */ |
||
| 607 | public function checkNoncriticalSettings() |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Checks if we can store data via sessions. If not, e.g. an user can't |
||
| 636 | * login into the admin section. |
||
| 637 | * |
||
| 638 | * @return bool |
||
| 639 | */ |
||
| 640 | public function checkSessionSettings() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Checks if phpMyFAQ database tables are available |
||
| 647 | * @param Driver $database |
||
| 648 | * @throws |
||
| 649 | */ |
||
| 650 | public function checkAvailableDatabaseTables(Driver $database) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Starts the installation. |
||
| 666 | * |
||
| 667 | * @param array $setup |
||
| 668 | * @throws |
||
| 669 | */ |
||
| 670 | public function startInstall(array $setup = null) |
||
| 1046 | |||
| 1047 | /** |
||
| 1048 | * Cleanup all files after an installation. |
||
| 1049 | * |
||
| 1050 | * @return void |
||
| 1051 | */ |
||
| 1052 | public function cleanUpFiles() |
||
| 1064 | } |
||
| 1065 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: