From 0ef5bf3d7b4ed40dd64e5aaa136575a1fbbe0036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr> Date: Thu, 3 Sep 2020 13:15:17 +0200 Subject: [PATCH] =?UTF-8?q?Ne=20plante=20pas=20si=20on=20ne=20trouve=20pas?= =?UTF-8?q?=20de=20d=C3=A9p=C3=B4t=20git.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Config.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Config.php b/src/Config.php index 67f3b3e..86573e4 100644 --- a/src/Config.php +++ b/src/Config.php @@ -5,7 +5,6 @@ * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. - * */ namespace Irstea\CS; @@ -167,8 +166,7 @@ final class Config extends PhpCsFixerConfig $fileLocator = new FileLocator($callerPath); $composerPackage = new ComposerPackage($fileLocator); - $repoPath = self::findGitRepository($callerPath); - $gitRepository = new GitRepository($repoPath); + $variables = self::getTemplateVariables($callerPath, $composerPackage); $headerProvider = new FormattedHeaderProvider( new ChainTemplateProvider( @@ -178,18 +176,34 @@ final class Config extends PhpCsFixerConfig new LicenseTemplateProvider($composerPackage), ] ), - new TemplateFormatter($gitRepository, $composerPackage) + new TemplateFormatter($variables) ); return new self($composerPackage, $headerProvider); } - private static function findGitRepository(string $path): string + private static function getTemplateVariables(string $path, ComposerPackage $composerPackage): array + { + $gitRepository = self::findGitRepository($path); + + return [ + '%copyrightHolder%' => 'INRAE', + '%package%' => $composerPackage->getName(), + '%description%' => $composerPackage->getDescription(), + '%yearRange%' => $gitRepository ? $gitRepository->getYearRange() : date('Y'), + ]; + } + + private static function findGitRepository(string $path): ?GitRepository { - while (!is_dir($path . '/.git') && $path !== '/') { + while ($path !== '/') { + if (is_dir($path . '/.git')) { + return new GitRepository($path); + } + $path = \dirname($path); } - return $path; + return null; } } -- GitLab