diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a837cd7..cb590c8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -52,19 +52,19 @@ lint: - find src/ -type f -name '*.php' -exec php -l {} \; | (! grep -v "No syntax errors detected" ) dependencies: [] -phpcs: +phpmd: stage: check script: - - vendor/bin/phpcs + - composer phpmd dependencies: - prepare_cache needs: - prepare_cache -phpmd: +code-style: stage: check script: - - vendor/bin/phpmd src/ text phpmd.xml.dist + - composer cs dependencies: - prepare_cache needs: diff --git a/composer.json b/composer.json index 4fa34c8..4ff1c97 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,9 @@ "phpmd/phpmd": "^2.10", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^9.5", - "squizlabs/php_codesniffer": "^3.6", + "slevomat/coding-standard": "^8.14", + "squizlabs/php_codesniffer": "^3.7", + "symplify/easy-coding-standard": "^12.0", "symfony/process": "^5.3" }, "suggest": { @@ -61,7 +63,8 @@ }, "config": { "allow-plugins": { - "infection/extension-installer": true + "infection/extension-installer": true, + "dealerdirect/phpcodesniffer-composer-installer": true }, "sort-packages": true }, @@ -78,8 +81,8 @@ "@infection" ], "check-all": "@check", - "cs": "vendor/bin/phpcs", - "cs-fix": "vendor/bin/phpcbf", + "cs": "vendor/bin/ecs", + "cs-fix": "vendor/bin/ecs --fix", "codestyle": "@cs", "format": "@cs-fix", "infection": "vendor/bin/infection", diff --git a/ecs.php b/ecs.php new file mode 100644 index 0000000..ebcff99 --- /dev/null +++ b/ecs.php @@ -0,0 +1,92 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + + ->withPreparedSets( + psr12: true, + cleanCode: true, + ) + ->withSkip([ + FinalClassFixer::class => [ + __DIR__ . '/tests/TestCase.php', + __DIR__ . '/src/Exceptions/GitHashException.php', + ], + ]) + ->withConfiguredRule(UnusedUsesSniff::class, [ + 'searchAnnotations' => true, + ]) + ->withConfiguredRule(ForbiddenFunctionsSniff::class, [ + 'forbiddenFunctions' => [ + 'sizeof' => 'count', //keep the default option + 'delete' => 'unset', //keep the default option + 'dump' => null, //debug statement + 'dd' => null, //debug statement + 'var_dump' => null, //debug statement + 'print_r' => null, //debug statement + 'exit' => null, + ], + ]) + ->withConfiguredRule(ClassStructureSniff::class, [ + 'groups' => [ + 'uses', + 'constants', + 'enum cases', + 'properties', + 'constructor', + 'static constructors', + 'destructor', + 'magic methods', + 'all public methods', + 'all protected methods', + 'all private methods', + ], + ]) + ->withConfiguredRule(ReferenceUsedNamesOnlySniff::class, [ + 'searchAnnotations' => true, + 'allowFullyQualifiedNameForCollidingClasses' => true, + ]) + ->withRules([ + ArrayIndentSniff::class, + SpaceAfterCastSniff::class, + StaticClosureSniff::class, + DisallowSuperGlobalVariableSniff::class, + ClassConstantVisibilitySniff::class, + DeclareStrictTypesFixer::class, + NoExtraBlankLinesFixer::class, + FinalClassFixer::class, + MethodSpacingSniff::class, + UselessAliasSniff::class, + EmptyCommentSniff::class, + UseSpacingSniff::class, + StrictCallSniff::class, + PhpUnitConstructFixer::class, + PhpUnitDedicateAssertFixer::class, + ]) + ; diff --git a/phpcs.xml.dist b/phpcs.xml.dist deleted file mode 100644 index d04f9e7..0000000 --- a/phpcs.xml.dist +++ /dev/null @@ -1,15 +0,0 @@ - - - The coding standard for GitHash. - ./src/ - ./tests/ - - - - - - - - - - diff --git a/src/Contracts/FinderFactory.php b/src/Contracts/FinderFactory.php index fe199a0..34e7208 100644 --- a/src/Contracts/FinderFactory.php +++ b/src/Contracts/FinderFactory.php @@ -7,6 +7,8 @@ interface FinderFactory { public function register(GitHashFinder $finder): void; + public function registerDefaultFinders(): void; + public function getRegisteredFinders(): array; } diff --git a/src/Exceptions/FindHashException.php b/src/Exceptions/FindHashException.php index 8831305..dcf47b8 100644 --- a/src/Exceptions/FindHashException.php +++ b/src/Exceptions/FindHashException.php @@ -4,6 +4,6 @@ namespace TJVB\GitHash\Exceptions; -class FindHashException extends GitHashException +final class FindHashException extends GitHashException { } diff --git a/src/Factories/GitHashFinderFactory.php b/src/Factories/GitHashFinderFactory.php index 5ff1a30..848eaf8 100644 --- a/src/Factories/GitHashFinderFactory.php +++ b/src/Factories/GitHashFinderFactory.php @@ -14,6 +14,13 @@ final class GitHashFinderFactory implements FinderFactory { private array $finders = []; + public static function withDefaultFinders(): GitHashFinderFactory + { + $factory = new self(); + $factory->registerDefaultFinders(); + return $factory; + } + public function register(GitHashFinder $finder): void { $this->finders[] = $finder; @@ -30,11 +37,4 @@ public function getRegisteredFinders(): array { return $this->finders; } - - public static function withDefaultFinders(): GitHashFinderFactory - { - $factory = new self(); - $factory->registerDefaultFinders(); - return $factory; - } } diff --git a/src/HashFinders/GitFileSystemHashFinder.php b/src/HashFinders/GitFileSystemHashFinder.php index 280e12c..0442af5 100644 --- a/src/HashFinders/GitFileSystemHashFinder.php +++ b/src/HashFinders/GitFileSystemHashFinder.php @@ -4,7 +4,6 @@ namespace TJVB\GitHash\HashFinders; -use Exception; use TJVB\GitHash\Contracts\GitHashFinder; use TJVB\GitHash\Exceptions\GitHashException; use TJVB\GitHash\Values\GitHash; diff --git a/src/HashFinders/GitShellExecCommandHashFinder.php b/src/HashFinders/GitShellExecCommandHashFinder.php index 058930c..99c9d40 100644 --- a/src/HashFinders/GitShellExecCommandHashFinder.php +++ b/src/HashFinders/GitShellExecCommandHashFinder.php @@ -33,6 +33,6 @@ public function isAvailable(): bool } // shell_exec is a function that is more then once disabled on system. $disabled = explode(',', ini_get('disable_functions')); - return !in_array('shell_exec', $disabled); + return !in_array('shell_exec', $disabled, true); } } diff --git a/src/Retrievers/Retriever.php b/src/Retrievers/Retriever.php index b1cc187..e65b41a 100644 --- a/src/Retrievers/Retriever.php +++ b/src/Retrievers/Retriever.php @@ -13,6 +13,13 @@ final class Retriever implements GitHashRetriever { private ?FinderFactory $finderFactory = null; + public static function getWithFactory(FinderFactory $finderFactory): Retriever + { + $retriever = new self(); + $retriever->setFinderFactory($finderFactory); + return $retriever; + } + public function setFinderFactory(FinderFactory $finderFactory): void { $this->finderFactory = $finderFactory; @@ -36,13 +43,6 @@ public function getHash(string $path): GitHash throw new GitHashException('No finder available'); } - public static function getWithFactory(FinderFactory $finderFactory): Retriever - { - $retriever = new self(); - $retriever->setFinderFactory($finderFactory); - return $retriever; - } - public function getHashAndIgnoreFailures(string $path): GitHash { if ($this->finderFactory === null) { diff --git a/tests/HashFinders/GitProcessCommandHashFinderTest.php b/tests/HashFinders/GitProcessCommandHashFinderTest.php index 5ad11fa..01b488b 100644 --- a/tests/HashFinders/GitProcessCommandHashFinderTest.php +++ b/tests/HashFinders/GitProcessCommandHashFinderTest.php @@ -5,7 +5,6 @@ namespace Tjvb\GitHash\Tests\HashFinders; use TJVB\GitHash\Exceptions\GitHashException; -use TJVB\GitHash\HashFinders\GitFileSystemHashFinder; use TJVB\GitHash\HashFinders\GitProcessCommandHashFinder; use TJVB\GitHash\Tests\TestCase; use TJVB\GitHash\Values\GitHash;