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 |
||
| 22 | class WebLibraryManager implements HtmlElementInterface { |
||
| 23 | /** |
||
| 24 | * The array of all included libraries. |
||
| 25 | * |
||
| 26 | * @var array<WebLibraryInterface> |
||
| 27 | */ |
||
| 28 | private $webLibraries = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * false if the toHtml method has not yet been called, true if it has been called. |
||
| 32 | * @var boolean |
||
| 33 | */ |
||
| 34 | private $rendered = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Adds a library to the list of libraries that should be loaded in the web page. |
||
| 38 | * <p>The function will also load the dependencies (if any) and will have no effect if the library has already been loaded.</p> |
||
| 39 | * |
||
| 40 | * @param WebLibraryInterface $library |
||
| 41 | */ |
||
| 42 | public function addLibrary(WebLibraryInterface $library) { |
||
| 59 | |||
| 60 | // TODO: add: addJs and addCss file |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The list of all libraries that should be loaded in the web page. |
||
| 64 | * <p>If you do not pass all dependencies of a library, the dependencies will be loaded automatically.</p> |
||
| 65 | * |
||
| 66 | * @Property |
||
| 67 | * @param array<WebLibraryInterface> $libraries |
||
| 68 | */ |
||
| 69 | public function setWebLibraries($libraries) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Renders the HTML in charge of loading CSS and JS files. |
||
| 77 | * The Html is echoed directly into the output. |
||
| 78 | * This function should be called within the head tag. |
||
| 79 | * |
||
| 80 | */ |
||
| 81 | public function toHtml() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Renders the HTML in charge of loading CSS files. |
||
| 96 | * The Html is echoed directly into the output. |
||
| 97 | * |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function toCss() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Renders the HTML in charge of loading JS files. |
||
| 121 | * The Html is echoed directly into the output. |
||
| 122 | * |
||
| 123 | */ |
||
| 124 | View Code Duplication | public function toJs() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Renders the HTML in charge of loading additional files. |
||
| 146 | * The Html is echoed directly into the output. |
||
| 147 | * |
||
| 148 | */ |
||
| 149 | View Code Duplication | public function toAdditional() |
|
| 168 | } |
||
| 169 | ?> |
||
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.