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 |
||
| 13 | class Menu_Item extends Basis { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Children. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $children = array(); |
||
| 21 | |||
| 22 | /** |
||
| 23 | * CSS Classes. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $classes = array(); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * If item has child. |
||
| 31 | * |
||
| 32 | * @var boolean |
||
| 33 | */ |
||
| 34 | protected $has_child = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Nesting level. |
||
| 38 | * |
||
| 39 | * @var integer |
||
| 40 | */ |
||
| 41 | public $level = 0; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Item title. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | public $title; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Checks if provided arg is instance of WP_Post and inits it. |
||
| 52 | * |
||
| 53 | * @param \WP_Post $item WP_Post object. |
||
| 54 | */ |
||
| 55 | public function __construct( $item ) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Returns item title. |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function get_title() { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Returns item slug. |
||
| 73 | * |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public function get_slug() { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Returns item link (url). |
||
| 82 | * |
||
| 83 | * @return string |
||
| 84 | */ |
||
| 85 | public function get_link() { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns item children, if there are any. |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function get_children() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns menu item classes. |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function get_classes() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Adds css class to classes array. |
||
| 109 | * |
||
| 110 | * @param string $class_name CSS class name. |
||
| 111 | */ |
||
| 112 | public function add_class( $class_name ) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Adds child to current Menu_Item. |
||
| 118 | * |
||
| 119 | * @param Menu_Item $item Menu_Item object. |
||
| 120 | */ |
||
| 121 | public function add_child( $item ) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Applies filters for item classes. |
||
| 137 | */ |
||
| 138 | protected function filter_classes() { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Updates children nesting level param. |
||
| 144 | * |
||
| 145 | * @return boolean |
||
| 146 | */ |
||
| 147 | View Code Duplication | protected function update_child_levels() { |
|
| 159 | } |
||
| 160 |
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: