| Total Complexity | 68 |
| Total Lines | 355 |
| Duplicated Lines | 0 % |
| Coverage | 97.2% |
| Changes | 0 | ||
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.
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 |
||
| 32 | class Date |
||
| 33 | { |
||
| 34 | |||
| 35 | use AffixesTrait, |
||
| 36 | DisplayTrait, |
||
| 37 | FormattingTrait, |
||
| 38 | TextCaseTrait; |
||
| 39 | |||
| 40 | // bitmask: ymd |
||
| 41 | const DATE_RANGE_STATE_NONE = 0; // 000 |
||
| 42 | const DATE_RANGE_STATE_DAY = 1; // 001 |
||
| 43 | const DATE_RANGE_STATE_MONTH = 2; // 010 |
||
| 44 | const DATE_RANGE_STATE_MONTHDAY = 3; // 011 |
||
| 45 | const DATE_RANGE_STATE_YEAR = 4; // 100 |
||
| 46 | const DATE_RANGE_STATE_YEARDAY = 5; // 101 |
||
| 47 | const DATE_RANGE_STATE_YEARMONTH = 6; // 110 |
||
| 48 | const DATE_RANGE_STATE_YEARMONTHDAY = 7; // 111 |
||
| 49 | |||
| 50 | private static $localizedDateFormats = [ |
||
| 51 | 'numeric', |
||
| 52 | 'text' |
||
| 53 | ]; |
||
| 54 | |||
| 55 | /** |
||
|
1 ignored issue
–
show
|
|||
| 56 | * @var ArrayList |
||
| 57 | */ |
||
| 58 | private $dateParts; |
||
| 59 | |||
| 60 | /** |
||
|
1 ignored issue
–
show
|
|||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | private $form = ""; |
||
| 64 | |||
| 65 | /** |
||
|
1 ignored issue
–
show
|
|||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $variable = ""; |
||
| 69 | |||
| 70 | /** |
||
|
1 ignored issue
–
show
|
|||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | private $datePartsAttribute = ""; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Date constructor. |
||
| 77 | * @param SimpleXMLElement $node |
||
|
3 ignored issues
–
show
|
|||
| 78 | * @throws InvalidStylesheetException |
||
|
1 ignored issue
–
show
|
|||
| 79 | */ |
||
| 80 | 67 | public function __construct(SimpleXMLElement $node) |
|
| 81 | { |
||
| 82 | 67 | $this->dateParts = new ArrayList(); |
|
| 83 | |||
| 84 | /** @var SimpleXMLElement $attribute */ |
||
|
3 ignored issues
–
show
|
|||
| 85 | 67 | foreach ($node->attributes() as $attribute) { |
|
| 86 | 67 | switch ($attribute->getName()) { |
|
| 87 | 67 | case 'form': |
|
| 88 | 34 | $this->form = (string) $attribute; |
|
| 89 | 34 | break; |
|
| 90 | 67 | case 'variable': |
|
| 91 | 67 | $this->variable = (string) $attribute; |
|
| 92 | 67 | break; |
|
| 93 | 40 | case 'date-parts': |
|
| 94 | 67 | $this->datePartsAttribute = (string) $attribute; |
|
| 95 | } |
||
| 96 | } |
||
| 97 | /** @var SimpleXMLElement $child */ |
||
|
3 ignored issues
–
show
|
|||
| 98 | 67 | foreach ($node->children() as $child) { |
|
| 99 | 51 | if ($child->getName() === "date-part") { |
|
| 100 | 51 | $datePartName = (string) $child->attributes()["name"]; |
|
| 101 | 51 | $this->dateParts->set($this->form . "-" . $datePartName, Util\Factory::create($child)); |
|
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | 67 | $this->initAffixesAttributes($node); |
|
| 106 | 67 | $this->initDisplayAttributes($node); |
|
| 107 | 67 | $this->initFormattingAttributes($node); |
|
| 108 | 67 | $this->initTextCaseAttributes($node); |
|
| 109 | 67 | } |
|
| 110 | |||
| 111 | /** |
||
|
1 ignored issue
–
show
|
|||
| 112 | * @param $data |
||
|
2 ignored issues
–
show
|
|||
| 113 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 114 | * @throws InvalidStylesheetException |
||
|
1 ignored issue
–
show
|
|||
| 115 | * @throws Exception |
||
|
1 ignored issue
–
show
|
|||
| 116 | */ |
||
| 117 | 60 | public function render($data) |
|
| 118 | { |
||
| 119 | 60 | $ret = ""; |
|
| 120 | 60 | $var = null; |
|
| 121 | 60 | if (isset($data->{$this->variable})) { |
|
| 122 | 56 | $var = $data->{$this->variable}; |
|
| 123 | } else { |
||
| 124 | 6 | return ""; |
|
| 125 | } |
||
| 126 | |||
| 127 | try { |
||
| 128 | 56 | $this->prepareDatePartsInVariable($data, $var); |
|
| 129 | 2 | } catch (CiteProcException $e) { |
|
| 130 | 2 | if (isset($data->{$this->variable}->{'raw'}) && |
|
| 131 | 1 | !preg_match("/(\p{L}+)\s?([\-\-&,])\s?(\p{L}+)/u", $data->{$this->variable}->{'raw'})) { |
|
| 132 | 1 | return $this->addAffixes($this->format($this->applyTextCase($data->{$this->variable}->{'raw'}))); |
|
| 133 | } else { |
||
| 134 | 1 | if (isset($data->{$this->variable}->{'string-literal'})) { |
|
| 135 | 1 | return $this->addAffixes($this->format($this->applyTextCase($data->{$this->variable}->{'string-literal'}))); |
|
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | 55 | $form = $this->form; |
|
| 141 | 55 | $dateParts = !empty($this->datePartsAttribute) ? explode("-", $this->datePartsAttribute) : []; |
|
| 142 | 55 | $this->prepareDatePartsChildren($dateParts, $form); |
|
| 143 | |||
| 144 | |||
| 145 | // No date-parts in date-part attribute defined, take into account that the defined date-part children will be used. |
||
| 146 | 55 | if (empty($this->datePartsAttribute) && $this->dateParts->count() > 0) { |
|
| 147 | /** @var DatePart $part */ |
||
|
3 ignored issues
–
show
|
|||
| 148 | 43 | foreach ($this->dateParts as $part) { |
|
| 149 | 43 | $dateParts[] = $part->getName(); |
|
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | /* cs:date may have one or more cs:date-part child elements (see Date-part). The attributes set on |
||
| 154 | these elements override those specified for the localized date formats (e.g. to get abbreviated months for all |
||
| 155 | locales, the form attribute on the month-cs:date-part element can be set to “short”). These cs:date-part |
||
| 156 | elements do not affect which, or in what order, date parts are rendered. Affixes, which are very |
||
| 157 | locale-specific, are not allowed on these cs:date-part elements. */ |
||
| 158 | |||
| 159 | 55 | if ($this->dateParts->count() > 0) { |
|
| 160 | 54 | if (!isset($var->{'date-parts'})) { // ignore empty date-parts |
|
| 161 | return ""; |
||
| 162 | } |
||
| 163 | |||
| 164 | 54 | if (count($data->{$this->variable}->{'date-parts'}) === 1) { |
|
| 165 | 52 | $data_ = $this->createDateTime($data->{$this->variable}->{'date-parts'}); |
|
| 166 | 52 | $ret .= $this->iterateAndRenderDateParts($dateParts, $data_); |
|
| 167 | 2 | } else if (count($var->{'date-parts'}) === 2) { //date range |
|
| 168 | 2 | $data_ = $this->createDateTime($var->{'date-parts'}); |
|
| 169 | 2 | $from = $data_[0]; |
|
| 170 | 2 | $to = $data_[1]; |
|
| 171 | 2 | $interval = $to->diff($from); |
|
| 172 | 2 | $delimiter = ""; |
|
| 173 | 2 | $toRender = 0; |
|
| 174 | 2 | if ($interval->y > 0 && in_array('year', $dateParts)) { |
|
| 175 | 1 | $toRender |= self::DATE_RANGE_STATE_YEAR; |
|
| 176 | 1 | $delimiter = $this->dateParts->get($this->form . "-year")->getRangeDelimiter(); |
|
| 177 | } |
||
| 178 | 2 | if ($interval->m > 0 && $from->getMonth() - $to->getMonth() !== 0 && in_array('month', $dateParts)) { |
|
| 179 | 1 | $toRender |= self::DATE_RANGE_STATE_MONTH; |
|
| 180 | 1 | $delimiter = $this->dateParts->get($this->form . "-month")->getRangeDelimiter(); |
|
| 181 | } |
||
| 182 | 2 | if ($interval->d > 0 && $from->getDay() - $to->getDay() !== 0 && in_array('day', $dateParts)) { |
|
| 183 | 1 | $toRender |= self::DATE_RANGE_STATE_DAY; |
|
| 184 | 1 | $delimiter = $this->dateParts->get($this->form . "-day")->getRangeDelimiter(); |
|
| 185 | } |
||
| 186 | 2 | if ($toRender === self::DATE_RANGE_STATE_NONE) { |
|
| 187 | 1 | $ret .= $this->iterateAndRenderDateParts($dateParts, $data_); |
|
| 188 | } else { |
||
| 189 | 1 | $ret .= $this->renderDateRange($toRender, $from, $to, $delimiter); |
|
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | 54 | if (isset($var->raw) && preg_match("/(\p{L}+)\s?([\-\-&,])\s?(\p{L}+)/u", $var->raw, $matches)) { |
|
| 194 | return $matches[1] . $matches[2] . $matches[3]; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | // fallback: |
||
| 198 | // When there are no dateParts children, but date-parts attribute in date |
||
| 199 | // render numeric |
||
| 200 | 1 | else if (!empty($this->datePartsAttribute)) { |
|
| 201 | 1 | $data = $this->createDateTime($var->{'date-parts'}); |
|
| 202 | 1 | $ret = $this->renderNumeric($data[0]); |
|
| 203 | } |
||
| 204 | |||
| 205 | 55 | return !empty($ret) ? $this->addAffixes($this->format($this->applyTextCase($ret))) : ""; |
|
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
|
1 ignored issue
–
show
|
|||
| 209 | * @param array $dates |
||
|
2 ignored issues
–
show
|
|||
| 210 | * @return array |
||
|
1 ignored issue
–
show
|
|||
| 211 | * @throws Exception |
||
|
1 ignored issue
–
show
|
|||
| 212 | */ |
||
| 213 | 55 | private function createDateTime($dates) |
|
|
1 ignored issue
–
show
|
|||
| 214 | { |
||
| 215 | 55 | $data = []; |
|
| 216 | 55 | foreach ($dates as $date) { |
|
| 217 | 55 | $date = $this->cleanDate($date); |
|
| 218 | 55 | if ($date[0] < 1000) { |
|
| 219 | 1 | $dateTime = new DateTime(0, 0, 0); |
|
| 220 | 1 | $dateTime->setDay(0)->setMonth(0)->setYear(0); |
|
| 221 | 1 | $data[] = $dateTime; |
|
| 222 | } |
||
| 223 | 55 | $dateTime = new DateTime($date[0], array_key_exists(1, $date) ? $date[1] : 1, array_key_exists(2, $date) ? $date[2] : 1); |
|
| 224 | 55 | if (!array_key_exists(1, $date)) { |
|
| 225 | 28 | $dateTime->setMonth(0); |
|
| 226 | } |
||
| 227 | 55 | if (!array_key_exists(2, $date)) { |
|
| 228 | 33 | $dateTime->setDay(0); |
|
| 229 | } |
||
| 230 | 55 | $data[] = $dateTime; |
|
| 231 | } |
||
| 232 | |||
| 233 | 55 | return $data; |
|
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
|
1 ignored issue
–
show
|
|||
| 237 | * @param int $toRender |
||
|
3 ignored issues
–
show
|
|||
| 238 | * @param DateTime $from |
||
|
2 ignored issues
–
show
|
|||
| 239 | * @param DateTime $to |
||
|
2 ignored issues
–
show
|
|||
| 240 | * @param $delimiter |
||
|
2 ignored issues
–
show
|
|||
| 241 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 242 | */ |
||
| 243 | 1 | private function renderDateRange($toRender, DateTime $from, DateTime $to, $delimiter) |
|
|
1 ignored issue
–
show
|
|||
| 244 | { |
||
| 245 | 1 | $datePartRenderer = DateRangeRenderer::factory($this, $toRender); |
|
| 246 | 1 | return $datePartRenderer->parseDateRange($this->dateParts, $from, $to, $delimiter); |
|
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
|
1 ignored issue
–
show
|
|||
| 250 | * @param string $format |
||
|
2 ignored issues
–
show
|
|||
| 251 | * @return bool |
||
|
1 ignored issue
–
show
|
|||
| 252 | */ |
||
| 253 | 16 | private function hasDatePartsFromLocales($format) |
|
|
1 ignored issue
–
show
|
|||
| 254 | { |
||
| 255 | 16 | $dateXml = CiteProc::getContext()->getLocale()->getDateXml(); |
|
| 256 | 16 | return !empty($dateXml[$format]); |
|
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
|
1 ignored issue
–
show
|
|||
| 260 | * @param string $format |
||
|
2 ignored issues
–
show
|
|||
| 261 | * @return array |
||
|
1 ignored issue
–
show
|
|||
| 262 | */ |
||
| 263 | 16 | private function getDatePartsFromLocales($format) |
|
|
1 ignored issue
–
show
|
|||
| 264 | { |
||
| 265 | 16 | $ret = []; |
|
| 266 | // date parts from locales |
||
| 267 | 16 | $dateFromLocale_ = CiteProc::getContext()->getLocale()->getDateXml(); |
|
| 268 | 16 | $dateFromLocale = $dateFromLocale_[$format]; |
|
| 269 | |||
| 270 | // no custom date parts within the date element (this)? |
||
| 271 | 16 | if (!empty($dateFromLocale)) { |
|
| 272 | |||
| 273 | 16 | $dateForm = array_filter(is_array($dateFromLocale) ? $dateFromLocale : [$dateFromLocale], function($element) use ($format) { |
|
| 274 | /** @var SimpleXMLElement $element */ |
||
|
3 ignored issues
–
show
|
|||
| 275 | 16 | $dateForm = (string) $element->attributes()["form"]; |
|
| 276 | 16 | return $dateForm === $format; |
|
| 277 | 16 | }); |
|
| 278 | |||
| 279 | //has dateForm from locale children (date-part elements)? |
||
| 280 | 16 | $localeDate = array_pop($dateForm); |
|
| 281 | |||
| 282 | 16 | if ($localeDate instanceof SimpleXMLElement && $localeDate->count() > 0) { |
|
| 283 | 16 | foreach ($localeDate as $child) { |
|
| 284 | 16 | $ret[] = $child; |
|
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | 16 | return $ret; |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
|
1 ignored issue
–
show
|
|||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | 28 | public function getVariable() |
|
| 295 | { |
||
| 296 | 28 | return $this->variable; |
|
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
|
1 ignored issue
–
show
|
|||
| 300 | * @param $data |
||
|
2 ignored issues
–
show
|
|||
| 301 | * @param $var |
||
|
2 ignored issues
–
show
|
|||
| 302 | * @throws CiteProcException |
||
|
1 ignored issue
–
show
|
|||
| 303 | */ |
||
| 304 | 56 | private function prepareDatePartsInVariable($data, $var) |
|
|
1 ignored issue
–
show
|
|||
| 305 | { |
||
| 306 | 56 | if (!isset($data->{$this->variable}->{'date-parts'}) || empty($data->{$this->variable}->{'date-parts'})) { |
|
| 307 | 7 | if (isset($data->{$this->variable}->raw) && !empty($data->{$this->variable}->raw)) { |
|
| 308 | // try to parse date parts from "raw" attribute |
||
| 309 | 6 | $var->{'date-parts'} = Util\DateHelper::parseDateParts($data->{$this->variable}); |
|
| 310 | } else { |
||
| 311 | 1 | throw new CiteProcException("No valid date format"); |
|
| 312 | } |
||
| 313 | } |
||
| 314 | 55 | } |
|
| 315 | |||
| 316 | /** |
||
|
1 ignored issue
–
show
|
|||
| 317 | * @param $dateParts |
||
|
2 ignored issues
–
show
|
|||
| 318 | * @param string $form |
||
|
2 ignored issues
–
show
|
|||
| 319 | * @throws InvalidStylesheetException |
||
|
1 ignored issue
–
show
|
|||
| 320 | */ |
||
| 321 | 55 | private function prepareDatePartsChildren($dateParts, $form) |
|
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | 55 | } |
|
| 349 | |||
| 350 | |||
| 351 | 1 | private function renderNumeric(DateTime $date) |
|
|
1 ignored issue
–
show
|
|||
| 352 | { |
||
| 353 | 1 | return $date->renderNumeric(); |
|
| 354 | } |
||
| 355 | |||
| 356 | 12 | public function getForm() |
|
| 357 | { |
||
| 358 | 12 | return $this->form; |
|
| 359 | } |
||
| 360 | |||
| 361 | 55 | private function cleanDate($date) |
|
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
|
1 ignored issue
–
show
|
|||
| 371 | * @param array $dateParts |
||
|
2 ignored issues
–
show
|
|||
| 372 | * @param array $data_ |
||
|
2 ignored issues
–
show
|
|||
| 373 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 374 | */ |
||
| 375 | 53 | private function iterateAndRenderDateParts(array $dateParts, array $data_) |
|
| 389 |