| Total Complexity | 40 |
| Total Lines | 202 |
| Duplicated Lines | 0 % |
| Coverage | 86.05% |
| Changes | 0 | ||
Complex classes like DatePart 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 DatePart, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class DatePart |
||
| 28 | { |
||
| 29 | |||
| 30 | const DEFAULT_RANGE_DELIMITER = "–"; |
||
| 31 | |||
| 32 | use FormattingTrait, |
||
| 33 | AffixesTrait, |
||
| 34 | TextCaseTrait, |
||
| 35 | RangeDelimiterTrait; |
||
| 36 | |||
| 37 | /** |
||
|
1 ignored issue
–
show
|
|||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $name; |
||
| 41 | |||
| 42 | /** |
||
|
1 ignored issue
–
show
|
|||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $form; |
||
| 46 | |||
| 47 | /** |
||
|
1 ignored issue
–
show
|
|||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $rangeDelimiter; |
||
| 51 | |||
| 52 | /** |
||
|
1 ignored issue
–
show
|
|||
| 53 | * @var Date |
||
| 54 | */ |
||
| 55 | private $parent; |
||
| 56 | |||
| 57 | 55 | public function __construct(\SimpleXMLElement $node) |
|
| 58 | { |
||
| 59 | 55 | foreach ($node->attributes() as $attribute) { |
|
| 60 | 55 | if ("name" === $attribute->getName()) { |
|
| 61 | 55 | $this->name = (string) $attribute; |
|
| 62 | } |
||
| 63 | 55 | if ("form" === $attribute->getName()) { |
|
| 64 | 23 | $this->form = (string) $attribute; |
|
| 65 | } |
||
| 66 | 55 | if ("range-delimiter" === $attribute->getName()) { |
|
| 67 | 1 | $this->rangeDelimiter = (string) $attribute; |
|
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | 55 | if (empty($this->rangeDelimiter)) { |
|
| 72 | 55 | $this->rangeDelimiter = self::DEFAULT_RANGE_DELIMITER; |
|
| 73 | } |
||
| 74 | |||
| 75 | 55 | $this->initFormattingAttributes($node); |
|
| 76 | 55 | $this->initAffixesAttributes($node); |
|
| 77 | 55 | $this->initTextCaseAttributes($node); |
|
| 78 | 55 | } |
|
| 79 | |||
| 80 | |||
| 81 | /** |
||
|
1 ignored issue
–
show
|
|||
| 82 | * @param DateTime $date |
||
|
2 ignored issues
–
show
|
|||
| 83 | * @param Date $parent |
||
|
3 ignored issues
–
show
|
|||
| 84 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 85 | */ |
||
| 86 | 47 | public function render(DateTime $date, Date $parent) |
|
| 87 | { |
||
| 88 | 47 | $this->parent = $parent; //set parent |
|
| 89 | 47 | $text = $this->renderWithoutAffixes($date); |
|
| 90 | 47 | return !empty($text) ? $this->addAffixes($text) : ""; |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
|
1 ignored issue
–
show
|
|||
| 94 | * @param $date |
||
|
1 ignored issue
–
show
|
|||
| 95 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 96 | */ |
||
| 97 | 47 | public function renderWithoutAffixes(DateTime $date, Date $parent = null) |
|
| 98 | { |
||
| 99 | 47 | if (!is_null($parent)) { |
|
| 100 | 1 | $this->parent = $parent; |
|
| 101 | } |
||
| 102 | 47 | $text = ""; |
|
| 103 | 47 | switch ($this->name) { |
|
| 104 | 47 | case 'year': |
|
| 105 | 47 | $text = $this->renderYear($date); |
|
| 106 | 47 | break; |
|
| 107 | 15 | case 'month': |
|
| 108 | 15 | $text = $this->renderMonth($date); |
|
| 109 | 15 | break; |
|
| 110 | 14 | case 'day': |
|
| 111 | 14 | $text = $this->renderDay($date); |
|
| 112 | } |
||
| 113 | |||
| 114 | 47 | return !empty($text) ? $this->format($this->applyTextCase($text)) : ""; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
|
1 ignored issue
–
show
|
|||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getForm() |
||
| 121 | { |
||
| 122 | return $this->form; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
|
1 ignored issue
–
show
|
|||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | 36 | public function getName() |
|
| 129 | { |
||
| 130 | 36 | return $this->name; |
|
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
|
1 ignored issue
–
show
|
|||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | 1 | public function getRangeDelimiter() |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
|
1 ignored issue
–
show
|
|||
| 142 | * @param DateTime $date |
||
|
2 ignored issues
–
show
|
|||
| 143 | * @return string|int |
||
|
1 ignored issue
–
show
|
|||
| 144 | */ |
||
| 145 | 47 | protected function renderYear(DateTime $date) |
|
| 146 | { |
||
| 147 | 47 | $text = $date->getYear(); |
|
| 148 | 47 | if ($text > 0 && $text < 1000) { |
|
| 149 | $text = $text . CiteProc::getContext()->getLocale()->filter("terms", "ad")->single; |
||
| 150 | return $text; |
||
| 151 | 47 | } elseif ($text < 0) { |
|
| 152 | $text = $text * -1; |
||
| 153 | $text = $text . CiteProc::getContext()->getLocale()->filter("terms", "bc")->single; |
||
| 154 | return $text; |
||
| 155 | } |
||
| 156 | 47 | return $text; |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
|
1 ignored issue
–
show
|
|||
| 160 | * @param DateTime $date |
||
|
2 ignored issues
–
show
|
|||
| 161 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 162 | */ |
||
| 163 | 15 | protected function renderMonth(DateTime $date) |
|
| 164 | { |
||
| 165 | 15 | if ($date->getMonth() < 1 || $date->getMonth() > 12) { |
|
| 166 | return ""; |
||
| 167 | } |
||
| 168 | |||
| 169 | 15 | $text = $date->getMonth(); |
|
| 170 | |||
| 171 | 15 | $form = !empty($this->form) ? $this->form : "long"; |
|
| 172 | switch ($form) { |
||
| 173 | 15 | case 'numeric': |
|
| 174 | 1 | break; |
|
| 175 | 15 | case 'numeric-leading-zeros': |
|
| 176 | 3 | $text = sprintf("%02d", $text); |
|
| 177 | 3 | break; |
|
| 178 | 13 | case 'short': |
|
| 179 | 3 | $text = $this->monthFromLocale($text, $form); |
|
| 180 | 3 | break; |
|
| 181 | 11 | case 'long': |
|
| 182 | default: |
||
| 183 | 11 | $text = $this->monthFromLocale($text, $form); |
|
| 184 | 11 | break; |
|
| 185 | } |
||
| 186 | 15 | return $text; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
|
1 ignored issue
–
show
|
|||
| 190 | * @param DateTime $date |
||
|
2 ignored issues
–
show
|
|||
| 191 | * @return int|string |
||
|
1 ignored issue
–
show
|
|||
| 192 | */ |
||
| 193 | 14 | protected function renderDay(DateTime $date) |
|
| 194 | { |
||
| 195 | 14 | if ($date->getDay() < 1 || $date->getDay() > 31) { |
|
| 196 | 3 | return ""; |
|
| 197 | } |
||
| 198 | |||
| 199 | 13 | $text = $date->getDay(); |
|
| 200 | 13 | $form = !empty($this->form) ? $this->form : $this->parent->getForm(); |
|
| 201 | switch ($form) { |
||
| 202 | 13 | case 'numeric': |
|
| 203 | 1 | break; |
|
| 204 | 13 | case 'numeric-leading-zeros': |
|
| 205 | 3 | $text = sprintf("%02d", $text); |
|
| 206 | 3 | break; |
|
| 207 | 11 | case 'ordinal': |
|
| 208 | $limitDayOrdinals = CiteProc::getContext()->getLocale()->filter("options", "limit-day-ordinals-to-day-1"); |
||
| 209 | if (!$limitDayOrdinals || Layout::getNumberOfCitedItems() <= 1) { |
||
| 210 | $text = Number::ordinal($text); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | 13 | return $text; |
|
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
|
1 ignored issue
–
show
|
|||
| 217 | * @param $text |
||
|
1 ignored issue
–
show
|
|||
| 218 | * @param $form |
||
|
1 ignored issue
–
show
|
|||
| 219 | * @return mixed |
||
|
1 ignored issue
–
show
|
|||
| 220 | */ |
||
| 221 | 13 | protected function monthFromLocale($text, $form) |
|
| 229 | } |
||
| 230 | |||
| 231 | } |