| Conditions | 2 |
| Paths | 2 |
| Total Lines | 108 |
| Code Lines | 11 |
| 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 |
||
| 99 | private function getElementJS(): string |
||
| 100 | { |
||
| 101 | $localeCode = $this->getLocaleCode(); |
||
| 102 | $id = $this->getAttribute('id'); |
||
| 103 | |||
| 104 | $baseLang = strtolower(explode('-', $localeCode)[0] ?? $localeCode); |
||
| 105 | $altFormat = ($baseLang === 'en') ? 'F d, Y' : 'd F, Y'; |
||
| 106 | |||
| 107 | return "<script> |
||
| 108 | window.addEventListener('load', function () { |
||
| 109 | var container = document.getElementById('{$id}_container'); |
||
| 110 | if (!container) return; |
||
| 111 | |||
| 112 | function cap(s){ return s ? s.charAt(0).toUpperCase() + s.slice(1) : s; } |
||
| 113 | |||
| 114 | // Build a flatpickr locale object using Intl (no external l10n files) |
||
| 115 | function buildFlatpickrLocale(loc) { |
||
| 116 | try { |
||
| 117 | var fmtWeekLong = new Intl.DateTimeFormat(loc, { weekday: 'long' }); |
||
| 118 | var fmtWeekShort = new Intl.DateTimeFormat(loc, { weekday: 'short' }); |
||
| 119 | var fmtMonthLong = new Intl.DateTimeFormat(loc, { month: 'long' }); |
||
| 120 | var fmtMonthShort= new Intl.DateTimeFormat(loc, { month: 'short' }); |
||
| 121 | |||
| 122 | // Weekdays 0..6 starting on Sunday (as flatpickr expects) |
||
| 123 | var sun = new Date(Date.UTC(2020, 0, 5)); |
||
| 124 | var weekdaysLong = [], weekdaysShort = []; |
||
| 125 | for (var i=0;i<7;i++){ |
||
| 126 | var d = new Date(sun); d.setUTCDate(sun.getUTCDate()+i); |
||
| 127 | weekdaysLong.push(cap(fmtWeekLong.format(d))); |
||
| 128 | weekdaysShort.push(cap(fmtWeekShort.format(d))); |
||
| 129 | } |
||
| 130 | |||
| 131 | // Months 0..11 |
||
| 132 | var monthsLong = [], monthsShort = []; |
||
| 133 | for (var m=0;m<12;m++){ |
||
| 134 | var dm = new Date(Date.UTC(2020, m, 1)); |
||
| 135 | monthsLong.push(cap(fmtMonthLong.format(dm))); |
||
| 136 | monthsShort.push(cap(fmtMonthShort.format(dm))); |
||
| 137 | } |
||
| 138 | |||
| 139 | // First day of week (fallback to Monday) |
||
| 140 | var firstDay = 1; |
||
| 141 | try { |
||
| 142 | if (window.Intl && Intl.Locale) { |
||
| 143 | var inf = new Intl.Locale(loc); |
||
| 144 | if (inf.weekInfo && inf.weekInfo.firstDay) { |
||
| 145 | firstDay = (inf.weekInfo.firstDay === 7) ? 0 : inf.weekInfo.firstDay; // 0=Sun |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } catch(e){} |
||
| 149 | |||
| 150 | return { |
||
| 151 | weekdays: { shorthand: weekdaysShort, longhand: weekdaysLong }, |
||
| 152 | months: { shorthand: monthsShort, longhand: monthsLong }, |
||
| 153 | firstDayOfWeek: firstDay, |
||
| 154 | weekAbbreviation: 'Wk', |
||
| 155 | rangeSeparator: ' \u2013 ', |
||
| 156 | time_24hr: true |
||
| 157 | }; |
||
| 158 | } catch(e) { |
||
| 159 | return 'en'; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | function initialize() { |
||
| 164 | try { |
||
| 165 | if (!window.flatpickr) return; |
||
| 166 | |||
| 167 | // If already initialized, destroy before re-init (in case something set EN earlier) |
||
| 168 | var input = container.querySelector('[data-input]'); |
||
| 169 | if (input && input._flatpickr) { input._flatpickr.destroy(); } |
||
| 170 | |||
| 171 | var loc = buildFlatpickrLocale('{$localeCode}'); |
||
| 172 | |||
| 173 | // Set as global default when possible |
||
| 174 | if (typeof flatpickr.localize === 'function' && typeof loc === 'object') { |
||
| 175 | flatpickr.localize(loc); |
||
| 176 | } |
||
| 177 | if (flatpickr.l10ns && typeof loc === 'object') { |
||
| 178 | flatpickr.l10ns['{$localeCode}'] = loc; |
||
| 179 | } |
||
| 180 | |||
| 181 | var instance = flatpickr('#{$id}_container', { |
||
| 182 | locale: loc, |
||
| 183 | altInput: true, |
||
| 184 | altFormat: '{$altFormat}', |
||
| 185 | enableTime: false, |
||
| 186 | dateFormat: 'Y-m-d', |
||
| 187 | time_24hr: true, |
||
| 188 | wrap: true |
||
| 189 | }); |
||
| 190 | |||
| 191 | try { |
||
| 192 | if (instance && instance.l10n && typeof loc === 'object') { |
||
| 193 | Object.assign(instance.l10n, loc); |
||
| 194 | instance.redraw(); |
||
| 195 | } |
||
| 196 | } catch(e){} |
||
| 197 | } catch(e) { |
||
| 198 | console.error('[DatePicker] flatpickr init error', e); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | initialize(); |
||
| 203 | |||
| 204 | // Hide original label if present (kept from your original code) |
||
| 205 | try { |
||
| 206 | var lbl = document.querySelector('label[for=\"{$id}\"]'); |
||
| 207 | if (lbl) { lbl.style.display = 'none'; lbl.classList.add('datepicker-label'); } |
||
| 256 |