Complex classes like Shim 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Shim, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Shim { |
||
|
|||
27 | /** @var FrenchCalendar */ |
||
28 | private static $french_calendar; |
||
29 | |||
30 | /** @var GregorianCalendar */ |
||
31 | private static $gregorian_calendar; |
||
32 | |||
33 | /** @var JewishCalendar */ |
||
34 | private static $jewish_calendar; |
||
35 | |||
36 | /** @var JulianCalendar */ |
||
37 | private static $julian_calendar; |
||
38 | |||
39 | /** |
||
40 | * English names for the days of the week. |
||
41 | * |
||
42 | * @var string[] |
||
43 | */ |
||
44 | private static $DAY_NAMES = array( |
||
45 | 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', |
||
46 | ); |
||
47 | |||
48 | /** |
||
49 | * Abbreviated English names for the days of the week. |
||
50 | * |
||
51 | * @var string[] |
||
52 | */ |
||
53 | private static $DAY_NAMES_SHORT = array( |
||
54 | 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', |
||
55 | ); |
||
56 | |||
57 | /** @var string[] Names of the months of the Gregorian/Julian calendars */ |
||
58 | private static $MONTH_NAMES = array( |
||
59 | '', 'January', 'February', 'March', 'April', 'May', 'June', |
||
60 | 'July', 'August', 'September', 'October', 'November', 'December', |
||
61 | ); |
||
62 | |||
63 | /** @var string[] Abbreviated names of the months of the Gregorian/Julian calendars */ |
||
64 | private static $MONTH_NAMES_SHORT = array( |
||
65 | '', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', |
||
66 | ); |
||
67 | |||
68 | /** @var string[] Name of the months of the French calendar */ |
||
69 | private static $MONTH_NAMES_FRENCH = array( |
||
70 | '', 'Vendemiaire', 'Brumaire', 'Frimaire', 'Nivose', 'Pluviose', 'Ventose', |
||
71 | 'Germinal', 'Floreal', 'Prairial', 'Messidor', 'Thermidor', 'Fructidor', 'Extra' |
||
72 | ); |
||
73 | |||
74 | /** @var string[] Names of the months of the Jewish calendar in a non-leap year */ |
||
75 | private static $MONTH_NAMES_JEWISH = array( |
||
76 | '', 'Tishri', 'Heshvan', 'Kislev', 'Tevet', 'Shevat', 'Adar', |
||
77 | 'Adar', 'Nisan', 'Iyyar', 'Sivan', 'Tammuz', 'Av', 'Elul', |
||
78 | ); |
||
79 | |||
80 | /** @var string[] Names of the months of the Jewish calendar in a leap year */ |
||
81 | private static $MONTH_NAMES_JEWISH_LEAP_YEAR = array( |
||
82 | '', 'Tishri', 'Heshvan', 'Kislev', 'Tevet', 'Shevat', 'Adar I', |
||
83 | 'Adar II', 'Nisan', 'Iyyar', 'Sivan', 'Tammuz', 'Av', 'Elul', |
||
84 | ); |
||
85 | |||
86 | /** @var string[] Names of the months of the Jewish calendar (before PHP bug 54254 was fixed) */ |
||
87 | private static $MONTH_NAMES_JEWISH_54254 = array( |
||
88 | '', 'Tishri', 'Heshvan', 'Kislev', 'Tevet', 'Shevat', 'AdarI', |
||
89 | 'AdarII', 'Nisan', 'Iyyar', 'Sivan', 'Tammuz', 'Av', 'Elul', |
||
90 | ); |
||
91 | |||
92 | /** |
||
93 | * Create the necessary shims to emulate the ext/calendar package. |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | public static function create() { |
||
105 | |||
106 | /** |
||
107 | * Do we need to emulate PHP bug #54254? |
||
108 | * |
||
109 | * This bug relates to the names used for months 6 and 7 in the Jewish calendar. |
||
110 | * |
||
111 | * It was fixed in PHP 5.5.0 |
||
112 | * |
||
113 | * @link https://bugs.php.net/bug.php?id=54254 |
||
114 | * |
||
115 | * @return boolean |
||
116 | */ |
||
117 | public static function shouldEmulateBug54254() { |
||
120 | |||
121 | /** |
||
122 | * Do we need to emulate PHP bug #67960? |
||
123 | * |
||
124 | * This bug relates to the constants CAL_DOW_SHORT and CAL_DOW_LONG. |
||
125 | * |
||
126 | * It was fixed in PHP 5.6.5 and 5.5.21 |
||
127 | * |
||
128 | * @link https://bugs.php.net/bug.php?id=67960 |
||
129 | * @link https://github.com/php/php-src/pull/806 |
||
130 | * |
||
131 | * @return boolean |
||
132 | */ |
||
133 | public static function shouldEmulateBug67960() { |
||
136 | |||
137 | /** |
||
138 | * Do we need to emulate PHP bug #67976? |
||
139 | * |
||
140 | * This bug relates to the number of days in the month 13 of year 14 in |
||
141 | * the French calendar. |
||
142 | * |
||
143 | * It was fixed in PHP 5.6.25 and 7.0.10 |
||
144 | * |
||
145 | * @link https://bugs.php.net/bug.php?id=67976 |
||
146 | * |
||
147 | * @return boolean |
||
148 | */ |
||
149 | public static function shouldEmulateBug67976() { |
||
152 | |||
153 | /** |
||
154 | * Return the number of days in a month for a given year and calendar. |
||
155 | * |
||
156 | * Shim implementation of cal_days_in_month() |
||
157 | * |
||
158 | * @link https://php.net/cal_days_in_month |
||
159 | * @link https://bugs.php.net/bug.php?id=67976 |
||
160 | * |
||
161 | * @param integer $calendar_id |
||
162 | * @param integer $month |
||
163 | * @param integer $year |
||
164 | * |
||
165 | * @return integer|boolean The number of days in the specified month, or false on error |
||
166 | */ |
||
167 | public static function calDaysInMonth($calendar_id, $month, $year) { |
||
185 | |||
186 | /** |
||
187 | * Calculate the number of days in a month in a specified (Gregorian or Julian) calendar. |
||
188 | * |
||
189 | * @param CalendarInterface $calendar |
||
190 | * @param integer $year |
||
191 | * @param integer $month |
||
192 | * |
||
193 | * @return integer|boolean |
||
194 | */ |
||
195 | private static function calDaysInMonthCalendar(CalendarInterface $calendar, $year, $month) { |
||
202 | |||
203 | /** |
||
204 | * Calculate the number of days in a month in the French calendar. |
||
205 | * |
||
206 | * Mimic PHP’s validation of the parameters |
||
207 | * |
||
208 | * @param integer $year |
||
209 | * @param integer $month |
||
210 | * |
||
211 | * @return integer|boolean |
||
212 | */ |
||
213 | private static function calDaysInMonthFrench($year, $month) { |
||
222 | |||
223 | /** |
||
224 | * Converts from Julian Day Count to a supported calendar. |
||
225 | * |
||
226 | * Shim implementation of cal_from_jd() |
||
227 | * |
||
228 | * @link https://php.net/cal_from_jd |
||
229 | * |
||
230 | * @param integer $julian_day Julian Day number |
||
231 | * @param integer $calendar_id Calendar constant |
||
232 | * |
||
233 | * @return array|boolean |
||
234 | */ |
||
235 | public static function calFromJd($julian_day, $calendar_id) { |
||
255 | |||
256 | /** |
||
257 | * Convert a Julian day number to a calendar and provide details. |
||
258 | * |
||
259 | * @param integer $julian_day |
||
260 | * @param string $mdy |
||
261 | * @param string[] $months |
||
262 | * @param string[] $months_short |
||
263 | * |
||
264 | * @return array |
||
265 | */ |
||
266 | private static function calFromJdCalendar($julian_day, $mdy, $months, $months_short) { |
||
281 | |||
282 | /** |
||
283 | * Returns information about a particular calendar. |
||
284 | * |
||
285 | * Shim implementation of cal_info() |
||
286 | * |
||
287 | * @link https://php.net/cal_info |
||
288 | * |
||
289 | * @param integer $calendar_id |
||
290 | * |
||
291 | * @return array|boolean |
||
292 | */ |
||
293 | public static function calInfo($calendar_id) { |
||
321 | |||
322 | /** |
||
323 | * Returns information about the French calendar. |
||
324 | * |
||
325 | * @param string[] $month_names |
||
326 | * @param string[] $month_names_short |
||
327 | * @param integer $max_days_in_month |
||
328 | * @param string $calendar_name |
||
329 | * @param string $calendar_symbol |
||
330 | * |
||
331 | * @return array |
||
332 | */ |
||
333 | private static function calInfoCalendar($month_names, $month_names_short, $max_days_in_month, $calendar_name, $calendar_symbol) { |
||
342 | |||
343 | /** |
||
344 | * Converts from a supported calendar to Julian Day Count |
||
345 | * |
||
346 | * Shim implementation of cal_to_jd() |
||
347 | * |
||
348 | * @link https://php.net/cal_to_jd |
||
349 | * |
||
350 | * @param integer $calendar_id |
||
351 | * @param integer $month |
||
352 | * @param integer $day |
||
353 | * @param integer $year |
||
354 | * |
||
355 | * @return integer|boolean |
||
356 | */ |
||
357 | public static function calToJd($calendar_id, $month, $day, $year) { |
||
375 | |||
376 | /** |
||
377 | * Get Unix timestamp for midnight on Easter of a given year. |
||
378 | * |
||
379 | * Shim implementation of easter_date() |
||
380 | * |
||
381 | * @link https://php.net/easter_date |
||
382 | * |
||
383 | * @param integer $year |
||
384 | * |
||
385 | * @return integer|boolean |
||
386 | */ |
||
387 | public static function easterDate($year) { |
||
404 | |||
405 | /** |
||
406 | * Get number of days after March 21 on which Easter falls for a given year. |
||
407 | * |
||
408 | * Shim implementation of easter_days() |
||
409 | * |
||
410 | * @link https://php.net/easter_days |
||
411 | * |
||
412 | * @param integer $year |
||
413 | * @param integer $method Use the Julian or Gregorian calendar |
||
414 | * |
||
415 | * @return integer |
||
416 | */ |
||
417 | public static function easterDays($year, $method) { |
||
428 | |||
429 | /** |
||
430 | * Converts a date from the French Republican Calendar to a Julian Day Count. |
||
431 | * |
||
432 | * Shim implementation of FrenchToJD() |
||
433 | * |
||
434 | * @link https://php.net/FrenchToJD |
||
435 | * |
||
436 | * @param integer $month |
||
437 | * @param integer $day |
||
438 | * @param integer $year |
||
439 | * |
||
440 | * @return integer |
||
441 | */ |
||
442 | public static function frenchToJd($month, $day, $year) { |
||
449 | |||
450 | /** |
||
451 | * Converts a Gregorian date to Julian Day Count. |
||
452 | * |
||
453 | * Shim implementation of GregorianToJD() |
||
454 | * |
||
455 | * @link https://php.net/GregorianToJD |
||
456 | * |
||
457 | * @param integer $month |
||
458 | * @param integer $day |
||
459 | * @param integer $year |
||
460 | * |
||
461 | * @return integer |
||
462 | */ |
||
463 | public static function gregorianToJd($month, $day, $year) { |
||
470 | |||
471 | /** |
||
472 | * Returns the day of the week. |
||
473 | * |
||
474 | * Shim implementation of JDDayOfWeek() |
||
475 | * |
||
476 | * @link https://php.net/JDDayOfWeek |
||
477 | * @link https://bugs.php.net/bug.php?id=67960 |
||
478 | * |
||
479 | * @param integer $julian_day |
||
480 | * @param integer $mode |
||
481 | * |
||
482 | * @return integer|string |
||
483 | */ |
||
484 | public static function jdDayOfWeek($julian_day, $mode) { |
||
501 | |||
502 | /** |
||
503 | * Returns a month name. |
||
504 | * |
||
505 | * Shim implementation of JDMonthName() |
||
506 | * |
||
507 | * @link https://php.net/JDMonthName |
||
508 | * |
||
509 | * @param integer $julian_day |
||
510 | * @param integer $mode |
||
511 | * |
||
512 | * @return string |
||
513 | */ |
||
514 | public static function jdMonthName($julian_day, $mode) { |
||
536 | |||
537 | /** |
||
538 | * Calculate the month-name for a given julian day, in a given calendar, |
||
539 | * with given set of month names. |
||
540 | * |
||
541 | * @param CalendarInterface $calendar |
||
542 | * @param integer $julian_day |
||
543 | * @param string[] $months |
||
544 | * |
||
545 | * @return string |
||
546 | */ |
||
547 | private static function jdMonthNameCalendar(CalendarInterface $calendar, $julian_day, $months) { |
||
552 | |||
553 | /** |
||
554 | * Determine which month names to use for the Jewish calendar. |
||
555 | * |
||
556 | * @param integer $julian_day |
||
557 | * |
||
558 | * @return string[] |
||
559 | */ |
||
560 | private static function jdMonthNameJewishMonths($julian_day) { |
||
569 | |||
570 | /** |
||
571 | * Convert a Julian day in a specific calendar to a day/month/year. |
||
572 | * |
||
573 | * Julian days outside the specified range are returned as “0/0/0”. |
||
574 | * |
||
575 | * @param CalendarInterface $calendar |
||
576 | * @param integer $julian_day |
||
577 | * @param integer $min_jd |
||
578 | * @param integer $max_jd |
||
579 | * |
||
580 | * @return string |
||
581 | */ |
||
582 | private static function jdToCalendar(CalendarInterface $calendar, $julian_day, $min_jd, $max_jd) { |
||
591 | |||
592 | /** |
||
593 | * Converts a Julian Day Count to the French Republican Calendar. |
||
594 | * |
||
595 | * Shim implementation of JDToFrench() |
||
596 | * |
||
597 | * @link https://php.net/JDToFrench |
||
598 | * |
||
599 | * @param integer $julian_day A Julian Day number |
||
600 | * |
||
601 | * @return string A string of the form "month/day/year" |
||
602 | */ |
||
603 | public static function jdToFrench($julian_day) { |
||
608 | |||
609 | /** |
||
610 | * Converts Julian Day Count to Gregorian date. |
||
611 | * |
||
612 | * Shim implementation of JDToGregorian() |
||
613 | * |
||
614 | * @link https://php.net/JDToGregorian |
||
615 | * |
||
616 | * @param integer $julian_day A Julian Day number |
||
617 | * |
||
618 | * @return string A string of the form "month/day/year" |
||
619 | */ |
||
620 | public static function jdToGregorian($julian_day) { |
||
626 | |||
627 | /** |
||
628 | * Converts a Julian day count to a Jewish calendar date. |
||
629 | * |
||
630 | * Shim implementation of JdtoJjewish() |
||
631 | * |
||
632 | * @link https://php.net/JdtoJewish |
||
633 | * |
||
634 | * @param integer $julian_day A Julian Day number |
||
635 | * @param boolean $hebrew If true, the date is returned in Hebrew text |
||
636 | * @param integer $fl If $hebrew is true, then add alafim and gereshayim to the text |
||
637 | * |
||
638 | * @return string|boolean A string of the form "month/day/year", or false on error |
||
639 | */ |
||
640 | public static function jdToJewish($julian_day, $hebrew, $fl) { |
||
657 | |||
658 | /** |
||
659 | * Converts a Julian Day Count to a Julian Calendar Date. |
||
660 | * |
||
661 | * Shim implementation of JDToJulian() |
||
662 | * |
||
663 | * @link https://php.net/JDToJulian |
||
664 | * |
||
665 | * @param integer $julian_day A Julian Day number |
||
666 | * |
||
667 | * @return string A string of the form "month/day/year" |
||
668 | */ |
||
669 | public static function jdToJulian($julian_day) { |
||
675 | |||
676 | /** |
||
677 | * Convert Julian Day to Unix timestamp. |
||
678 | * |
||
679 | * Shim implementation of jdtounix() |
||
680 | * |
||
681 | * @link https://php.net/jdtounix |
||
682 | * |
||
683 | * @param integer $julian_day |
||
684 | * |
||
685 | * @return integer|false |
||
686 | */ |
||
687 | public static function jdToUnix($julian_day) { |
||
694 | |||
695 | /** |
||
696 | * Converts a date in the Jewish Calendar to Julian Day Count. |
||
697 | * |
||
698 | * Shim implementation of JewishToJD() |
||
699 | * |
||
700 | * @link https://php.net/JewishToJD |
||
701 | * |
||
702 | * @param integer $month |
||
703 | * @param integer $day |
||
704 | * @param integer $year |
||
705 | * |
||
706 | * @return integer |
||
707 | */ |
||
708 | public static function jewishToJd($month, $day, $year) { |
||
715 | |||
716 | /** |
||
717 | * Converts a Julian Calendar date to Julian Day Count. |
||
718 | * |
||
719 | * Shim implementation of JdToJulian() |
||
720 | * |
||
721 | * @link https://php.net/JdToJulian |
||
722 | * |
||
723 | * @param integer $month |
||
724 | * @param integer $day |
||
725 | * @param integer $year |
||
726 | * |
||
727 | * @return integer |
||
728 | */ |
||
729 | public static function julianToJd($month, $day, $year) { |
||
736 | |||
737 | /** |
||
738 | * Convert Unix timestamp to Julian Day. |
||
739 | * |
||
740 | * Shim implementation of unixtojd() |
||
741 | * |
||
742 | * @link https://php.net/unixtojd |
||
743 | * |
||
744 | * @param integer $timestamp |
||
745 | * |
||
746 | * @return false|integer |
||
747 | */ |
||
748 | public static function unixToJd($timestamp) { |
||
756 | |||
757 | /** |
||
758 | * By default, the result of A % B will be of the sign of A. |
||
759 | * This is not necessarily the agreed version of the mod operator by other languages. |
||
760 | * This mod function returns always the positive result of the modulo operation. |
||
761 | * |
||
762 | * For example: |
||
763 | * (Standard PHP) -3 % 4 = -3 |
||
764 | * (Shim) Shim::mod(-3, 4) = 1 |
||
765 | * |
||
766 | * @param number $dividend |
||
767 | * @param number $divisor |
||
768 | * @return number |
||
769 | */ |
||
770 | public static function mod($dividend, $divisor) { |
||
780 | } |
||
781 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.