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 |
||
| 25 | abstract class Jetpack_Sitemap_Buffer { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Largest number of items the buffer can hold. |
||
| 29 | * |
||
| 30 | * @access protected |
||
| 31 | * @since 4.8.0 |
||
| 32 | * @var int $item_capacity The item capacity. |
||
| 33 | */ |
||
| 34 | protected $item_capacity; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Largest number of bytes the buffer can hold. |
||
| 38 | * |
||
| 39 | * @access protected |
||
| 40 | * @since 4.8.0 |
||
| 41 | * @var int $byte_capacity The byte capacity. |
||
| 42 | */ |
||
| 43 | protected $byte_capacity; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Flag which detects when the buffer is full. |
||
| 47 | * |
||
| 48 | * @access protected |
||
| 49 | * @since 4.8.0 |
||
| 50 | * @var bool $is_full_flag The flag value. This flag is set to false on construction and only flipped to true if we've tried to add something and failed. |
||
| 51 | */ |
||
| 52 | protected $is_full_flag; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Flag which detects when the buffer is empty. |
||
| 56 | * |
||
| 57 | * @access protected |
||
| 58 | * @since 4.8.0 |
||
| 59 | * @var bool $is_empty_flag The flag value. This flag is set to true on construction and only flipped to false if we've tried to add something and succeeded. |
||
| 60 | */ |
||
| 61 | protected $is_empty_flag; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The most recent timestamp seen by the buffer. |
||
| 65 | * |
||
| 66 | * @access protected |
||
| 67 | * @since 4.8.0 |
||
| 68 | * @var string $timestamp Must be in 'YYYY-MM-DD hh:mm:ss' format. |
||
| 69 | */ |
||
| 70 | protected $timestamp; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The DOM document object that is currently being used to construct the XML doc. |
||
| 74 | * |
||
| 75 | * @access protected |
||
| 76 | * @since 5.3.0 |
||
| 77 | * @var DOMDocument $doc |
||
| 78 | */ |
||
| 79 | protected $doc = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The root DOM element object that holds everything inside. Do not use directly, call |
||
| 83 | * the get_root_element getter method instead. |
||
| 84 | * |
||
| 85 | * @access protected |
||
| 86 | * @since 5.3.0 |
||
| 87 | * @var DOMElement $doc |
||
| 88 | */ |
||
| 89 | protected $root = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Helper class to construct sitemap paths. |
||
| 93 | * |
||
| 94 | * @since 5.3.0 |
||
| 95 | * @protected |
||
| 96 | * @var Jetpack_Sitemap_Finder |
||
| 97 | */ |
||
| 98 | protected $finder; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Construct a new Jetpack_Sitemap_Buffer. |
||
| 102 | * |
||
| 103 | * @since 4.8.0 |
||
| 104 | * |
||
| 105 | * @param int $item_limit The maximum size of the buffer in items. |
||
| 106 | * @param int $byte_limit The maximum size of the buffer in bytes. |
||
| 107 | * @param string $time The initial datetime of the buffer. Must be in 'YYYY-MM-DD hh:mm:ss' format. |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function __construct( $item_limit, $byte_limit, $time ) { |
|
| 110 | $this->is_full_flag = false; |
||
| 111 | $this->timestamp = $time; |
||
| 112 | |||
| 113 | $this->finder = new Jetpack_Sitemap_Finder(); |
||
| 114 | $this->doc = new DOMDocument( '1.0', 'UTF-8' ); |
||
| 115 | |||
| 116 | $this->item_capacity = max( 1, intval( $item_limit ) ); |
||
| 117 | $this->byte_capacity = max( 1, intval( $byte_limit ) ) - strlen( $this->doc->saveXML() ); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns a DOM element that contains all sitemap elements. |
||
| 122 | * |
||
| 123 | * @access protected |
||
| 124 | * @since 5.3.0 |
||
| 125 | * @return DOMElement $root |
||
| 126 | */ |
||
| 127 | abstract protected function get_root_element(); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Append an item to the buffer, if there is room for it, |
||
| 131 | * and set is_empty_flag to false. If there is no room, |
||
| 132 | * we set is_full_flag to true. If $item is null, |
||
| 133 | * don't do anything and report success. |
||
| 134 | * |
||
| 135 | * @since 4.8.0 |
||
| 136 | * |
||
| 137 | * @param string $item The item to be added. |
||
| 138 | * |
||
| 139 | * @return bool True if the append succeeded, False if not. |
||
| 140 | */ |
||
| 141 | public function try_to_add_item( $item ) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Append an item to the buffer, if there is room for it, |
||
| 152 | * and set is_empty_flag to false. If there is no room, |
||
| 153 | * we set is_full_flag to true. If $item is null, |
||
| 154 | * don't do anything and report success. |
||
| 155 | * |
||
| 156 | * @since 5.3.0 |
||
| 157 | * |
||
| 158 | * @param array $array The item to be added. |
||
| 159 | * |
||
| 160 | * @return bool True if the append succeeded, False if not. |
||
| 161 | */ |
||
| 162 | public function append( $array ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Retrieve the contents of the buffer. |
||
| 186 | * |
||
| 187 | * @since 4.8.0 |
||
| 188 | * |
||
| 189 | * @return string The contents of the buffer (with the footer included). |
||
| 190 | */ |
||
| 191 | public function contents() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Retrieve the document object. |
||
| 201 | * |
||
| 202 | * @since 5.3.0 |
||
| 203 | * @return DOMDocument $doc |
||
| 204 | */ |
||
| 205 | public function get_document() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Detect whether the buffer is full. |
||
| 211 | * |
||
| 212 | * @since 4.8.0 |
||
| 213 | * |
||
| 214 | * @return bool True if the buffer is full, false otherwise. |
||
| 215 | */ |
||
| 216 | public function is_full() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Detect whether the buffer is empty. |
||
| 222 | * |
||
| 223 | * @since 4.8.0 |
||
| 224 | * |
||
| 225 | * @return bool True if the buffer is empty, false otherwise. |
||
| 226 | */ |
||
| 227 | public function is_empty() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Update the timestamp of the buffer. |
||
| 236 | * |
||
| 237 | * @since 4.8.0 |
||
| 238 | * |
||
| 239 | * @param string $new_time A datetime string in 'YYYY-MM-DD hh:mm:ss' format. |
||
| 240 | */ |
||
| 241 | public function view_time( $new_time ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Retrieve the timestamp of the buffer. |
||
| 247 | * |
||
| 248 | * @since 4.8.0 |
||
| 249 | * |
||
| 250 | * @return string A datetime string in 'YYYY-MM-DD hh:mm:ss' format. |
||
| 251 | */ |
||
| 252 | public function last_modified() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Render an associative array as an XML string. This is needed because |
||
| 258 | * SimpleXMLElement only handles valid XML, but we sometimes want to |
||
| 259 | * pass around (possibly invalid) fragments. Note that 'null' values make |
||
| 260 | * a tag self-closing; this is only sometimes correct (depending on the |
||
| 261 | * version of HTML/XML); see the list of 'void tags'. |
||
| 262 | * |
||
| 263 | * Example: |
||
| 264 | * |
||
| 265 | * array( |
||
| 266 | * 'html' => array( |<html xmlns="foo"> |
||
| 267 | * 'head' => array( | <head> |
||
| 268 | * 'title' => 'Woo!', | <title>Woo!</title> |
||
| 269 | * ), | </head> |
||
| 270 | * 'body' => array( ==> | <body> |
||
| 271 | * 'h2' => 'Some thing', | <h2>Some thing</h2> |
||
| 272 | * 'p' => 'it's all up ons', | <p>it's all up ons</p> |
||
| 273 | * 'br' => null, | <br /> |
||
| 274 | * ), | </body> |
||
| 275 | * ), |</html> |
||
| 276 | * ) |
||
| 277 | * |
||
| 278 | * @access protected |
||
| 279 | * @since 3.9.0 |
||
| 280 | * @since 4.8.0 Rename, add $depth parameter, and change return type. |
||
| 281 | * @since 5.3.0 Refactor, remove $depth parameter, add $parent and $root, make access protected. |
||
| 282 | * |
||
| 283 | * @param array $array A recursive associative array of tag/child relationships. |
||
| 284 | * @param DOMElement $parent (optional) an element to which new children should be added. |
||
| 285 | * @param DOMDocument $root (optional) the parent document. |
||
| 286 | * |
||
| 287 | * @return string|DOMDocument The rendered XML string or an object if root element is specified. |
||
| 288 | */ |
||
| 289 | protected function array_to_xml_string( $array, $parent = null, $root = null ) { |
||
| 326 | } |
||
| 327 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: