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) |
||
74 | |||
75 | public function addTemplateDir($dir, $namespace) |
||
79 | |||
80 | public function setTemplateName($name) |
||
84 | |||
85 | View Code Duplication | public function addCSS($uri) |
|
94 | |||
95 | /** |
||
96 | * Add a JavaScript file from its src URI |
||
97 | * |
||
98 | * @param string $uri The webpath to the JavaScript file |
||
99 | */ |
||
100 | View Code Duplication | public function addJS($uri) |
|
109 | |||
110 | /** |
||
111 | * Add a JavaScript file from a set of files known to the framework |
||
112 | * |
||
113 | * @param string $jsFileID the ID of the JS file |
||
114 | * @param boolean $async Can the JS file be loaded asynchronously? |
||
115 | */ |
||
116 | public function addWellKnownJS($jsFileID) |
||
122 | |||
123 | /** |
||
124 | * Add a CSS file from a set of files known to the framework |
||
125 | * |
||
126 | * @param string $cssFileID the ID of the CSS file |
||
127 | */ |
||
128 | public function addWellKnownCSS($cssFileID) |
||
134 | |||
135 | /** |
||
136 | * Add a link to the header |
||
137 | * |
||
138 | * @param string $name The name of the link |
||
139 | * @param boolean|string $url The URL to link to |
||
140 | * @param boolean|array $submenu Any submenu items for the dropdown |
||
141 | */ |
||
142 | public function addLink($name, $url = false, $submenu = false) |
||
151 | |||
152 | /** Notification that is green for success */ |
||
153 | const NOTIFICATION_SUCCESS = 'alert-success'; |
||
154 | /** Notification that is blue for infomrational messages */ |
||
155 | const NOTIFICATION_INFO = 'alert-info'; |
||
156 | /** Notification that is yellow for warning */ |
||
157 | const NOTIFICATION_WARNING = 'alert-warning'; |
||
158 | /** Notification that is red for error */ |
||
159 | const NOTIFICATION_FAILED = 'alert-danger'; |
||
160 | |||
161 | /** |
||
162 | * Add a notification to the page |
||
163 | * |
||
164 | * @param string $message The message to show in the notifcation |
||
165 | * @param string $severity The severity of the notifcation |
||
166 | * @param boolean $dismissible Can the user dismiss the notificaton? |
||
167 | */ |
||
168 | public function addNotification($message, $severity = self::NOTIFICATION_INFO, $dismissible = true) |
||
176 | |||
177 | protected function addLinks() |
||
180 | |||
181 | protected function getContent() |
||
191 | |||
192 | public function handleRequest($request, $response, $args) |
||
198 | |||
199 | public function printPage() |
||
203 | |||
204 | /** |
||
205 | * Get the currently requested URL |
||
206 | * |
||
207 | * @return string The full URL of the requested page |
||
208 | * |
||
209 | * @SuppressWarnings("Superglobals") |
||
210 | */ |
||
211 | View Code Duplication | public function currentURL() |
|
224 | } |
||
225 | |||
226 |
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: