| Conditions | 27 |
| Paths | 475 |
| Total Lines | 89 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 48 |
| CRAP Score | 27.0466 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 389 |