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_Timeselect extends Nip_Form_Element_MultiElement |
||
|
|
|||
| 4 | { |
||
| 5 | protected $_type = 'timeselect'; |
||
| 6 | |||
| 7 | 1 | public function init() |
|
| 8 | { |
||
| 9 | 1 | parent::init(); |
|
| 10 | |||
| 11 | 1 | $this->initSelects(); |
|
| 12 | 1 | } |
|
| 13 | |||
| 14 | 1 | public function initSelects() |
|
| 15 | { |
||
| 16 | 1 | if (! isset($this->elements['hours'])) { |
|
| 17 | 1 | $hoursElement = $this->getForm()->getNewSelectElement(); |
|
| 18 | |||
| 19 | 1 | $hoursElement->addOption('-', 'HH'); |
|
| 20 | 1 | for ($i = 0; $i <= 24; $i++) { |
|
| 21 | 1 | $hoursElement->addOption($i, $i . 'h'); |
|
| 22 | } |
||
| 23 | 1 | $hoursElement->setValue('-'); |
|
| 24 | |||
| 25 | 1 | $this->elements['hours'] = $hoursElement; |
|
| 26 | } |
||
| 27 | |||
| 28 | |||
| 29 | 1 | if (! isset($this->elements['minutes'])) { |
|
| 30 | 1 | $minutesElement = $this->getForm()->getNewSelectElement(); |
|
| 31 | |||
| 32 | 1 | $minutesElement->addOption('-', 'MM'); |
|
| 33 | 1 | for ($i = 0; $i <= 59; $i++) { |
|
| 34 | 1 | $minutesElement->addOption($i, $i . 'm'); |
|
| 35 | } |
||
| 36 | 1 | $minutesElement->setValue('-'); |
|
| 37 | |||
| 38 | 1 | $this->elements['minutes'] = $minutesElement; |
|
| 39 | } |
||
| 40 | |||
| 41 | 1 | if (! isset($this->elements['seconds'])) { |
|
| 42 | 1 | $secondsElement = $this->getForm()->getNewSelectElement(); |
|
| 43 | |||
| 44 | 1 | $secondsElement->addOption('-', 'SS'); |
|
| 45 | 1 | for ($i = 0; $i <= 59; $i++) { |
|
| 46 | 1 | $secondsElement->addOption($i, $i . 's'); |
|
| 47 | } |
||
| 48 | 1 | $secondsElement->setValue('-'); |
|
| 49 | |||
| 50 | 1 | $this->elements['seconds'] = $secondsElement; |
|
| 51 | } |
||
| 52 | 1 | } |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @inheritdoc |
||
| 56 | */ |
||
| 57 | public function setName($name) |
||
| 58 | { |
||
| 59 | $return = parent::setName($name); |
||
| 60 | $this->updateNameSelects(); |
||
| 61 | |||
| 62 | return $return; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function updateNameSelects() |
||
| 66 | { |
||
| 67 | $inputName = $this->getName(); |
||
| 68 | $this->elements['hours']->setName($inputName . '[hours]'); |
||
| 69 | $this->elements['minutes']->setName($inputName . '[minutes]'); |
||
| 70 | $this->elements['seconds']->setName($inputName . '[seconds]'); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @inheritdoc |
||
| 75 | */ |
||
| 76 | public function getData($data, $source = 'abstract') |
||
| 77 | { |
||
| 78 | if ($source == 'model') { |
||
| 79 | $dateUnix = strtotime($data); |
||
| 80 | if ($dateUnix && $dateUnix !== false && $dateUnix > -62169989992) { |
||
| 81 | $this->elements['hours']->setValue(date('H', $dateUnix)); |
||
| 82 | $this->elements['minutes']->setValue(date('i', $dateUnix)); |
||
| 83 | $this->elements['seconds']->setValue(date('s', $dateUnix)); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $this; |
||
| 87 | } |
||
| 88 | |||
| 89 | return parent::getData($data, $source); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** @noinspection PhpMissingParentCallCommonInspection |
||
| 93 | * @inheritdoc |
||
| 94 | */ |
||
| 95 | 1 | public function getDataFromRequest($request) |
|
| 96 | { |
||
| 97 | 1 | if (is_array($request)) { |
|
| 98 | 1 | $elements = $this->getElements(); |
|
| 99 | 1 | foreach ($elements as $key => $element) { |
|
| 100 | 1 | if (isset($request[$key])) { |
|
| 101 | 1 | $value = $request[$key]; |
|
| 102 | 1 | $element->setValue($value); |
|
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | 1 | return $this; |
|
| 108 | } |
||
| 109 | |||
| 110 | public function validate() |
||
| 111 | { |
||
| 112 | parent::validate(); |
||
| 113 | if (! $this->isError()) { |
||
| 114 | $value = $this->getValue(); |
||
| 115 | if ($value) { |
||
| 116 | $expectedValue = str_pad(intval($this->elements['hours']->getValue()), 2, "0", STR_PAD_LEFT); |
||
| 117 | $expectedValue .= ':' . str_pad(intval($this->elements['minutes']->getValue()), 2, "0", STR_PAD_LEFT); |
||
| 118 | $expectedValue .= ':' . str_pad(intval($this->elements['seconds']->getValue()), 2, "0", STR_PAD_LEFT); |
||
| 119 | if ($expectedValue != $value) { |
||
| 120 | $message = $this->getForm()->getMessageTemplate('bad-' . $this->getName()); |
||
| 121 | $message = $message ? $message : 'I couldn\'t parse the ' . strtolower($this->getLabel()) . ' you entered'; |
||
| 122 | $this->addError($message); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** @noinspection PhpMissingDocCommentInspection |
||
| 129 | * @inheritdoc |
||
| 130 | */ |
||
| 131 | 1 | public function getValue($requester = 'abstract') |
|
| 139 | } |
||
| 140 | |||
| 141 | |||
| 142 | /** @noinspection PhpMissingDocCommentInspection |
||
| 143 | * @inheritdoc |
||
| 144 | */ |
||
| 145 | 1 | public function getUnix($format = false) |
|
| 146 | { |
||
| 155 | } |
||
| 156 | } |
||
| 157 |
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.