| Conditions | 26 | 
| Paths | 138 | 
| Total Lines | 100 | 
| Code Lines | 64 | 
| 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 | ||
| 86 | public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string | ||
| 87 |     { | ||
| 88 | $calendar_service = new CalendarService(); | ||
| 89 | |||
| 90 | $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); | ||
| 91 | $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); | ||
| 92 | $calendar = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR); | ||
| 93 | |||
| 94 | extract($config, EXTR_OVERWRITE); | ||
| 95 | |||
| 96 | $jewish_calendar = new JewishCalendar(); | ||
| 97 | $startjd = Registry::timestampFactory()->now()->julianDay(); | ||
| 98 | $endjd = Registry::timestampFactory()->now()->addDays($days - 1)->julianDay(); | ||
| 99 | |||
| 100 | // The standard anniversary rules cover most of the Yahrzeit rules, we just | ||
| 101 | // need to handle a few special cases. | ||
| 102 | // Fetch normal anniversaries, with an extra day before/after | ||
| 103 | $yahrzeits = new Collection(); | ||
| 104 |         for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) { | ||
| 105 |             foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) { | ||
| 106 | // Exact hebrew dates only | ||
| 107 | $date = $fact->date(); | ||
| 108 |                 if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) { | ||
| 109 | $jd_yahrtzeit = $jd; | ||
| 110 | // ...then adjust DEAT dates (but not _YART) | ||
| 111 |                     if ($fact->tag() === 'INDI:DEAT') { | ||
| 112 | $today = new JewishDate($jd); | ||
| 113 | $hd = $fact->date()->minimumDate(); | ||
| 114 | $hd1 = new JewishDate($hd); | ||
| 115 | ++$hd1->year; | ||
| 116 | $hd1->setJdFromYmd(); | ||
| 117 | // Special rules. See https://www.hebcal.com/help/anniv.html | ||
| 118 | // Everything else is taken care of by our standard anniversary rules. | ||
| 119 |                         if ($hd->day === 30 && $hd->month === 2 && $hd->year !== 0 && $hd1->daysInMonth() < 30) { | ||
| 120 | // 30 CSH - Last day in CSH | ||
| 121 | $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1; | ||
| 122 |                         } elseif ($hd->day === 30 && $hd->month === 3 && $hd->year !== 0 && $hd1->daysInMonth() < 30) { | ||
| 123 | // 30 KSL - Last day in KSL | ||
| 124 | $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1; | ||
| 125 |                         } elseif ($hd->day === 30 && $hd->month === 6 && $hd->year !== 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) { | ||
| 126 | // 30 ADR - Last day in SHV | ||
| 127 | $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | // Filter adjusted dates to our date range | ||
| 132 |                     if ($jd_yahrtzeit >= $startjd && $jd_yahrtzeit < $startjd + $days) { | ||
| 133 | // upcoming yahrzeit dates | ||
| 134 |                         switch ($calendar) { | ||
| 135 | case 'gregorian': | ||
| 136 | $yahrzeit_calendar_date = new GregorianDate($jd_yahrtzeit); | ||
| 137 | break; | ||
| 138 | case 'jewish': | ||
| 139 | default: | ||
| 140 | $yahrzeit_calendar_date = new JewishDate($jd_yahrtzeit); | ||
| 141 | break; | ||
| 142 | } | ||
| 143 |                         $yahrzeit_date = new Date($yahrzeit_calendar_date->format('%@ %A %O %E')); | ||
| 144 | |||
| 145 | $yahrzeits->add((object) [ | ||
| 146 | 'individual' => $fact->record(), | ||
| 147 | 'fact_date' => $fact->date(), | ||
| 148 | 'fact' => $fact, | ||
| 149 | 'yahrzeit_date' => $yahrzeit_date, | ||
| 150 | ]); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 |         switch ($infoStyle) { | ||
| 157 | case 'list': | ||
| 158 |                 $content = view('modules/yahrzeit/list', [ | ||
| 159 | 'id' => $block_id, | ||
| 160 | 'limit_low' => self::LIMIT_LOW, | ||
| 161 | 'limit_high' => self::LIMIT_HIGH, | ||
| 162 | 'yahrzeits' => $yahrzeits, | ||
| 163 | ]); | ||
| 164 | break; | ||
| 165 | case 'table': | ||
| 166 | default: | ||
| 167 |                 $content = view('modules/yahrzeit/table', [ | ||
| 168 | 'limit_low' => self::LIMIT_LOW, | ||
| 169 | 'limit_high' => self::LIMIT_HIGH, | ||
| 170 | 'yahrzeits' => $yahrzeits, | ||
| 171 | ]); | ||
| 172 | break; | ||
| 173 | } | ||
| 174 | |||
| 175 |         if ($context !== self::CONTEXT_EMBED) { | ||
| 176 |             return view('modules/block-template', [ | ||
| 177 | 'block' => Str::kebab($this->name()), | ||
| 178 | 'id' => $block_id, | ||
| 179 | 'config_url' => $this->configUrl($tree, $context, $block_id), | ||
| 180 | 'title' => $this->title(), | ||
| 181 | 'content' => $content, | ||
| 182 | ]); | ||
| 183 | } | ||
| 184 | |||
| 185 | return $content; | ||
| 186 | } | ||
| 275 |