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