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 |
||
24 | class ElementTray extends Element implements ContainerInterface |
||
25 | { |
||
26 | /** |
||
27 | * array of form element objects |
||
28 | * |
||
29 | * @var Element[] |
||
30 | */ |
||
31 | protected $elements = array(); |
||
32 | |||
33 | /** |
||
34 | * __construct |
||
35 | * |
||
36 | * @param string|array $caption caption or array of all attributes |
||
37 | * Control attributes: |
||
38 | * :joiner joiner for elements in tray |
||
39 | * @param string $joiner joiner for elements in tray |
||
40 | * @param string $name name |
||
41 | */ |
||
42 | 16 | View Code Duplication | public function __construct($caption, $joiner = ' ', $name = '') |
43 | { |
||
44 | 16 | if (is_array($caption)) { |
|
45 | 4 | parent::__construct($caption); |
|
46 | 4 | $this->setIfNotSet(':joiner', ' '); |
|
47 | } else { |
||
48 | 14 | parent::__construct(); |
|
49 | 14 | $this->setName($name); |
|
50 | 14 | $this->setCaption($caption); |
|
51 | 14 | $this->set(':joiner', $joiner); |
|
52 | } |
||
53 | 16 | } |
|
54 | |||
55 | /** |
||
56 | * Are there are required elements? |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | 2 | public function isRequired() |
|
69 | |||
70 | /** |
||
71 | * Add an element to the tray |
||
72 | * |
||
73 | * @param Element $formElement Element to add |
||
74 | * @param boolean $required true = entry required |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | 12 | public function addElement(Element $formElement, $required = false) |
|
79 | { |
||
80 | 12 | $this->elements[] = $formElement; |
|
81 | 12 | if ($required) { |
|
82 | 1 | $formElement->setRequired(); |
|
83 | } |
||
84 | 12 | } |
|
85 | |||
86 | // ContainerInterface |
||
87 | /** |
||
88 | * get an array of "required" form elements |
||
89 | * |
||
90 | * @return array array of Element objects |
||
91 | */ |
||
92 | 1 | View Code Duplication | public function getRequired() |
93 | { |
||
94 | 1 | $required = []; |
|
95 | 1 | foreach ($this->elements as $el) { |
|
96 | 1 | if ($el->isRequired()) { |
|
97 | 1 | $required[] = $el; |
|
98 | } |
||
99 | } |
||
100 | 1 | return $required; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Get an array of the elements in this group |
||
105 | * |
||
106 | * @param bool $recurse get elements recursively? |
||
107 | * |
||
108 | * @return array Array of Element objects. |
||
109 | */ |
||
110 | 10 | View Code Duplication | public function getElements($recurse = false) |
133 | |||
134 | /** |
||
135 | * Get the delimiter of this group |
||
136 | * |
||
137 | * @param boolean $encode True to encode special characters |
||
138 | * |
||
139 | * @return string The delimiter |
||
140 | */ |
||
141 | 5 | protected function getJoiner($encode = false) |
|
146 | |||
147 | /** |
||
148 | * prepare HTML to output this group |
||
149 | * |
||
150 | * @return string HTML output |
||
151 | */ |
||
152 | 8 | public function render() |
|
174 | } |
||
175 |