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 | /** |
||
| 25 | * The array of all included libraries. |
||
| 26 | * |
||
| 27 | * @var array<WebLibraryInterface> |
||
| 28 | */ |
||
| 29 | private $webLibraries = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * false if the toHtml method has not yet been called, true if it has been called. |
||
| 33 | * @var boolean |
||
| 34 | */ |
||
| 35 | private $rendered = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Adds a library to the list of libraries that should be loaded in the web page. |
||
| 39 | * <p>The function will also load the dependencies (if any) and will have no effect if the library has already been loaded.</p> |
||
| 40 | * |
||
| 41 | * @param WebLibraryInterface $library |
||
| 42 | */ |
||
| 43 | public function addLibrary(WebLibraryInterface $library) { |
||
| 60 | |||
| 61 | // TODO: add: addJs and addCss file |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The list of all libraries that should be loaded in the web page. |
||
| 65 | * <p>If you do not pass all dependencies of a library, the dependencies will be loaded automatically.</p> |
||
| 66 | * |
||
| 67 | * @Property |
||
| 68 | * @param array<WebLibraryInterface> $libraries |
||
| 69 | */ |
||
| 70 | public function setWebLibraries($libraries) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Renders the HTML in charge of loading CSS and JS files. |
||
| 78 | * The Html is echoed directly into the output. |
||
| 79 | * This function should be called within the head tag. |
||
| 80 | * |
||
| 81 | */ |
||
| 82 | public function toHtml() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Renders the HTML in charge of loading CSS files. |
||
| 97 | * The Html is echoed directly into the output. |
||
| 98 | * |
||
| 99 | */ |
||
| 100 | View Code Duplication | public function cssToHtml() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Renders the HTML in charge of loading JS files. |
||
| 122 | * The Html is echoed directly into the output. |
||
| 123 | * |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function jsToHtml() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Renders the HTML in charge of loading additional files. |
||
| 147 | * The Html is echoed directly into the output. |
||
| 148 | * |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function additionalToHtml() |
|
| 169 | } |
||
| 170 | ?> |
||
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.