| Conditions | 4 |
| Paths | 4 |
| Total Lines | 51 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 62 | public static function toPHPDateFormat($excelDateFormat) |
||
| 63 | { |
||
| 64 | // Remove brackets potentially present at the beginning of the format string |
||
| 65 | $dateFormat = preg_replace('/^(\[\$[^\]]+?\])/i', '', $excelDateFormat); |
||
| 66 | |||
| 67 | // Double quotes are used to escape characters that must not be interpreted. |
||
| 68 | // For instance, ["Day " dd] should result in "Day 13" and we should not try to interpret "D", "a", "y" |
||
| 69 | // By exploding the format string using double quote as a delimiter, we can get all parts |
||
| 70 | // that must be transformed (even indexes) and all parts that must not be (odd indexes). |
||
| 71 | $dateFormatParts = explode('"', $dateFormat); |
||
| 72 | |||
| 73 | foreach ($dateFormatParts as $partIndex => $dateFormatPart) { |
||
| 74 | // do not look at odd indexes |
||
| 75 | if ($partIndex % 2 === 1) { |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | |||
| 79 | // Make sure all characters are lowercase, as the mapping table is using lowercase characters |
||
| 80 | $transformedPart = strtolower($dateFormatPart); |
||
| 81 | |||
| 82 | // Remove escapes related to non-format characters |
||
| 83 | $transformedPart = str_replace('\\', '', $transformedPart); |
||
| 84 | |||
| 85 | // Apply general transformation first... |
||
| 86 | $transformedPart = strtr($transformedPart, self::$excelDateFormatToPHPDateFormatMapping[self::KEY_GENERAL]); |
||
| 87 | |||
| 88 | // ... then apply hour transformation, for 12-hour or 24-hour format |
||
| 89 | if (self::has12HourFormatMarker($dateFormatPart)) { |
||
| 90 | $transformedPart = strtr($transformedPart, self::$excelDateFormatToPHPDateFormatMapping[self::KEY_HOUR_12]); |
||
| 91 | } else { |
||
| 92 | $transformedPart = strtr($transformedPart, self::$excelDateFormatToPHPDateFormatMapping[self::KEY_HOUR_24]); |
||
| 93 | } |
||
| 94 | |||
| 95 | // overwrite the parts array with the new transformed part |
||
| 96 | $dateFormatParts[$partIndex] = $transformedPart; |
||
| 97 | } |
||
| 98 | |||
| 99 | // Merge all transformed parts back together |
||
| 100 | $phpDateFormat = implode('"', $dateFormatParts); |
||
| 101 | |||
| 102 | // Finally, to have the date format compatible with the DateTime::format() function, we need to escape |
||
| 103 | // all characters that are inside double quotes (and double quotes must be removed). |
||
| 104 | // For instance, ["Day " dd] should become [\D\a\y\ dd] |
||
| 105 | $phpDateFormat = preg_replace_callback('/"(.+?)"/', function($matches) { |
||
| 106 | $stringToEscape = $matches[1]; |
||
| 107 | $letters = preg_split('//u', $stringToEscape, -1, PREG_SPLIT_NO_EMPTY); |
||
| 108 | return '\\' . implode('\\', $letters); |
||
| 109 | }, $phpDateFormat); |
||
| 110 | |||
| 111 | return $phpDateFormat; |
||
| 112 | } |
||
| 113 | |||
| 123 |