Skip to content

Commit c256c9c

Browse files
authored
Replace squizlabs/php_codesniffer with symplify/easy-coding-standard (#60)
* Replace squizlabs/php_codesniffer with symplify/easy-coding-standard * Fixed code style issues with enhanced symplify/easy-coding-standard config * Fixed issues with strict type added by the code style
1 parent 86b7058 commit c256c9c

39 files changed

+131
-236
lines changed

.github/workflows/tests.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ jobs:
8686
- name: "Install dependencies with composer"
8787
run: composer update --no-interaction --no-progress
8888

89-
- name: "Run checkstyle with squizlabs/php_codesniffer"
90-
run: vendor/bin/phpcs
89+
- name: "Run checkstyle with symplify/easy-coding-standard"
90+
run: vendor/bin/ecs

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
"doctrine/annotations": "^1.14",
1818
"myclabs/php-enum": "^1.8",
1919
"phpunit/phpunit": "^9.6",
20-
"squizlabs/php_codesniffer": "^3.10",
2120
"symfony/form": "^7.0",
2221
"symfony/http-kernel": "^7.0",
2322
"symfony/translation": "^7.0",
2423
"symfony/twig-bundle": "^7.0",
2524
"symfony/validator": "^7.0",
2625
"symfony/yaml": "^7.0",
26+
"symplify/easy-coding-standard": "^12.3",
2727
"twig/twig": "^2.0|^3.0"
2828
},
2929
"suggest": {

ecs.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
6+
use PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer;
7+
use PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer;
8+
use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer;
9+
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayListItemNewlineFixer;
10+
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayOpenerAndCloserNewlineFixer;
11+
use Symplify\CodingStandard\Fixer\ArrayNotation\StandaloneLineInMultilineArrayFixer;
12+
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
13+
use Symplify\EasyCodingStandard\Config\ECSConfig;
14+
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
15+
16+
return function (ECSConfig $ecsConfig): void {
17+
$ecsConfig->paths([
18+
__DIR__ . '/src',
19+
__DIR__ . '/tests/Integration/src',
20+
__DIR__ . '/tests/Integration/tests',
21+
__DIR__ . '/tests/Unit',
22+
]);
23+
24+
$ecsConfig->sets([
25+
SetList::ARRAY,
26+
SetList::DOCBLOCK,
27+
SetList::NAMESPACES,
28+
SetList::COMMENTS,
29+
SetList::STRICT,
30+
SetList::PSR_12,
31+
]);
32+
33+
$ecsConfig->skip([
34+
/* Do not force array on multiple lines : ['foo' => $foo, 'bar' => $bar] */
35+
ArrayOpenerAndCloserNewlineFixer::class,
36+
ArrayListItemNewlineFixer::class,
37+
StandaloneLineInMultilineArrayFixer::class,
38+
]);
39+
40+
$ecsConfig->ruleWithConfiguration(YodaStyleFixer::class, [
41+
'equal' => false,
42+
'identical' => false,
43+
'less_and_greater' => false,
44+
]);
45+
$ecsConfig->ruleWithConfiguration(LineLengthFixer::class, [
46+
LineLengthFixer::INLINE_SHORT_LINES => false,
47+
]);
48+
$ecsConfig->ruleWithConfiguration(ForbiddenFunctionsSniff::class, [
49+
'forbiddenFunctions' => ['dump' => null, 'dd' => null, 'var_dump' => null, 'die' => null],
50+
]);
51+
$ecsConfig->ruleWithConfiguration(FunctionDeclarationFixer::class, [
52+
'closure_fn_spacing' => 'none',
53+
]);
54+
$ecsConfig->ruleWithConfiguration(NativeFunctionInvocationFixer::class, [
55+
'scope' => 'namespaced',
56+
'include' => ['@all'],
57+
]);
58+
};

phpcs.xml.dist

-25
This file was deleted.

src/ConstantExtractor.php

+14-14
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
final class ConstantExtractor
1717
{
1818
/**
19-
* @param string $pattern
20-
*
21-
* @return array
2219
* @throws LogicException
2320
*/
2421
public static function extract(string $pattern): array
@@ -34,21 +31,21 @@ public static function extract(string $pattern): array
3431

3532
private static function filter(array $constants, string $regexp, string $pattern): array
3633
{
37-
$matchingNames = preg_grep($regexp, array_keys($constants));
34+
$matchingNames = \preg_grep($regexp, \array_keys($constants));
3835

39-
if (count($matchingNames) === 0) {
36+
if (\count($matchingNames) === 0) {
4037
throw LogicException::cannotExtractConstants($pattern, 'Pattern matches no constant.');
4138
}
4239

43-
return array_values(array_intersect_key($constants, array_flip($matchingNames)));
40+
return \array_values(\array_intersect_key($constants, \array_flip($matchingNames)));
4441
}
4542

4643
private static function publicConstants(string $class, string $pattern): array
4744
{
4845
try {
4946
$constants = (new ReflectionClass($class))->getReflectionConstants();
5047
} catch (ReflectionException $exception) {
51-
throw LogicException::cannotExtractConstants($pattern, sprintf('Class %s does not exists.', $class));
48+
throw LogicException::cannotExtractConstants($pattern, \sprintf('Class %s does not exists.', $class));
5249
}
5350

5451
$list = [];
@@ -60,34 +57,37 @@ private static function publicConstants(string $class, string $pattern): array
6057
$list[$constant->getName()] = $constant->getValue();
6158
}
6259

63-
if (count($list) === 0) {
64-
throw LogicException::cannotExtractConstants($pattern, sprintf('Class %s has no public constant.', $class));
60+
if (\count($list) === 0) {
61+
throw LogicException::cannotExtractConstants(
62+
$pattern,
63+
\sprintf('Class %s has no public constant.', $class)
64+
);
6565
}
6666

6767
return $list;
6868
}
6969

7070
private static function explode(string $pattern): array
7171
{
72-
if (substr_count($pattern, '::') !== 1) {
72+
if (\substr_count($pattern, '::') !== 1) {
7373
throw LogicException::cannotExtractConstants(
7474
$pattern,
7575
'Pattern must look like Fully\\Qualified\\ClassName::CONSTANT_*.'
7676
);
7777
}
7878

79-
[$class, $constantsNamePattern] = explode('::', $pattern);
79+
[$class, $constantsNamePattern] = \explode('::', $pattern);
8080

81-
if (substr_count($constantsNamePattern, '*') === 0) {
81+
if (\substr_count($constantsNamePattern, '*') === 0) {
8282
throw LogicException::cannotExtractConstants(
8383
$pattern,
8484
'Pattern must look like Fully\\Qualified\\ClassName::CONSTANT_*.'
8585
);
8686
}
8787

88-
$constantsNameRegexp = sprintf(
88+
$constantsNameRegexp = \sprintf(
8989
'#^%s$#',
90-
str_replace('*', '[0-9a-zA-Z_]+', $constantsNamePattern)
90+
\str_replace('*', '[0-9a-zA-Z_]+', $constantsNamePattern)
9191
);
9292

9393
return [$class, $constantsNameRegexp];

src/ConstantListEnum.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99
*/
1010
class ConstantListEnum extends Enum
1111
{
12-
/**
13-
* @param string $constantsPattern
14-
* @param string|null $name
15-
*/
1612
public function __construct(string $constantsPattern, ?string $name = null)
1713
{
1814
$values = ConstantExtractor::extract($constantsPattern);
19-
parent::__construct(array_combine($values, $values), $name);
15+
parent::__construct(\array_combine($values, $values), $name);
2016
}
2117
}

src/ConstantListTranslatedEnum.php

-7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111
*/
1212
class ConstantListTranslatedEnum extends TranslatedEnum
1313
{
14-
/**
15-
* @param string $constantsPattern
16-
* @param TranslatorInterface $translator
17-
* @param string $transPattern
18-
* @param string $transDomain
19-
* @param string|null $name
20-
*/
2114
public function __construct(
2215
string $constantsPattern,
2316
TranslatorInterface $translator,

src/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPass.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
*/
1414
final class TaggedEnumCollectorCompilerPass implements CompilerPassInterface
1515
{
16-
/**
17-
* @inheritdoc
18-
*/
1916
public function process(ContainerBuilder $container): void
2017
{
2118
if (!$container->hasDefinition('yokai_enum.enum_registry')) {
@@ -24,7 +21,7 @@ public function process(ContainerBuilder $container): void
2421

2522
$registry = $container->getDefinition('yokai_enum.enum_registry');
2623

27-
foreach (array_keys($container->findTaggedServiceIds('yokai_enum.enum')) as $enum) {
24+
foreach (\array_keys($container->findTaggedServiceIds('yokai_enum.enum')) as $enum) {
2825
$registry->addMethodCall('add', [new Reference($enum)]);
2926
}
3027
}

src/DependencyInjection/EnumExtension.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,16 @@
2222
*/
2323
final class EnumExtension extends Extension
2424
{
25-
/**
26-
* @inheritdoc
27-
*/
2825
public function load(array $configs, ContainerBuilder $container): void
2926
{
3027
$container->register('yokai_enum.enum_registry', EnumRegistry::class);
3128
$container->setAlias(EnumRegistry::class, 'yokai_enum.enum_registry');
3229

3330
$registry = new Reference(EnumRegistry::class);
3431

35-
$requiresForm = interface_exists(FormInterface::class);
36-
$requiresValidator = interface_exists(ValidatorInterface::class);
37-
$requiresTwig = class_exists(TwigBundle::class);
32+
$requiresForm = \interface_exists(FormInterface::class);
33+
$requiresValidator = \interface_exists(ValidatorInterface::class);
34+
$requiresTwig = \class_exists(TwigBundle::class);
3835

3936
if ($requiresForm) {
4037
$container->register('yokai_enum.form_type.enum_type', EnumType::class)

src/Enum.php

+2-14
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Enum implements EnumInterface
3030
*/
3131
public function __construct(?array $choices, ?string $name = null)
3232
{
33-
if (__CLASS__ === static::class && $choices === null) {
33+
if (static::class === __CLASS__ && $choices === null) {
3434
throw new LogicException(
3535
'When using ' . __CLASS__ . ' directly, $choices argument in ' . __FUNCTION__ . ' method cannot be null'
3636
);
@@ -53,44 +53,32 @@ public function __construct(?array $choices, ?string $name = null)
5353
$this->name = $name;
5454
}
5555

56-
/**
57-
* @inheritDoc
58-
*/
5956
public function getChoices(): array
6057
{
6158
$this->init();
6259

6360
return $this->choices;
6461
}
6562

66-
/**
67-
* @inheritDoc
68-
*/
6963
public function getValues(): array
7064
{
7165
$this->init();
7266

7367
return \array_values($this->choices);
7468
}
7569

76-
/**
77-
* @inheritDoc
78-
*/
7970
public function getLabel($value): string
8071
{
8172
$this->init();
8273

83-
$label = \array_search($value, $this->choices);
74+
$label = \array_search($value, $this->choices, false);
8475
if ($label === false) {
8576
throw InvalidArgumentException::enumMissingValue($this, $value);
8677
}
8778

8879
return $label;
8980
}
9081

91-
/**
92-
* @inheritDoc
93-
*/
9482
public function getName(): string
9583
{
9684
return $this->name;

src/EnumInterface.php

-4
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,11 @@ public function getValues(): array;
2727
* Returns enum value label.
2828
*
2929
* @param mixed $value
30-
*
31-
* @return string
3230
*/
3331
public function getLabel($value): string;
3432

3533
/**
3634
* Returns enum identifier (must be unique across app).
37-
*
38-
* @return string
3935
*/
4036
public function getName(): string;
4137
}

src/EnumRegistry.php

-10
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ final class EnumRegistry
1818
private $enums = [];
1919

2020
/**
21-
* @param EnumInterface $enum
22-
*
2321
* @throws LogicException
2422
*/
2523
public function add(EnumInterface $enum): void
@@ -32,9 +30,6 @@ public function add(EnumInterface $enum): void
3230
}
3331

3432
/**
35-
* @param string $name
36-
*
37-
* @return EnumInterface
3833
* @throws InvalidArgumentException
3934
*/
4035
public function get(string $name): EnumInterface
@@ -46,11 +41,6 @@ public function get(string $name): EnumInterface
4641
return $this->enums[$name];
4742
}
4843

49-
/**
50-
* @param string $name
51-
*
52-
* @return bool
53-
*/
5444
public function has(string $name): bool
5545
{
5646
return isset($this->enums[$name]);

src/Exception/InvalidArgumentException.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@ final class InvalidArgumentException extends \InvalidArgumentException implement
1313
{
1414
public static function unregisteredEnum(string $name): self
1515
{
16-
return new self(sprintf(
16+
return new self(\sprintf(
1717
'Enum with name "%s" was not registered in registry',
1818
$name
1919
));
2020
}
2121

22-
public static function enumMissingValue(EnumInterface $enum, string $value): self
22+
public static function enumMissingValue(EnumInterface $enum, mixed $value): self
2323
{
24-
return new self(sprintf(
24+
if (\is_object($value) && \method_exists($value, '__toString')) {
25+
$value = (string)$value;
26+
}
27+
if (!\is_string($value)) {
28+
$value = \get_debug_type($value);
29+
}
30+
31+
return new self(\sprintf(
2532
'Enum "%s" does not have "%s" value.',
2633
$enum->getName(),
2734
$value

0 commit comments

Comments
 (0)