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->contents() ); |
||
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() { |
||
194 | |||
195 | /** |
||
196 | * Retrieve the document object. |
||
197 | * |
||
198 | * @since 5.3.0 |
||
199 | * @return DOMDocument $doc |
||
200 | */ |
||
201 | public function get_document() { |
||
204 | |||
205 | /** |
||
206 | * Detect whether the buffer is full. |
||
207 | * |
||
208 | * @since 4.8.0 |
||
209 | * |
||
210 | * @return bool True if the buffer is full, false otherwise. |
||
211 | */ |
||
212 | public function is_full() { |
||
215 | |||
216 | /** |
||
217 | * Detect whether the buffer is empty. |
||
218 | * |
||
219 | * @since 4.8.0 |
||
220 | * |
||
221 | * @return bool True if the buffer is empty, false otherwise. |
||
222 | */ |
||
223 | public function is_empty() { |
||
229 | |||
230 | /** |
||
231 | * Update the timestamp of the buffer. |
||
232 | * |
||
233 | * @since 4.8.0 |
||
234 | * |
||
235 | * @param string $new_time A datetime string in 'YYYY-MM-DD hh:mm:ss' format. |
||
236 | */ |
||
237 | public function view_time( $new_time ) { |
||
240 | |||
241 | /** |
||
242 | * Retrieve the timestamp of the buffer. |
||
243 | * |
||
244 | * @since 4.8.0 |
||
245 | * |
||
246 | * @return string A datetime string in 'YYYY-MM-DD hh:mm:ss' format. |
||
247 | */ |
||
248 | public function last_modified() { |
||
251 | |||
252 | /** |
||
253 | * Render an associative array as an XML string. This is needed because |
||
254 | * SimpleXMLElement only handles valid XML, but we sometimes want to |
||
255 | * pass around (possibly invalid) fragments. Note that 'null' values make |
||
256 | * a tag self-closing; this is only sometimes correct (depending on the |
||
257 | * version of HTML/XML); see the list of 'void tags'. |
||
258 | * |
||
259 | * Example: |
||
260 | * |
||
261 | * array( |
||
262 | * 'html' => array( |<html xmlns="foo"> |
||
263 | * 'head' => array( | <head> |
||
264 | * 'title' => 'Woo!', | <title>Woo!</title> |
||
265 | * ), | </head> |
||
266 | * 'body' => array( ==> | <body> |
||
267 | * 'h2' => 'Some thing', | <h2>Some thing</h2> |
||
268 | * 'p' => 'it's all up ons', | <p>it's all up ons</p> |
||
269 | * 'br' => null, | <br /> |
||
270 | * ), | </body> |
||
271 | * ), |</html> |
||
272 | * ) |
||
273 | * |
||
274 | * @access protected |
||
275 | * @since 3.9.0 |
||
276 | * @since 4.8.0 Rename, add $depth parameter, and change return type. |
||
277 | * @since 5.3.0 Refactor, remove $depth parameter, add $parent and $root, make access protected. |
||
278 | * |
||
279 | * @param array $array A recursive associative array of tag/child relationships. |
||
280 | * @param DOMElement $parent (optional) an element to which new children should be added. |
||
281 | * @param DOMDocument $root (optional) the parent document. |
||
282 | * |
||
283 | * @return string|DOMDocument The rendered XML string or an object if root element is specified. |
||
284 | */ |
||
285 | protected function array_to_xml_string( $array, $parent = null, $root = null ) { |
||
322 | } |
||
323 |
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: