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 |
||
9 | class Menu_Item extends Basis { |
||
10 | |||
11 | /** |
||
12 | * Children |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $children = array(); |
||
16 | |||
17 | /** |
||
18 | * CSS Classes |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $classes = array(); |
||
22 | |||
23 | /** |
||
24 | * If item has child |
||
25 | * @var boolean |
||
26 | */ |
||
27 | protected $has_child = false; |
||
28 | |||
29 | /** |
||
30 | * Nesting level |
||
31 | * @var integer |
||
32 | */ |
||
33 | public $level = 0; |
||
34 | |||
35 | /** |
||
36 | * Item title |
||
37 | * @var string |
||
38 | */ |
||
39 | public $title; |
||
40 | |||
41 | /** |
||
42 | * Checks if provided arg is instance of WP_Post and inits it |
||
43 | * |
||
44 | * @param \WP_Post $item |
||
45 | */ |
||
46 | public function __construct( $item ) { |
||
56 | |||
57 | /** |
||
58 | * Returns item title |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | public function get_title() { |
||
67 | |||
68 | /** |
||
69 | * Returns item slug |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function get_slug() { |
||
78 | |||
79 | /** |
||
80 | * Returns item link (url) |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | public function get_link() { |
||
89 | |||
90 | /** |
||
91 | * Retuns item children, if there are any |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public function get_children() { |
||
100 | |||
101 | /** |
||
102 | * Returns menu item classes |
||
103 | * @return string |
||
104 | */ |
||
105 | public function get_classes() { |
||
110 | |||
111 | /** |
||
112 | * Adds css class to classes array |
||
113 | * |
||
114 | * @param string $class_name |
||
115 | */ |
||
116 | public function add_class( $class_name ) { |
||
121 | |||
122 | /** |
||
123 | * Adds child to current Menu_Item |
||
124 | * |
||
125 | * @param Menu_Item $item |
||
126 | */ |
||
127 | public function add_child( $item ) { |
||
142 | |||
143 | /** |
||
144 | * Applies filters for item classes |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | protected function filter_classes() { |
||
153 | |||
154 | /** |
||
155 | * Updates children nesting level param |
||
156 | * |
||
157 | * @return boolean |
||
158 | */ |
||
159 | View Code Duplication | protected function update_child_levels() { |
|
175 | } |
||
176 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: