diff --git a/src/Config.php b/src/Config.php index 67f3b3e052f80955b44f74a735dba036c64a9adb..86573e48a0832e95f7b6eb9aabe0b5295c061253 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; } }