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:
Complex classes like Date often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Date, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Date |
||
| 29 | { |
||
| 30 | |||
| 31 | use AffixesTrait, |
||
| 32 | DisplayTrait, |
||
| 33 | FormattingTrait, |
||
| 34 | TextCaseTrait; |
||
| 35 | |||
| 36 | // ymd |
||
| 37 | const DATE_RANGE_STATE_NONE = 0; // 000 |
||
| 38 | const DATE_RANGE_STATE_DAY = 1; // 001 |
||
| 39 | const DATE_RANGE_STATE_MONTH = 2; // 010 |
||
| 40 | const DATE_RANGE_STATE_MONTHDAY = 3; // 011 |
||
| 41 | const DATE_RANGE_STATE_YEAR = 4; // 100 |
||
| 42 | const DATE_RANGE_STATE_YEARDAY = 5; // 101 |
||
| 43 | const DATE_RANGE_STATE_YEARMONTH = 6; // 110 |
||
| 44 | const DATE_RANGE_STATE_YEARMONTHDAY = 7; // 111 |
||
| 45 | |||
| 46 | private static $localizedDateFormats = [ |
||
| 47 | 'numeric', |
||
| 48 | 'text' |
||
| 49 | ]; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var ArrayList |
||
| 53 | */ |
||
| 54 | private $dateParts; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $form = ""; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | private $variable = ""; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $datePartsAttribute = ""; |
||
| 70 | |||
| 71 | public function __construct(\SimpleXMLElement $node) |
||
| 72 | { |
||
| 73 | $this->dateParts = new ArrayList(); |
||
| 74 | |||
| 75 | /** @var \SimpleXMLElement $attribute */ |
||
| 76 | View Code Duplication | foreach ($node->attributes() as $attribute) { |
|
|
|
|||
| 77 | switch ($attribute->getName()) { |
||
| 78 | case 'form': |
||
| 79 | $this->form = (string) $attribute; |
||
| 80 | break; |
||
| 81 | case 'variable': |
||
| 82 | $this->variable = (string) $attribute; |
||
| 83 | break; |
||
| 84 | case 'date-parts': |
||
| 85 | $this->datePartsAttribute = (string) $attribute; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | /** @var \SimpleXMLElement $child */ |
||
| 89 | foreach ($node->children() as $child) { |
||
| 90 | if ($child->getName() === "date-part") { |
||
| 91 | $datePartName = (string) $child->attributes()["name"]; |
||
| 92 | $this->dateParts->set($this->form . "-" . $datePartName, Util\Factory::create($child)); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | $this->initAffixesAttributes($node); |
||
| 97 | $this->initDisplayAttributes($node); |
||
| 98 | $this->initFormattingAttributes($node); |
||
| 99 | $this->initTextCaseAttributes($node); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param $data |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function render($data) |
||
| 107 | { |
||
| 108 | $ret = ""; |
||
| 109 | $var = null; |
||
| 110 | if (isset($data->{$this->variable})) { |
||
| 111 | $var = $data->{$this->variable}; |
||
| 112 | } else { |
||
| 113 | return ""; |
||
| 114 | } |
||
| 115 | |||
| 116 | try { |
||
| 117 | $this->prepareDatePartsInVariable($data, $var); |
||
| 118 | } catch (CiteProcException $e) { |
||
| 119 | if (!preg_match("/(\p{L}+)\s?([\-\-\&,])\s?(\p{L}+)/u", $data->{$this->variable}->raw)) { |
||
| 120 | return $this->addAffixes($this->format($this->applyTextCase($data->{$this->variable}->raw))); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | $form = $this->form; |
||
| 125 | $dateParts = !empty($this->datePartsAttribute) ? explode("-", $this->datePartsAttribute) : []; |
||
| 126 | $this->prepareDatePartsChildren($dateParts, $form); |
||
| 127 | |||
| 128 | |||
| 129 | // No date-parts in date-part attribute defined, take into account that the defined date-part children will be used. |
||
| 130 | if (empty($this->datePartsAttribute) && $this->dateParts->count() > 0) { |
||
| 131 | /** @var DatePart $part */ |
||
| 132 | foreach ($this->dateParts as $part) { |
||
| 133 | $dateParts[] = $part->getName(); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /* cs:date may have one or more cs:date-part child elements (see Date-part). The attributes set on |
||
| 138 | these elements override those specified for the localized date formats (e.g. to get abbreviated months for all |
||
| 139 | locales, the form attribute on the month-cs:date-part element can be set to “short”). These cs:date-part |
||
| 140 | elements do not affect which, or in what order, date parts are rendered. Affixes, which are very |
||
| 141 | locale-specific, are not allowed on these cs:date-part elements. */ |
||
| 142 | |||
| 143 | if ($this->dateParts->count() > 0) { |
||
| 144 | |||
| 145 | /* if (isset($var->raw) && !preg_match("/(\p{L}+)\s?([\-\-\&,])\s?(\p{L}+)/u", $var->raw) && $this->dateParts->count() > 0) { |
||
| 146 | //$var->{"date-parts"} = []; |
||
| 147 | } else*/ |
||
| 148 | if (!isset($var->{'date-parts'})) { // ignore empty date-parts |
||
| 149 | return ""; |
||
| 150 | } |
||
| 151 | |||
| 152 | if (count($data->{$this->variable}->{'date-parts'}) === 1) { |
||
| 153 | $data_ = $this->createDateTime($data->{$this->variable}->{'date-parts'}); |
||
| 154 | /** @var DatePart $datePart */ |
||
| 155 | foreach ($this->dateParts as $key => $datePart) { |
||
| 156 | list($f, $p) = explode("-", $key); |
||
| 157 | if (in_array($p, $dateParts)) { |
||
| 158 | $ret .= $datePart->render($data_[0], $this); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } else if (count($var->{'date-parts'}) === 2) { //date range |
||
| 162 | $data_ = $this->createDateTime($var->{'date-parts'}); |
||
| 163 | $from = $data_[0]; |
||
| 164 | $to = $data_[1]; |
||
| 165 | $interval = $to->diff($from); |
||
| 166 | $delim = ""; |
||
| 167 | $toRender = 0; |
||
| 168 | if ($interval->y > 0) { |
||
| 169 | $toRender |= self::DATE_RANGE_STATE_YEAR; |
||
| 170 | $delim = $this->dateParts->get($this->form . "-year")->getRangeDelimiter(); |
||
| 171 | } |
||
| 172 | if ($interval->m > 0 && $from->getMonth() - $to->getMonth() !== 0) { |
||
| 173 | $toRender |= self::DATE_RANGE_STATE_MONTH; |
||
| 174 | $delim = $this->dateParts->get($this->form . "-month")->getRangeDelimiter(); |
||
| 175 | } |
||
| 176 | if ($interval->d > 0 && $from->getDay() - $to->getDay() !== 0) { |
||
| 177 | $toRender |= self::DATE_RANGE_STATE_DAY; |
||
| 178 | $delim = $this->dateParts->get($this->form . "-day")->getRangeDelimiter(); |
||
| 179 | } |
||
| 180 | |||
| 181 | $ret = $this->renderDateRange($toRender, $from, $to, $delim); |
||
| 182 | } |
||
| 183 | |||
| 184 | if (isset($var->raw) && preg_match("/(\p{L}+)\s?([\-\-\&,])\s?(\p{L}+)/u", $var->raw, $matches)) { |
||
| 185 | return $matches[1] . $matches[2] . $matches[3]; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | // fallback: |
||
| 189 | // When there are no dateParts children, but date-parts attribute in date |
||
| 190 | // render numeric |
||
| 191 | else if (!empty($this->datePartsAttribute)) { |
||
| 192 | $data = $this->createDateTime($var->{'date-parts'}); |
||
| 193 | $ret = $this->renderNumeric($data[0]); |
||
| 194 | } |
||
| 195 | |||
| 196 | return !empty($ret) ? $this->addAffixes($this->format($this->applyTextCase($ret))) : ""; |
||
| 197 | } |
||
| 198 | |||
| 199 | private function createDateTime($dates) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param integer $differentParts |
||
| 224 | * @param DateTime $from |
||
| 225 | * @param DateTime $to |
||
| 226 | * @param $delim |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | private function renderDateRange($differentParts, DateTime $from, DateTime $to, $delim) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param $datePart |
||
| 376 | * @param DateTime $from |
||
| 377 | * @param DateTime $to |
||
| 378 | * @param $delim |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | protected function renderOneRangePart(DatePart $datePart, $from, $to, $delim) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param string $format |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | private function hasDatePartsFromLocales($format) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param string $format |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | private function getDatePartsFromLocales($format) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | public function getVariable() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param $data |
||
| 442 | * @param $var |
||
| 443 | * @throws CiteProcException |
||
| 444 | */ |
||
| 445 | private function prepareDatePartsInVariable($data, $var) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $form |
||
| 466 | */ |
||
| 467 | private function prepareDatePartsChildren($dateParts, $form) |
||
| 495 | |||
| 496 | |||
| 497 | private function renderNumeric(DateTime $date) |
||
| 501 | |||
| 502 | public function getForm() |
||
| 506 | |||
| 507 | private function cleanDate($date) |
||
| 515 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.