Skip to content

Commit

Permalink
Switch the codestyle config from phpcs to ecs
Browse files Browse the repository at this point in the history
  • Loading branch information
tvbeek committed Aug 15, 2024
1 parent 1b9d0a8 commit c048855
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 41 deletions.
8 changes: 4 additions & 4 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -61,7 +63,8 @@
},
"config": {
"allow-plugins": {
"infection/extension-installer": true
"infection/extension-installer": true,
"dealerdirect/phpcodesniffer-composer-installer": true
},
"sort-packages": true
},
Expand All @@ -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",
Expand Down
92 changes: 92 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

use PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\ArrayIndentSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterCastSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
use PhpCsFixer\Fixer\ClassNotation\FinalClassFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitConstructFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertFixer;
use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer;
use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer;
use SlevomatCodingStandard\Sniffs\Classes\ClassConstantVisibilitySniff;
use SlevomatCodingStandard\Sniffs\Classes\ClassStructureSniff;
use SlevomatCodingStandard\Sniffs\Classes\MethodSpacingSniff;
use SlevomatCodingStandard\Sniffs\Commenting\EmptyCommentSniff;
use SlevomatCodingStandard\Sniffs\Functions\StaticClosureSniff;
use SlevomatCodingStandard\Sniffs\Functions\StrictCallSniff;
use SlevomatCodingStandard\Sniffs\Namespaces\ReferenceUsedNamesOnlySniff;
use SlevomatCodingStandard\Sniffs\Namespaces\UnusedUsesSniff;
use SlevomatCodingStandard\Sniffs\Namespaces\UselessAliasSniff;
use SlevomatCodingStandard\Sniffs\Namespaces\UseSpacingSniff;
use SlevomatCodingStandard\Sniffs\Variables\DisallowSuperGlobalVariableSniff;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->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,
])
;
15 changes: 0 additions & 15 deletions phpcs.xml.dist

This file was deleted.

2 changes: 2 additions & 0 deletions src/Contracts/FinderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
interface FinderFactory
{
public function register(GitHashFinder $finder): void;

public function registerDefaultFinders(): void;

public function getRegisteredFinders(): array;
}
2 changes: 1 addition & 1 deletion src/Exceptions/FindHashException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace TJVB\GitHash\Exceptions;

class FindHashException extends GitHashException
final class FindHashException extends GitHashException
{
}
14 changes: 7 additions & 7 deletions src/Factories/GitHashFinderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,11 +37,4 @@ public function getRegisteredFinders(): array
{
return $this->finders;
}

public static function withDefaultFinders(): GitHashFinderFactory
{
$factory = new self();
$factory->registerDefaultFinders();
return $factory;
}
}
1 change: 0 additions & 1 deletion src/HashFinders/GitFileSystemHashFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/HashFinders/GitShellExecCommandHashFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 7 additions & 7 deletions src/Retrievers/Retriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion tests/HashFinders/GitProcessCommandHashFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c048855

Please sign in to comment.