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 |
||
3 | class Nip_Form_Element_Dateselect extends Nip_Form_Element_MultiElement |
||
|
|||
4 | { |
||
5 | protected $_type = 'dateselect'; |
||
6 | protected $_locale = 'ct_EN'; |
||
7 | protected $format = 'M d Y'; |
||
8 | |||
9 | public function init() |
||
10 | { |
||
11 | parent::init(); |
||
12 | |||
13 | $localeObj = Nip_Locale::instance(); |
||
14 | $this->setLocale($localeObj->getCurrent()); |
||
15 | $this->setFormat($localeObj->getOption(['time', 'dateFormat'])); |
||
16 | |||
17 | $this->initSelects(); |
||
18 | } |
||
19 | |||
20 | public function initSelects() |
||
21 | { |
||
22 | $inputName = $this->getName(); |
||
23 | |||
24 | if (!$this->hasElement('day')) { |
||
25 | $dayElement = $this->getForm()->getNewElement('select'); |
||
26 | |||
27 | for ($i = 1; $i <= 31; $i++) { |
||
28 | $dayElement->addOption($i, $i); |
||
29 | } |
||
30 | $dayElement->setValue(date('d')); |
||
31 | $this->elements['day'] = $dayElement; |
||
32 | } |
||
33 | |||
34 | |||
35 | if (!$this->hasElement('month')) { |
||
36 | $monthElement = $this->getForm()->getNewElement('select'); |
||
37 | for ($i = 1; $i <= 12; $i++) { |
||
38 | $monthElement->addOption($i, date('M', mktime(0, 0, 0, $i, 1, 2014))); |
||
39 | } |
||
40 | $monthElement->setValue(date('m')); |
||
41 | $this->elements['month'] = $monthElement; |
||
42 | } |
||
43 | |||
44 | if (!$this->hasElement('year')) { |
||
45 | $yearElement = $this->getForm()->getNewElement('select'); |
||
46 | $currentYear = date('Y'); |
||
47 | $startYear = $currentYear - 100; |
||
48 | $endYear = $currentYear + 5; |
||
49 | for ($i = $startYear; $i <= $endYear; $i++) { |
||
50 | $yearElement->addOption($i, $i); |
||
51 | } |
||
52 | $yearElement->setValue(date('Y')); |
||
53 | $this->elements['year'] = $yearElement; |
||
54 | } |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @inheritdoc |
||
59 | */ |
||
60 | public function setName($name) |
||
61 | { |
||
62 | $return = parent::setName($name); |
||
63 | $this->updateNameSelects(); |
||
64 | return $return; |
||
65 | } |
||
66 | |||
67 | public function updateNameSelects() |
||
68 | { |
||
69 | $inputName = $this->getName(); |
||
70 | $this->getElement('day')->setName($inputName . '[day]'); |
||
71 | $this->getElement('month')->setName($inputName . '[month]'); |
||
72 | $this->getElement('year')->setName($inputName . '[year]'); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | public function getLocale() |
||
79 | { |
||
80 | return $this->_locale; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param $format |
||
85 | */ |
||
86 | public function setLocale($format) |
||
87 | { |
||
88 | $this->_locale = $format; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getFormat() |
||
95 | { |
||
96 | return $this->format; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param $format |
||
101 | */ |
||
102 | public function setFormat($format) |
||
103 | { |
||
104 | $this->format = $format; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param $data |
||
109 | * @param string $source |
||
110 | * @return Nip_Form_Element_Abstract |
||
111 | */ |
||
112 | public function getData($data, $source = 'abstract') |
||
113 | { |
||
114 | if ($source == 'model') { |
||
115 | if ($data && $data != '0000-00-00' && $data != '0000-00-00 00:00:00') { |
||
116 | $dateUnix = strtotime($data); |
||
117 | if ($dateUnix && $dateUnix !== false && $dateUnix > -62169989992) { |
||
118 | $this->getElement('day')->setValue(date('d', $dateUnix)); |
||
119 | $this->getElement('month')->setValue(date('m', $dateUnix)); |
||
120 | $this->getElement('year')->setValue(date('Y', $dateUnix)); |
||
121 | } |
||
122 | } |
||
123 | return $this; |
||
124 | } |
||
125 | return parent::getData($data, $source); |
||
126 | } |
||
127 | |||
128 | /** @noinspection PhpMissingParentCallCommonInspection |
||
129 | * @param $request |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function getDataFromRequest($request) |
||
144 | } |
||
145 | |||
146 | public function validate() |
||
147 | { |
||
148 | parent::validate(); |
||
149 | if (!$this->isError()) { |
||
150 | $value = $this->getValue(); |
||
151 | if ($value) { |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | /** @noinspection PhpMissingParentCallCommonInspection |
||
157 | * @param string $requester |
||
158 | * @return string|null |
||
159 | */ |
||
160 | public function getValue($requester = 'abstract') |
||
161 | { |
||
162 | $unixTime = $this->getUnix(); |
||
163 | $format = $requester == 'model' ? 'Y-m-d' : $this->format; |
||
164 | if ($unixTime) { |
||
165 | return date($format, $unixTime); |
||
166 | } |
||
167 | |||
168 | return; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param bool $format |
||
173 | * @return false|int |
||
174 | */ |
||
175 | public function getUnix($format = false) |
||
182 | } |
||
183 | } |
||
184 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.