Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 77588a8

Browse files
committed
Use first tag date in year range
1 parent fe7a622 commit 77588a8

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

COPYRIGHT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Copyright (c) 2018, Zend Technologies USA, Inc.
1+
Copyright (c) 2016-2018, Zend Technologies USA, Inc.

src/ZendCodingStandard/Sniffs/Files/MdSniff.php

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99

1010
namespace ZendCodingStandard\Sniffs\Files;
1111

12+
use DateTime;
13+
use DateTimeZone;
1214
use PHP_CodeSniffer\Config;
1315
use PHP_CodeSniffer\Files\File;
1416
use PHP_CodeSniffer\Sniffs\Sniff;
1517

1618
use function array_merge;
1719
use function basename;
20+
use function exec;
1821
use function file_get_contents;
1922
use function filter_var;
20-
use function gmdate;
21-
use function preg_match;
2223
use function rtrim;
2324
use function strpos;
2425
use function strstr;
@@ -60,6 +61,16 @@ class MdSniff implements Sniff
6061
*/
6162
public $variables = [];
6263

64+
/**
65+
* @var string
66+
*/
67+
public $yearTimezone = 'GMT';
68+
69+
/**
70+
* @var null|string
71+
*/
72+
private $yearRange;
73+
6374
/**
6475
* @return int[]
6576
*/
@@ -113,17 +124,7 @@ public function process(File $phpcsFile, $stackPtr)
113124

114125
$content = $phpcsFile->getTokensAsString(0, $phpcsFile->numTokens);
115126

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));
127128

128129
if ($content !== $newContent) {
129130
$error = 'Content is outdated; found %s; expected %s';
@@ -165,7 +166,26 @@ private function getDefaultVariables() : array
165166
'{category}' => Config::getConfigData('zfcs:category') ?: 'components',
166167
'{org}' => Config::getConfigData('zfcs:org') ?: 'zendframework',
167168
'{repo}' => Config::getConfigData('zfcs:repo'),
168-
'{year}' => gmdate('Y'),
169+
'{year}' => $this->getYearRange(),
169170
];
170171
}
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+
}
171191
}

0 commit comments

Comments
 (0)