diff --git a/src/Composer/ComposerPackage.php b/src/Composer/ComposerPackage.php index ef10f1a7a945f1abe1be4d13b3ce8baee544a74c..15fc83bdc9ba52497cb34e6286896efd2ebffa7c 100644 --- a/src/Composer/ComposerPackage.php +++ b/src/Composer/ComposerPackage.php @@ -39,23 +39,23 @@ final class ComposerPackage implements ComposerPackageInterface /** * {@inheritdoc} */ - public function getName() + public function getName(): string { - return $this->getKey('name'); + return $this->getKey('name', ''); } /** * {@inheritdoc} */ - public function getDescription() + public function getDescription(): string { - return $this->getKey('description'); + return $this->getKey('description', ''); } /** * {@inheritdoc} */ - public function getRequiredPHPVersion() + public function getRequiredPHPVersion(): float { $require = $this->getKey('require', []); if ( @@ -71,7 +71,7 @@ final class ComposerPackage implements ComposerPackageInterface /** * {@inheritdoc} */ - public function getLicenses() + public function getLicenses(): iterable { return (array) $this->getKey('license', 'proprietary'); } @@ -82,7 +82,7 @@ final class ComposerPackage implements ComposerPackageInterface * * @throws \Assert\AssertionFailedException * - * @return mixed|null + * @return mixed|string|number|null */ private function getKey($key, $default = null) { @@ -93,20 +93,15 @@ final class ComposerPackage implements ComposerPackageInterface return \array_key_exists($key, $data) ? $data[$key] : $default; } - /** - * @return array - */ - private function getComposerJson() + private function getComposerJson(): array { return $this->composerJson !== null ? $this->composerJson : $this->readComposerJson(); } /** * @throws \Assert\AssertionFailedException - * - * @return array */ - private function readComposerJson() + private function readComposerJson(): array { $composerPath = $this->fileLocator->locate('composer.json'); Assertion::notNull($composerPath, 'could not find composer.json'); diff --git a/src/Composer/ComposerPackageInterface.php b/src/Composer/ComposerPackageInterface.php index a6abdd90b0206a06ddec1c4cd4789eb513d070f4..40218d5106bcc57e71c410c64c0f0e25b7ac2272 100644 --- a/src/Composer/ComposerPackageInterface.php +++ b/src/Composer/ComposerPackageInterface.php @@ -15,23 +15,11 @@ namespace Irstea\CS\Composer; */ interface ComposerPackageInterface { - /** - * @return string - */ - public function getName(); + public function getName(): string; - /** - * @return string - */ - public function getDescription(); + public function getDescription(): string; - /** - * @return float - */ - public function getRequiredPHPVersion(); + public function getRequiredPHPVersion(): float; - /** - * @return iterable - */ - public function getLicenses(); + public function getLicenses(): iterable; } diff --git a/src/Config.php b/src/Config.php index 1b69e7a5d59466f6aeb410a90323ac10d48d5ecb..64998adaa5e3dd8dbe81f8702452392c0ad8da82 100644 --- a/src/Config.php +++ b/src/Config.php @@ -1,4 +1,4 @@ -<?php +<?php declare(strict_types=1); /* * irstea/php-cs-fixer-config - Jeux de règles pour php-cs-fixer. * Copyright (C) 2018-2019 IRSTEA @@ -46,13 +46,11 @@ final class Config extends PhpCsFixerConfig /** * Config constructor. - * - * @param string $name */ public function __construct( ComposerPackageInterface $composerPackage, HeaderProviderInterface $headerProvider, - $name = 'default' + string $name = 'default' ) { parent::__construct($name); @@ -60,20 +58,10 @@ final class Config extends PhpCsFixerConfig $this->headerProvider = $headerProvider; } - /** - * Set docHeaderfile. - * - * @param string $docHeaderfile - */ - public function setDocHeaderfile($docHeaderfile) - { - $this->docHeaderfile = $docHeaderfile; - } - /** * {@inheritdoc} */ - public function getRules() + public function getRules(): array { $phpVersion = $this->composerPackage->getRequiredPHPVersion(); $risky = $this->getRiskyAllowed(); @@ -85,13 +73,7 @@ final class Config extends PhpCsFixerConfig ); } - /** - * @param float $phpVersion - * @param bool $risky - * - * @return array - */ - private function ruleSets($phpVersion, $risky) + private function ruleSets(float $phpVersion, bool $risky): array { return [ '@PSR2' => true, @@ -107,13 +89,7 @@ final class Config extends PhpCsFixerConfig ]; } - /** - * @param float $phpVersion - * @param bool $risky - * - * @return array - */ - public function baseRules($phpVersion, $risky) + public function baseRules(float $phpVersion, bool $risky): array { $rules = [ // Configuration && overrides @@ -163,7 +139,7 @@ final class Config extends PhpCsFixerConfig * @param string $rule * @param mixed $setting */ - public function setRule($rule, $setting) + public function setRule($rule, $setting): void { $this->ruleOverrides[$rule] = $setting; } @@ -171,7 +147,7 @@ final class Config extends PhpCsFixerConfig /** * {@inheritdoc} */ - public function setRules(array $rules) + public function setRules(array $rules): void { $this->ruleOverrides = array_replace($this->ruleOverrides, $rules); } @@ -179,7 +155,7 @@ final class Config extends PhpCsFixerConfig /** * {@inheritdoc} */ - public static function create() + public static function create(): self { $stackTrace = debug_backtrace(1); $callerPath = \dirname($stackTrace[0]['file']); diff --git a/src/FileLocator/ChainFileLocator.php b/src/FileLocator/ChainFileLocator.php index 74357b86b015eab003657c8f3f8808eeed82fb46..896751f657567cfac2796e08305676a895f62787 100644 --- a/src/FileLocator/ChainFileLocator.php +++ b/src/FileLocator/ChainFileLocator.php @@ -1,4 +1,4 @@ -<?php +<?php declare(strict_types=1); /* * irstea/php-cs-fixer-config - Jeux de règles pour php-cs-fixer. * Copyright (C) 2018-2019 IRSTEA @@ -27,9 +27,8 @@ final class ChainFileLocator implements FileLocatorInterface * * @param FileLocatorInterface[] $fileLocators */ - public function __construct($fileLocators) + public function __construct(array $fileLocators) { - Assertion::isArray($fileLocators); Assertion::allImplementsInterface($fileLocators, FileLocatorInterface::class); $this->fileLocators = $fileLocators; @@ -38,11 +37,11 @@ final class ChainFileLocator implements FileLocatorInterface /** * {@inheritdoc} */ - public function locate($filename) + public function locate(string $filename): ?string { foreach ($this->fileLocators as $fileLocator) { $result = $fileLocator->locate($filename); - if ($result !== null) { + if ($result) { return $result; } } diff --git a/src/FileLocator/FileLocator.php b/src/FileLocator/FileLocator.php index 88e32827cf207d14064dbc47caf7a0317b2ca3d5..d33d599b15e04afcb40e3020ebc2c0b5188adab6 100644 --- a/src/FileLocator/FileLocator.php +++ b/src/FileLocator/FileLocator.php @@ -1,4 +1,4 @@ -<?php +<?php declare(strict_types=1); /* * irstea/php-cs-fixer-config - Jeux de règles pour php-cs-fixer. * Copyright (C) 2018-2019 IRSTEA @@ -10,8 +10,6 @@ namespace Irstea\CS\FileLocator; -use Assert\Assertion; - /** * Class FileLocator. */ @@ -24,23 +22,17 @@ final class FileLocator implements FileLocatorInterface /** * FileLocator constructor. - * - * @param string $baseDir */ - public function __construct($baseDir) + public function __construct(string $baseDir) { - Assertion::string($baseDir); - $this->baseDir = rtrim($baseDir, \DIRECTORY_SEPARATOR) . \DIRECTORY_SEPARATOR; } /** * {@inheritdoc} */ - public function locate($filename) + public function locate(string $filename): ?string { - Assertion::string($filename); - $path = $this->baseDir . ltrim($filename, \DIRECTORY_SEPARATOR); if (!file_exists($path)) { return null; diff --git a/src/FileLocator/FileLocatorInterface.php b/src/FileLocator/FileLocatorInterface.php index 5a1fb82e38c8a9a0f33e7136309b4bb449c761a8..cdf287a3349607c3f7653bb16d20fd27aa9cd59a 100644 --- a/src/FileLocator/FileLocatorInterface.php +++ b/src/FileLocator/FileLocatorInterface.php @@ -1,4 +1,4 @@ -<?php +<?php declare(strict_types=1); /* * irstea/php-cs-fixer-config - Jeux de règles pour php-cs-fixer. * Copyright (C) 2018-2019 IRSTEA @@ -15,10 +15,5 @@ namespace Irstea\CS\FileLocator; */ interface FileLocatorInterface { - /** - * @param string $filename - * - * @return string|null - */ - public function locate($filename); + public function locate(string $filename): ?string; } diff --git a/src/Git/CachedGitRepository.php b/src/Git/CachedGitRepository.php index a6431177ff366f7965e03dbe67d2421bfbf485d5..9587fd48890dc6e26ab7cd93f4f448048f884b98 100644 --- a/src/Git/CachedGitRepository.php +++ b/src/Git/CachedGitRepository.php @@ -39,7 +39,7 @@ final class CachedGitRepository implements GitRepositoryInterface /** * {@inheritdoc} */ - public function getHeadCommit() + public function getHeadCommit(): string { // Jamais mis en cache ! return $this->inner->getHeadCommit(); @@ -48,7 +48,7 @@ final class CachedGitRepository implements GitRepositoryInterface /** * {@inheritdoc} */ - public function getYearRange() + public function getYearRange(): string { return $this->cache->get('git.year-range', [$this->inner, 'getYearRange']); } diff --git a/src/Git/GitRepository.php b/src/Git/GitRepository.php index db6357eb1dd6634bccd64bd08466740f8bc21c3d..e18ee75a1ed8bf25f318eaf6364867e5a0b085d5 100644 --- a/src/Git/GitRepository.php +++ b/src/Git/GitRepository.php @@ -24,12 +24,9 @@ final class GitRepository implements GitRepositoryInterface /** * GitRepository constructor. - * - * @param string $repositoryPath */ - public function __construct($repositoryPath) + public function __construct(string $repositoryPath) { - Assertion::string($repositoryPath); Assertion::directory($repositoryPath . '/.git'); $this->repositoryPath = $repositoryPath; @@ -38,7 +35,7 @@ final class GitRepository implements GitRepositoryInterface /** * {@inheritdoc} */ - public function getHeadCommit() + public function getHeadCommit(): string { return trim(shell_exec('git -C ' . escapeshellarg($this->repositoryPath) . ' rev-parse HEAD')); } @@ -46,7 +43,7 @@ final class GitRepository implements GitRepositoryInterface /** * {@inheritdoc} */ - public function getYearRange() + public function getYearRange(): string { $last = date('Y'); $first = exec('git -C ' . escapeshellarg($this->repositoryPath) . ' log --format=%cd --date=format:%Y --date-order | tail -n1'); diff --git a/src/Git/GitRepositoryInterface.php b/src/Git/GitRepositoryInterface.php index 4ee3cf13514d66839d2b493aafe4bf411941abec..79cccddb8b6cff335d897bc2d58c814a8e60934b 100644 --- a/src/Git/GitRepositoryInterface.php +++ b/src/Git/GitRepositoryInterface.php @@ -15,13 +15,7 @@ namespace Irstea\CS\Git; */ interface GitRepositoryInterface { - /** - * @return string - */ - public function getHeadCommit(); + public function getHeadCommit(): string; - /** - * @return string - */ - public function getYearRange(); + public function getYearRange(): string; } diff --git a/src/HeaderComment/ChainTemplateProvider.php b/src/HeaderComment/ChainTemplateProvider.php index 52939e95f5ea177d5ee1710d85e7a583296ddda2..661dcb9a3a8a76cd0b094be8fd1346f48006475c 100644 --- a/src/HeaderComment/ChainTemplateProvider.php +++ b/src/HeaderComment/ChainTemplateProvider.php @@ -27,9 +27,8 @@ final class ChainTemplateProvider implements TemplateProviderInterface * * @param TemplateProviderInterface[] $templateProviders */ - public function __construct($templateProviders) + public function __construct(array $templateProviders) { - Assertion::isArray($templateProviders); Assertion::allImplementsInterface($templateProviders, TemplateProviderInterface::class); $this->templateProviders = $templateProviders; @@ -38,7 +37,7 @@ final class ChainTemplateProvider implements TemplateProviderInterface /** * {@inheritdoc} */ - public function getTemplate() + public function getTemplate(): ?string { foreach ($this->templateProviders as $templateProvider) { $template = $templateProvider->getTemplate(); diff --git a/src/HeaderComment/FormattedHeaderProvider.php b/src/HeaderComment/FormattedHeaderProvider.php index e6df362d98b9c954c78c70673602d8941486c7ba..c2fa0421a0c6ac0745dd92bc5fc7f5882f7194ec 100644 --- a/src/HeaderComment/FormattedHeaderProvider.php +++ b/src/HeaderComment/FormattedHeaderProvider.php @@ -37,7 +37,7 @@ final class FormattedHeaderProvider implements HeaderProviderInterface /** * {@inheritdoc} */ - public function getHeader() + public function getHeader(): ?string { $template = $this->templateProvider->getTemplate(); diff --git a/src/HeaderComment/HeaderProviderInterface.php b/src/HeaderComment/HeaderProviderInterface.php index 51acfe7278767110eb2835e9ca55ad3ae97c6a4f..21e00c34f333cd8260919c2c6c00c4d3c2a389b6 100644 --- a/src/HeaderComment/HeaderProviderInterface.php +++ b/src/HeaderComment/HeaderProviderInterface.php @@ -1,4 +1,4 @@ -<?php +<?php declare(strict_types=1); /* * irstea/php-cs-fixer-config - Jeux de règles pour php-cs-fixer. * Copyright (C) 2018-2019 IRSTEA @@ -15,8 +15,5 @@ namespace Irstea\CS\HeaderComment; */ interface HeaderProviderInterface { - /** - * @return string|null - */ - public function getHeader(); + public function getHeader(): ?string; } diff --git a/src/HeaderComment/LicenseTemplateProvider.php b/src/HeaderComment/LicenseTemplateProvider.php index 2419045c0c9633ad20984af7647ee3d5862bed14..6ce59dedb18eecfef742d86c3941ca352866f95a 100644 --- a/src/HeaderComment/LicenseTemplateProvider.php +++ b/src/HeaderComment/LicenseTemplateProvider.php @@ -43,17 +43,14 @@ final class LicenseTemplateProvider implements TemplateProviderInterface /** * {@inheritdoc} */ - public function getTemplate() + public function getTemplate(): ?string { $path = $this->getLicensePath(); return $path ? file_get_contents($path) : null; } - /** - * @return string|null - */ - private function getLicensePath() + private function getLicensePath(): ?string { $licenses = $this->composerPackage->getLicenses(); if (!$licenses) { diff --git a/src/HeaderComment/TemplateFormatter.php b/src/HeaderComment/TemplateFormatter.php index b2be4e92d719cc472efb59342057273a545b6ff9..ca9729a90f638cd3321d89f43451bbb3a7511e6b 100644 --- a/src/HeaderComment/TemplateFormatter.php +++ b/src/HeaderComment/TemplateFormatter.php @@ -10,7 +10,6 @@ namespace Irstea\CS\HeaderComment; -use Assert\Assertion; use Irstea\CS\Composer\ComposerPackageInterface; use Irstea\CS\Git\GitRepositoryInterface; @@ -41,10 +40,8 @@ final class TemplateFormatter implements TemplateFormatterInterface /** * {@inheritdoc} */ - public function format($template) + public function format(string $template): string { - Assertion::string($template); - $variables = [ '%package%' => $this->composerPackage->getName(), '%description%' => $this->composerPackage->getDescription(), diff --git a/src/HeaderComment/TemplateFormatterInterface.php b/src/HeaderComment/TemplateFormatterInterface.php index 330f2d237df6bfe6029189d37df2e59a6da2816f..6234e39a36769d193d8b0456395e4218731d66ce 100644 --- a/src/HeaderComment/TemplateFormatterInterface.php +++ b/src/HeaderComment/TemplateFormatterInterface.php @@ -15,10 +15,5 @@ namespace Irstea\CS\HeaderComment; */ interface TemplateFormatterInterface { - /** - * @param string $template - * - * @return string - */ - public function format($template); + public function format(string $template): string; } diff --git a/src/HeaderComment/TemplateProviderInterface.php b/src/HeaderComment/TemplateProviderInterface.php index 1883fcb5e2f33c02b73bf61b2ca23456b3ce2c69..227ced140759a988789e5002251bdd6459a94a73 100644 --- a/src/HeaderComment/TemplateProviderInterface.php +++ b/src/HeaderComment/TemplateProviderInterface.php @@ -15,8 +15,5 @@ namespace Irstea\CS\HeaderComment; */ interface TemplateProviderInterface { - /** - * @return string|null - */ - public function getTemplate(); + public function getTemplate(): ?string; } diff --git a/src/HeaderComment/UserDefinedTemplateProvider.php b/src/HeaderComment/UserDefinedTemplateProvider.php index 54c00cf47d52b12b48d9cf0ec813e04071b9c63f..ccb24fb1e1c0e05cb5e0fc4c9ea4cf96ab74f090 100644 --- a/src/HeaderComment/UserDefinedTemplateProvider.php +++ b/src/HeaderComment/UserDefinedTemplateProvider.php @@ -1,4 +1,4 @@ -<?php +<?php declare(strict_types=1); /* * irstea/php-cs-fixer-config - Jeux de règles pour php-cs-fixer. * Copyright (C) 2018-2019 IRSTEA @@ -33,8 +33,10 @@ final class UserDefinedTemplateProvider implements TemplateProviderInterface /** * {@inheritdoc} */ - public function getTemplate() + public function getTemplate(): ?string { - return $this->fileLocator->locate('.docheader'); + $path = $this->fileLocator->locate('.docheader'); + + return $path ? file_get_contents($path) : null; } } diff --git a/tests/FileLocator/FileLocatorTest.php b/tests/FileLocator/FileLocatorTest.php index 7dc2e3574976bebdac2502f6c2ec7fbe3d94a0a2..c401cda069a5e2d4a7f9190d6c2728bd9aed1f28 100644 --- a/tests/FileLocator/FileLocatorTest.php +++ b/tests/FileLocator/FileLocatorTest.php @@ -19,7 +19,7 @@ use PHPUnit\Framework\TestCase; */ class FileLocatorTest extends TestCase { - public function testShouldLocateExistingFile() + public function testShouldLocateExistingFile(): void { $fs = vfsStream::setup('root', 0755, [ 'file' => 'content', @@ -33,7 +33,7 @@ class FileLocatorTest extends TestCase ); } - public function testShouldReturnNullOnMissingFile() + public function testShouldReturnNullOnMissingFile(): void { $fs = vfsStream::setup(); $locator = new FileLocator($fs->url()); diff --git a/tests/HeaderComment/LicenseTemplateProviderTest.php b/tests/HeaderComment/LicenseTemplateProviderTest.php index a84096b92ab9c804bbf964e8047c19f158cc7af0..0fdcb1fdf4d8cae4688d19c2fba534e5bade5da1 100644 --- a/tests/HeaderComment/LicenseTemplateProviderTest.php +++ b/tests/HeaderComment/LicenseTemplateProviderTest.php @@ -1,4 +1,4 @@ -<?php +<?php declare(strict_types=1); /* * irstea/php-cs-fixer-config - Jeux de règles pour php-cs-fixer. * Copyright (C) 2018-2019 IRSTEA @@ -69,7 +69,7 @@ final class LicenseTemplateProviderTest extends TestCase ); } - public function testShouldProvideKnownLicenseHeader() + public function testShouldProvideKnownLicenseHeader(): void { $this->composerPackage->getLicenses() ->shouldBeCalled() @@ -78,7 +78,7 @@ final class LicenseTemplateProviderTest extends TestCase self::assertEquals('GPL-template', $this->templateProvider->getTemplate()); } - public function testShouldProvideDefaultLicenseHeader() + public function testShouldProvideDefaultLicenseHeader(): void { $this->composerPackage->getLicenses() ->shouldBeCalled() @@ -87,7 +87,7 @@ final class LicenseTemplateProviderTest extends TestCase self::assertEquals('default-template', $this->templateProvider->getTemplate()); } - public function testShouldProvideProprietaryLicenseHeaderWithNoLicense() + public function testShouldProvideProprietaryLicenseHeaderWithNoLicense(): void { $this->composerPackage->getLicenses() ->shouldBeCalled()