| Conditions | 34 | 
| Paths | 1173 | 
| Total Lines | 123 | 
| Code Lines | 65 | 
| Lines | 0 | 
| Ratio | 0 % | 
| 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 | ||
| 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 | |||
| 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.