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:
| 1 | <?php |
||
| 10 | class WebPage |
||
| 11 | { |
||
| 12 | public $body = ''; |
||
| 13 | protected $templateName = 'main.html'; |
||
| 14 | |||
| 15 | public function __construct($title) |
||
| 16 | { |
||
| 17 | $this->settings = \Settings::getInstance(); |
||
|
|
|||
| 18 | $this->loader = new \Twig_Loader_Filesystem(dirname(__FILE__).'/../templates'); |
||
| 19 | |||
| 20 | $twigCache = $this->settings->getGlobalSetting('twig_cache', '/var/php_cache/twig'); |
||
| 21 | $twigSettings = array('cache' => $twigCache); |
||
| 22 | |||
| 23 | $this->wwwUrl = $this->settings->getGlobalSetting('www_url', 'https://www.burningflipside.com'); |
||
| 24 | $this->wikiUrl = $this->settings->getGlobalSetting('wiki_url', 'https://wiki.burningflipside.com'); |
||
| 25 | $this->secureUrl = $this->settings->getGlobalSetting('secure_url', 'https://secure.burningflipside.com'); |
||
| 26 | $this->profilesUrl = $this->settings->getGlobalSetting('profiles_url', 'https://profiles.burningflipside.com'); |
||
| 27 | $this->registerUrl = $this->settings->getGlobalSetting('register_url', $this->profilesUrl.'/register.php'); |
||
| 28 | $this->resetUrl = $this->settings->getGlobalSetting('reset_url', $this->profilesUrl.'/reset.php'); |
||
| 29 | $this->loginUrl = $this->settings->getGlobalSetting('login_url', $this->profilesUrl.'/login.php'); |
||
| 30 | $this->logoutUrl = $this->settings->getGlobalSetting('logout_url', $this->profilesUrl.'/logout.php'); |
||
| 31 | |||
| 32 | $this->twig = new \Twig_Environment($this->loader, $twigSettings); |
||
| 33 | $this->content = array('pageTitle' => $title); |
||
| 34 | $this->user = \FlipSession::getUser(); |
||
| 35 | $this->content['header'] = array(); |
||
| 36 | $this->content['header']['sites'] = array(); |
||
| 37 | $this->content['header']['sites']['Profiles'] = $this->profilesUrl; |
||
| 38 | $this->content['header']['sites']['WWW'] = $this->wwwUrl; |
||
| 39 | $this->content['header']['sites']['Pyropedia'] = $this->wikiUrl; |
||
| 40 | $this->content['header']['sites']['Secure'] = $this->secureUrl; |
||
| 41 | |||
| 42 | $this->aboutUrl = $this->settings->getGlobalSetting('about_url', $this->wwwUrl.'/about'); |
||
| 43 | $this->content['header']['right']['About'] = array( |
||
| 44 | 'url' => $this->aboutUrl, |
||
| 45 | 'menu' => $this->settings->getGlobalSetting('about_menu', array( |
||
| 46 | 'Burning Flipside' => $this->wwwUrl.'/about/event', |
||
| 47 | 'AAR, LLC' => $this->wwwUrl.'/organization/aar', |
||
| 48 | 'Privacy Policy' => $this->wwwUrl.'/about/privacy' |
||
| 49 | ))); |
||
| 50 | |||
| 51 | $this->content['urls']['wwwUrl'] = $this->wwwUrl; |
||
| 52 | $this->content['urls']['wikiUrl'] = $this->wikiUrl; |
||
| 53 | $this->content['urls']['profilesUrl'] = $this->profilesUrl; |
||
| 54 | $this->content['urls']['secureUrl'] = $this->secureUrl; |
||
| 55 | |||
| 56 | $this->content['urls']['registerUrl'] = $this->registerUrl; |
||
| 57 | $this->content['urls']['resetUrl'] = $this->resetUrl; |
||
| 58 | $this->content['urls']['loginUrl'] = $this->loginUrl; |
||
| 59 | $this->content['urls']['logoutUrl'] = $this->logoutUrl; |
||
| 60 | |||
| 61 | if($this->user === false || $this->user === null) |
||
| 62 | { |
||
| 63 | if(isset($_SERVER['REQUEST_URI']) && strstr($_SERVER['REQUEST_URI'], 'logout.php') === false) |
||
| 64 | { |
||
| 65 | $this->addLink('Login', $this->loginUrl); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | else |
||
| 69 | { |
||
| 70 | $this->addLink('Logout', $this->settings->getGlobalSetting('logout_url', $this->profilesUrl.'/logout.php')); |
||
| 71 | $this->addLinks(); |
||
| 72 | } |
||
| 73 | |||
| 74 | $this->minified = 'min'; |
||
| 75 | $this->cdn = 'cdn'; |
||
| 76 | if($this->settings->getGlobalSetting('use_minified', true) == false) |
||
| 77 | { |
||
| 78 | $this->minified = 'no'; |
||
| 79 | } |
||
| 80 | if($this->settings->getGlobalSetting('use_cdn', true) == false) |
||
| 81 | { |
||
| 82 | $this->cdn = 'no'; |
||
| 83 | $this->content['useCDN'] = false; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | public function addTemplateDir($dir, $namespace) |
||
| 91 | |||
| 92 | public function setTemplateName($name) |
||
| 96 | |||
| 97 | View Code Duplication | public function addCSS($uri) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Add a JavaScript file from its src URI |
||
| 109 | * |
||
| 110 | * @param string $uri The webpath to the JavaScript file |
||
| 111 | */ |
||
| 112 | View Code Duplication | public function addJS($uri) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Add a JavaScript file from a set of files known to the framework |
||
| 124 | * |
||
| 125 | * @param string $jsFileID the ID of the JS file |
||
| 126 | * @param boolean $async Can the JS file be loaded asynchronously? |
||
| 127 | */ |
||
| 128 | public function addWellKnownJS($jsFileID) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Add a CSS file from a set of files known to the framework |
||
| 148 | * |
||
| 149 | * @param string $cssFileID the ID of the CSS file |
||
| 150 | */ |
||
| 151 | public function addWellKnownCSS($cssFileID) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Add a link to the header |
||
| 160 | * |
||
| 161 | * @param string $name The name of the link |
||
| 162 | * @param boolean|string $url The URL to link to |
||
| 163 | * @param boolean|array $submenu Any submenu items for the dropdown |
||
| 164 | */ |
||
| 165 | public function addLink($name, $url = false, $submenu = false) |
||
| 174 | |||
| 175 | /** Notification that is green for success */ |
||
| 176 | const NOTIFICATION_SUCCESS = 'alert-success'; |
||
| 177 | /** Notification that is blue for infomrational messages */ |
||
| 178 | const NOTIFICATION_INFO = 'alert-info'; |
||
| 179 | /** Notification that is yellow for warning */ |
||
| 180 | const NOTIFICATION_WARNING = 'alert-warning'; |
||
| 181 | /** Notification that is red for error */ |
||
| 182 | const NOTIFICATION_FAILED = 'alert-danger'; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Add a notification to the page |
||
| 186 | * |
||
| 187 | * @param string $message The message to show in the notifcation |
||
| 188 | * @param string $severity The severity of the notifcation |
||
| 189 | * @param boolean $dismissible Can the user dismiss the notificaton? |
||
| 190 | */ |
||
| 191 | public function addNotification($message, $severity = self::NOTIFICATION_INFO, $dismissible = true) |
||
| 199 | |||
| 200 | protected function addLinks() |
||
| 203 | |||
| 204 | protected function getContent() |
||
| 215 | |||
| 216 | public function handleRequest($request, $response, $args) |
||
| 222 | |||
| 223 | public function printPage() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the currently requested URL |
||
| 230 | * |
||
| 231 | * @return string The full URL of the requested page |
||
| 232 | * |
||
| 233 | * @SuppressWarnings("Superglobals") |
||
| 234 | */ |
||
| 235 | public function currentURL() |
||
| 248 | } |
||
| 249 | |||
| 250 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: