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 | const CSS = 'css'; |
||
| 24 | const JS = 'js'; |
||
| 25 | const ADDITIONAL = 'additional'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The array of all included libraries. |
||
| 29 | * |
||
| 30 | * @var array<WebLibraryInterface> |
||
| 31 | */ |
||
| 32 | private $webLibraries = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * false if the toHtml method has not yet been called, true if it has been called. |
||
| 36 | * @var boolean |
||
| 37 | */ |
||
| 38 | private $rendered = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Adds a library to the list of libraries that should be loaded in the web page. |
||
| 42 | * <p>The function will also load the dependencies (if any) and will have no effect if the library has already been loaded.</p> |
||
| 43 | * |
||
| 44 | * @param WebLibraryInterface $library |
||
| 45 | */ |
||
| 46 | public function addLibrary(WebLibraryInterface $library) { |
||
| 63 | |||
| 64 | // TODO: add: addJs and addCss file |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The list of all libraries that should be loaded in the web page. |
||
| 68 | * <p>If you do not pass all dependencies of a library, the dependencies will be loaded automatically.</p> |
||
| 69 | * |
||
| 70 | * @Property |
||
| 71 | * @param array<WebLibraryInterface> $libraries |
||
| 72 | */ |
||
| 73 | public function setWebLibraries($libraries) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Renders the HTML in charge of loading CSS and JS files. |
||
| 81 | * The Html is echoed directly into the output. |
||
| 82 | * This function should be called within the head tag. |
||
| 83 | * |
||
| 84 | */ |
||
| 85 | public function toHtml() |
||
| 86 | { |
||
| 87 | /*if ($this->rendered) { |
||
| 88 | throw new WebLibraryException("The library has already been rendered."); |
||
| 89 | }*/ |
||
| 90 | |||
| 91 | $this->toCss(); |
||
| 92 | $this->toHtml(); |
||
| 93 | $this->toAdditional(); |
||
| 94 | |||
| 95 | $this->rendered = true; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Renders the HTML in charge of loading CSS files. |
||
| 100 | * The Html is echoed directly into the output. |
||
| 101 | * |
||
| 102 | */ |
||
| 103 | View Code Duplication | public function toCss() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Renders the HTML in charge of loading JS files. |
||
| 125 | * The Html is echoed directly into the output. |
||
| 126 | * |
||
| 127 | */ |
||
| 128 | View Code Duplication | public function toJs() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Renders the HTML in charge of loading additional files. |
||
| 150 | * The Html is echoed directly into the output. |
||
| 151 | * |
||
| 152 | */ |
||
| 153 | View Code Duplication | public function toAdditional() |
|
| 172 | } |
||
| 173 | ?> |
||
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.