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 |
||
5 | abstract class Model implements \IteratorAggregate |
||
6 | { |
||
7 | protected $client; |
||
8 | private $data; |
||
9 | |||
10 | public function __construct(GoogleBooks $client, \stdClass $data) |
||
15 | |||
16 | /** |
||
17 | * Returns true if the model is created from a search result response |
||
18 | * (and thus do not contain all the data of the full record). |
||
19 | * |
||
20 | * @return bool |
||
21 | */ |
||
22 | public function isSearchResult() |
||
26 | |||
27 | /** |
||
28 | * Expand a search result response object to a full record. |
||
29 | */ |
||
30 | public function expandToFullRecord() |
||
35 | |||
36 | /** |
||
37 | * Special method that allows the object to be iterated over, for example |
||
38 | * with a foreach statement. |
||
39 | */ |
||
40 | public function getIterator() |
||
44 | |||
45 | /** |
||
46 | * Get an item from an array using "dot" notation. |
||
47 | * |
||
48 | * @param string $key |
||
49 | * @param mixed $default |
||
50 | * @return mixed |
||
51 | */ |
||
52 | View Code Duplication | public function get($key, $default = null) |
|
63 | |||
64 | /** |
||
65 | * Check if an item or items exist in an array using "dot" notation. |
||
66 | * |
||
67 | * @param string $key |
||
68 | * @return mixed |
||
69 | */ |
||
70 | View Code Duplication | public function has($key) |
|
81 | |||
82 | /** |
||
83 | * Get a string representation of the object |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public function __toString() |
||
91 | |||
92 | /** |
||
93 | * Provide object-like access to the data. |
||
94 | * |
||
95 | * @param string $key |
||
96 | * @return mixed |
||
97 | */ |
||
98 | public function __get($key) |
||
102 | } |
||
103 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.