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

Commit

Permalink
Use first tag date in year range
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Apr 4, 2018
1 parent e7528fe commit a6c1858
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion COPYRIGHT.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Copyright (c) 2018, Zend Technologies USA, Inc.
Copyright (c) 2016-2018, Zend Technologies USA, Inc.
48 changes: 34 additions & 14 deletions src/ZendCodingStandard/Sniffs/Files/MdSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@

namespace ZendCodingStandard\Sniffs\Files;

use DateTime;
use DateTimeZone;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

use function array_merge;
use function basename;
use function exec;
use function file_get_contents;
use function filter_var;
use function gmdate;
use function preg_match;
use function rtrim;
use function strpos;
use function strstr;
Expand Down Expand Up @@ -60,6 +61,16 @@ class MdSniff implements Sniff
*/
public $variables = [];

/**
* @var string
*/
public $yearTimezone = 'GMT';

/**
* @var null|string
*/
private $yearRange;

/**
* @return int[]
*/
Expand Down Expand Up @@ -113,17 +124,7 @@ public function process(File $phpcsFile, $stackPtr)

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

$variables = $this->variables;
if (preg_match('/\s(\d{4})(-\d{4})?/', $content, $match)) {
$year = $match[1];
$currentYear = gmdate('Y');
if ($year < $currentYear) {
$year .= '-' . $currentYear;
}
$variables['{year}'] = $year;
}

$newContent = strtr($template, array_merge($this->getDefaultVariables(), $variables));
$newContent = strtr($template, array_merge($this->getDefaultVariables(), $this->variables));

if ($content !== $newContent) {
$error = 'Content is outdated; found %s; expected %s';
Expand Down Expand Up @@ -165,7 +166,26 @@ private function getDefaultVariables() : array
'{category}' => Config::getConfigData('zfcs:category') ?: 'components',
'{org}' => Config::getConfigData('zfcs:org') ?: 'zendframework',
'{repo}' => Config::getConfigData('zfcs:repo'),
'{year}' => gmdate('Y'),
'{year}' => $this->getYearRange(),
];
}

private function getYearRange() : string
{
if (! $this->yearRange) {
$timezone = new DateTimeZone($this->yearTimezone);

exec('git tag -l --format=\'%(taggerdate)\' | head -n 1', $output, $return);
$date = new DateTime($return === 0 && isset($output[0]) ? $output[0] : 'now');
$date->setTimezone($timezone);
$this->yearRange = $date->format('Y');

$currentYear = (new DateTime('now', $timezone))->format('Y');
if ($this->yearRange < $currentYear) {
$this->yearRange .= '-' . $currentYear;
}
}

return $this->yearRange;
}
}

0 comments on commit a6c1858

Please sign in to comment.