|
9 | 9 |
|
10 | 10 | namespace ZendCodingStandard\Sniffs\Files;
|
11 | 11 |
|
| 12 | +use DateTime; |
| 13 | +use DateTimeZone; |
12 | 14 | use PHP_CodeSniffer\Config;
|
13 | 15 | use PHP_CodeSniffer\Files\File;
|
14 | 16 | use PHP_CodeSniffer\Sniffs\Sniff;
|
15 | 17 |
|
16 | 18 | use function array_merge;
|
17 | 19 | use function basename;
|
| 20 | +use function exec; |
18 | 21 | use function file_get_contents;
|
19 | 22 | use function filter_var;
|
20 |
| -use function gmdate; |
21 |
| -use function preg_match; |
22 | 23 | use function rtrim;
|
23 | 24 | use function strpos;
|
24 | 25 | use function strstr;
|
@@ -60,6 +61,16 @@ class MdSniff implements Sniff
|
60 | 61 | */
|
61 | 62 | public $variables = [];
|
62 | 63 |
|
| 64 | + /** |
| 65 | + * @var string |
| 66 | + */ |
| 67 | + public $yearTimezone = 'GMT'; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var null|string |
| 71 | + */ |
| 72 | + private $yearRange; |
| 73 | + |
63 | 74 | /**
|
64 | 75 | * @return int[]
|
65 | 76 | */
|
@@ -113,17 +124,7 @@ public function process(File $phpcsFile, $stackPtr)
|
113 | 124 |
|
114 | 125 | $content = $phpcsFile->getTokensAsString(0, $phpcsFile->numTokens);
|
115 | 126 |
|
116 |
| - $variables = $this->variables; |
117 |
| - if (preg_match('/\s(\d{4})(-\d{4})?/', $content, $match)) { |
118 |
| - $year = $match[1]; |
119 |
| - $currentYear = gmdate('Y'); |
120 |
| - if ($year < $currentYear) { |
121 |
| - $year .= '-' . $currentYear; |
122 |
| - } |
123 |
| - $variables['{year}'] = $year; |
124 |
| - } |
125 |
| - |
126 |
| - $newContent = strtr($template, array_merge($this->getDefaultVariables(), $variables)); |
| 127 | + $newContent = strtr($template, array_merge($this->getDefaultVariables(), $this->variables)); |
127 | 128 |
|
128 | 129 | if ($content !== $newContent) {
|
129 | 130 | $error = 'Content is outdated; found %s; expected %s';
|
@@ -165,7 +166,26 @@ private function getDefaultVariables() : array
|
165 | 166 | '{category}' => Config::getConfigData('zfcs:category') ?: 'components',
|
166 | 167 | '{org}' => Config::getConfigData('zfcs:org') ?: 'zendframework',
|
167 | 168 | '{repo}' => Config::getConfigData('zfcs:repo'),
|
168 |
| - '{year}' => gmdate('Y'), |
| 169 | + '{year}' => $this->getYearRange(), |
169 | 170 | ];
|
170 | 171 | }
|
| 172 | + |
| 173 | + private function getYearRange() : string |
| 174 | + { |
| 175 | + if (! $this->yearRange) { |
| 176 | + $timezone = new DateTimeZone($this->yearTimezone); |
| 177 | + |
| 178 | + exec('git tag -l --format=\'%(taggerdate)\' | head -n 1', $output, $return); |
| 179 | + $date = new DateTime($return === 0 && isset($output[0]) ? $output[0] : 'now'); |
| 180 | + $date->setTimezone($timezone); |
| 181 | + $this->yearRange = $date->format('Y'); |
| 182 | + |
| 183 | + $currentYear = (new DateTime('now', $timezone))->format('Y'); |
| 184 | + if ($this->yearRange < $currentYear) { |
| 185 | + $this->yearRange .= '-' . $currentYear; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + return $this->yearRange; |
| 190 | + } |
171 | 191 | }
|
0 commit comments