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) |
||
| 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 | if (!isset($data->{$this->variable}->{'date-parts'}) || empty($data->{$this->variable}->{'date-parts'})) { |
||
| 117 | if (isset($data->{$this->variable}->raw) && !empty($data->{$this->variable}->raw)) { |
||
| 118 | try { |
||
| 119 | // try to parse date parts from "raw" attribute |
||
| 120 | $var->{'date-parts'} = Util\Date::parseDateParts($data->{$this->variable}); |
||
| 121 | } catch (CiteProcException $e) { |
||
| 122 | if (!preg_match("/(\p{L}+)\s?([\-\-\&,])\s?(\p{L}+)/u", $data->{$this->variable}->raw)) { |
||
| 123 | return $this->addAffixes($this->format($this->applyTextCase($data->{$this->variable}->raw))); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } else { |
||
| 127 | return ""; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | $form = $this->form; |
||
| 132 | $dateParts = explode("-", $this->datePartsAttribute); |
||
| 133 | |||
| 134 | /* Localized date formats are selected with the optional form attribute, which must set to either “numeric” |
||
| 135 | (for fully numeric formats, e.g. “12-15-2005”), or “text” (for formats with a non-numeric month, e.g. |
||
| 136 | “December 15, 2005”). Localized date formats can be customized in two ways. First, the date-parts attribute may |
||
| 137 | be used to show fewer date parts. The possible values are: |
||
| 138 | - “year-month-day” - (default), renders the year, month and day |
||
| 139 | - “year-month” - renders the year and month |
||
| 140 | - “year” - renders the year */ |
||
| 141 | |||
| 142 | if ($this->dateParts->count() < 1 && in_array($form, self::$localizedDateFormats)) { |
||
| 143 | if ($this->hasDatePartsFromLocales($form)) { |
||
| 144 | $datePartsFromLocales = $this->getDatePartsFromLocales($form); |
||
| 145 | array_filter($datePartsFromLocales, function (\SimpleXMLElement $item) use ($dateParts) { |
||
| 146 | return in_array($item["name"], $dateParts); |
||
| 147 | }); |
||
| 148 | |||
| 149 | foreach ($datePartsFromLocales as $datePartNode) { |
||
| 150 | $datePart = $datePartNode["name"]; |
||
| 151 | $this->dateParts->set("$form-$datePart", Util\Factory::create($datePartNode)); |
||
| 152 | } |
||
| 153 | } else { //otherwise create default date parts |
||
| 154 | foreach ($dateParts as $datePart) { |
||
| 155 | $this->dateParts->add("$form-$datePart", new DatePart(new \SimpleXMLElement('<date-part name="' . $datePart . '" form="' . $form . '" />'))); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | |||
| 161 | // No date-parts in date-part attribute defined, take into account that the defined date-part children will be used. |
||
| 162 | if (empty($this->datePartsAttribute) && $this->dateParts->count() > 0) { |
||
| 163 | /** @var DatePart $part */ |
||
| 164 | foreach ($this->dateParts as $part) { |
||
| 165 | $dateParts[] = $part->getName(); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /* cs:date may have one or more cs:date-part child elements (see Date-part). The attributes set on |
||
| 170 | these elements override those specified for the localized date formats (e.g. to get abbreviated months for all |
||
| 171 | locales, the form attribute on the month-cs:date-part element can be set to “short”). These cs:date-part |
||
| 172 | elements do not affect which, or in what order, date parts are rendered. Affixes, which are very |
||
| 173 | locale-specific, are not allowed on these cs:date-part elements. */ |
||
| 174 | |||
| 175 | if ($this->dateParts->count() > 0) { |
||
| 176 | |||
| 177 | if (isset($var->raw) && !preg_match("/(\p{L}+)\s?([\-\-\&,])\s?(\p{L}+)/u", $var->raw) && $this->dateParts->count() > 0) { |
||
| 178 | //$var->{"date-parts"} = []; |
||
| 179 | } else if (!isset($var->{'date-parts'})) { // ignore empty date-parts |
||
| 180 | return ""; |
||
| 181 | } |
||
| 182 | |||
| 183 | if (count($data->{$this->variable}->{'date-parts'}) === 1) { |
||
| 184 | $data_ = $this->createDateTime($data->{$this->variable}->{'date-parts'}); |
||
| 185 | /** @var DatePart $datePart */ |
||
| 186 | foreach ($this->dateParts as $key => $datePart) { |
||
| 187 | list($f, $p) = explode("-", $key); |
||
| 188 | if (in_array($p, $dateParts)) { |
||
| 189 | $ret .= $datePart->render($data_[0], $this); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } else if (count($var->{'date-parts'}) === 2) { //date range |
||
| 193 | $data_ = $this->createDateTime($var->{'date-parts'}); |
||
| 194 | $from = $data_[0]; |
||
| 195 | $to = $data_[1]; |
||
| 196 | $interval = $to->diff($from); |
||
| 197 | $delim = ""; |
||
| 198 | $toRender = 0; |
||
| 199 | if ($interval->y > 0) { |
||
| 200 | $toRender |= self::DATE_RANGE_STATE_YEAR; |
||
| 201 | $delim = $this->dateParts->get($this->form . "-year")->getRangeDelimiter(); |
||
| 202 | } |
||
| 203 | if ($interval->m > 0 && $from->getMonth() - $to->getMonth() !== 0) { |
||
| 204 | $toRender |= self::DATE_RANGE_STATE_MONTH; |
||
| 205 | $delim = $this->dateParts->get($this->form . "-month")->getRangeDelimiter(); |
||
| 206 | } |
||
| 207 | if ($interval->d > 0 && $from->getDay() - $to->getDay() !== 0) { |
||
| 208 | $toRender |= self::DATE_RANGE_STATE_DAY; |
||
| 209 | $delim = $this->dateParts->get($this->form . "-day")->getRangeDelimiter(); |
||
| 210 | } |
||
| 211 | |||
| 212 | $ret = $this->renderDateRange($toRender, $from, $to, $delim); |
||
| 213 | } |
||
| 214 | |||
| 215 | if (isset($var->raw) && preg_match("/(\p{L}+)\s?([\-\-\&,])\s?(\p{L}+)/u", $var->raw, $matches)){ |
||
| 216 | return $matches[1].$matches[2].$matches[3]; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | // fallback: |
||
| 220 | // When there are no dateParts children, but date-parts attribute in date |
||
| 221 | // render numeric |
||
| 222 | else if (!empty($this->datePartsAttribute)) { |
||
| 223 | $data = $this->createDateTime($var->{'date-parts'}); |
||
| 224 | $ret = $this->renderNumeric($data[0]); |
||
| 225 | } |
||
| 226 | |||
| 227 | return !empty($ret) ? $this->addAffixes($this->format($this->applyTextCase($ret))) : ""; |
||
| 228 | } |
||
| 229 | |||
| 230 | private function renderNumeric(DateTime $date) |
||
| 234 | |||
| 235 | public function getForm() |
||
| 239 | |||
| 240 | private function createDateTime($dates) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param $differentParts |
||
| 264 | * @param DateTime $from |
||
| 265 | * @param DateTime $to |
||
| 266 | * @param $delim |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | private function renderDateRange($differentParts, DateTime $from, DateTime $to, $delim) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param $datePart |
||
| 415 | * @param $from |
||
| 416 | * @param $to |
||
| 417 | * @param $delim |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | protected function renderOneRangePart(DatePart $datePart, $from, $to, $delim) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return bool |
||
| 431 | */ |
||
| 432 | private function hasDatePartsFromLocales($format) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | private function getDatePartsFromLocales($format) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | public function getVariable() |
||
| 476 | } |
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.