Skip to content

Commit

Permalink
Replace squizlabs/php_codesniffer with symplify/easy-coding-standard (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
yann-eugone authored Jul 18, 2024
1 parent 86b7058 commit c256c9c
Show file tree
Hide file tree
Showing 39 changed files with 131 additions and 236 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ jobs:
- name: "Install dependencies with composer"
run: composer update --no-interaction --no-progress

- name: "Run checkstyle with squizlabs/php_codesniffer"
run: vendor/bin/phpcs
- name: "Run checkstyle with symplify/easy-coding-standard"
run: vendor/bin/ecs
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
"doctrine/annotations": "^1.14",
"myclabs/php-enum": "^1.8",
"phpunit/phpunit": "^9.6",
"squizlabs/php_codesniffer": "^3.10",
"symfony/form": "^7.0",
"symfony/http-kernel": "^7.0",
"symfony/translation": "^7.0",
"symfony/twig-bundle": "^7.0",
"symfony/validator": "^7.0",
"symfony/yaml": "^7.0",
"symplify/easy-coding-standard": "^12.3",
"twig/twig": "^2.0|^3.0"
},
"suggest": {
Expand Down
58 changes: 58 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
use PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer;
use PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer;
use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer;
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayListItemNewlineFixer;
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayOpenerAndCloserNewlineFixer;
use Symplify\CodingStandard\Fixer\ArrayNotation\StandaloneLineInMultilineArrayFixer;
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests/Integration/src',
__DIR__ . '/tests/Integration/tests',
__DIR__ . '/tests/Unit',
]);

$ecsConfig->sets([
SetList::ARRAY,
SetList::DOCBLOCK,
SetList::NAMESPACES,
SetList::COMMENTS,
SetList::STRICT,
SetList::PSR_12,
]);

$ecsConfig->skip([
/* Do not force array on multiple lines : ['foo' => $foo, 'bar' => $bar] */
ArrayOpenerAndCloserNewlineFixer::class,
ArrayListItemNewlineFixer::class,
StandaloneLineInMultilineArrayFixer::class,
]);

$ecsConfig->ruleWithConfiguration(YodaStyleFixer::class, [
'equal' => false,
'identical' => false,
'less_and_greater' => false,
]);
$ecsConfig->ruleWithConfiguration(LineLengthFixer::class, [
LineLengthFixer::INLINE_SHORT_LINES => false,
]);
$ecsConfig->ruleWithConfiguration(ForbiddenFunctionsSniff::class, [
'forbiddenFunctions' => ['dump' => null, 'dd' => null, 'var_dump' => null, 'die' => null],
]);
$ecsConfig->ruleWithConfiguration(FunctionDeclarationFixer::class, [
'closure_fn_spacing' => 'none',
]);
$ecsConfig->ruleWithConfiguration(NativeFunctionInvocationFixer::class, [
'scope' => 'namespaced',
'include' => ['@all'],
]);
};
25 changes: 0 additions & 25 deletions phpcs.xml.dist

This file was deleted.

28 changes: 14 additions & 14 deletions src/ConstantExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
final class ConstantExtractor
{
/**
* @param string $pattern
*
* @return array
* @throws LogicException
*/
public static function extract(string $pattern): array
Expand All @@ -34,21 +31,21 @@ public static function extract(string $pattern): array

private static function filter(array $constants, string $regexp, string $pattern): array
{
$matchingNames = preg_grep($regexp, array_keys($constants));
$matchingNames = \preg_grep($regexp, \array_keys($constants));

if (count($matchingNames) === 0) {
if (\count($matchingNames) === 0) {
throw LogicException::cannotExtractConstants($pattern, 'Pattern matches no constant.');
}

return array_values(array_intersect_key($constants, array_flip($matchingNames)));
return \array_values(\array_intersect_key($constants, \array_flip($matchingNames)));
}

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

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

if (count($list) === 0) {
throw LogicException::cannotExtractConstants($pattern, sprintf('Class %s has no public constant.', $class));
if (\count($list) === 0) {
throw LogicException::cannotExtractConstants(
$pattern,
\sprintf('Class %s has no public constant.', $class)
);
}

return $list;
}

private static function explode(string $pattern): array
{
if (substr_count($pattern, '::') !== 1) {
if (\substr_count($pattern, '::') !== 1) {
throw LogicException::cannotExtractConstants(
$pattern,
'Pattern must look like Fully\\Qualified\\ClassName::CONSTANT_*.'
);
}

[$class, $constantsNamePattern] = explode('::', $pattern);
[$class, $constantsNamePattern] = \explode('::', $pattern);

if (substr_count($constantsNamePattern, '*') === 0) {
if (\substr_count($constantsNamePattern, '*') === 0) {
throw LogicException::cannotExtractConstants(
$pattern,
'Pattern must look like Fully\\Qualified\\ClassName::CONSTANT_*.'
);
}

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

return [$class, $constantsNameRegexp];
Expand Down
6 changes: 1 addition & 5 deletions src/ConstantListEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
*/
class ConstantListEnum extends Enum
{
/**
* @param string $constantsPattern
* @param string|null $name
*/
public function __construct(string $constantsPattern, ?string $name = null)
{
$values = ConstantExtractor::extract($constantsPattern);
parent::__construct(array_combine($values, $values), $name);
parent::__construct(\array_combine($values, $values), $name);
}
}
7 changes: 0 additions & 7 deletions src/ConstantListTranslatedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
*/
class ConstantListTranslatedEnum extends TranslatedEnum
{
/**
* @param string $constantsPattern
* @param TranslatorInterface $translator
* @param string $transPattern
* @param string $transDomain
* @param string|null $name
*/
public function __construct(
string $constantsPattern,
TranslatorInterface $translator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
*/
final class TaggedEnumCollectorCompilerPass implements CompilerPassInterface
{
/**
* @inheritdoc
*/
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition('yokai_enum.enum_registry')) {
Expand All @@ -24,7 +21,7 @@ public function process(ContainerBuilder $container): void

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

foreach (array_keys($container->findTaggedServiceIds('yokai_enum.enum')) as $enum) {
foreach (\array_keys($container->findTaggedServiceIds('yokai_enum.enum')) as $enum) {
$registry->addMethodCall('add', [new Reference($enum)]);
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/DependencyInjection/EnumExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@
*/
final class EnumExtension extends Extension
{
/**
* @inheritdoc
*/
public function load(array $configs, ContainerBuilder $container): void
{
$container->register('yokai_enum.enum_registry', EnumRegistry::class);
$container->setAlias(EnumRegistry::class, 'yokai_enum.enum_registry');

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

$requiresForm = interface_exists(FormInterface::class);
$requiresValidator = interface_exists(ValidatorInterface::class);
$requiresTwig = class_exists(TwigBundle::class);
$requiresForm = \interface_exists(FormInterface::class);
$requiresValidator = \interface_exists(ValidatorInterface::class);
$requiresTwig = \class_exists(TwigBundle::class);

if ($requiresForm) {
$container->register('yokai_enum.form_type.enum_type', EnumType::class)
Expand Down
16 changes: 2 additions & 14 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Enum implements EnumInterface
*/
public function __construct(?array $choices, ?string $name = null)
{
if (__CLASS__ === static::class && $choices === null) {
if (static::class === __CLASS__ && $choices === null) {
throw new LogicException(
'When using ' . __CLASS__ . ' directly, $choices argument in ' . __FUNCTION__ . ' method cannot be null'
);
Expand All @@ -53,44 +53,32 @@ public function __construct(?array $choices, ?string $name = null)
$this->name = $name;
}

/**
* @inheritDoc
*/
public function getChoices(): array
{
$this->init();

return $this->choices;
}

/**
* @inheritDoc
*/
public function getValues(): array
{
$this->init();

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

/**
* @inheritDoc
*/
public function getLabel($value): string
{
$this->init();

$label = \array_search($value, $this->choices);
$label = \array_search($value, $this->choices, false);
if ($label === false) {
throw InvalidArgumentException::enumMissingValue($this, $value);
}

return $label;
}

/**
* @inheritDoc
*/
public function getName(): string
{
return $this->name;
Expand Down
4 changes: 0 additions & 4 deletions src/EnumInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ public function getValues(): array;
* Returns enum value label.
*
* @param mixed $value
*
* @return string
*/
public function getLabel($value): string;

/**
* Returns enum identifier (must be unique across app).
*
* @return string
*/
public function getName(): string;
}
10 changes: 0 additions & 10 deletions src/EnumRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ final class EnumRegistry
private $enums = [];

/**
* @param EnumInterface $enum
*
* @throws LogicException
*/
public function add(EnumInterface $enum): void
Expand All @@ -32,9 +30,6 @@ public function add(EnumInterface $enum): void
}

/**
* @param string $name
*
* @return EnumInterface
* @throws InvalidArgumentException
*/
public function get(string $name): EnumInterface
Expand All @@ -46,11 +41,6 @@ public function get(string $name): EnumInterface
return $this->enums[$name];
}

/**
* @param string $name
*
* @return bool
*/
public function has(string $name): bool
{
return isset($this->enums[$name]);
Expand Down
13 changes: 10 additions & 3 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ final class InvalidArgumentException extends \InvalidArgumentException implement
{
public static function unregisteredEnum(string $name): self
{
return new self(sprintf(
return new self(\sprintf(
'Enum with name "%s" was not registered in registry',
$name
));
}

public static function enumMissingValue(EnumInterface $enum, string $value): self
public static function enumMissingValue(EnumInterface $enum, mixed $value): self
{
return new self(sprintf(
if (\is_object($value) && \method_exists($value, '__toString')) {
$value = (string)$value;
}
if (!\is_string($value)) {
$value = \get_debug_type($value);
}

return new self(\sprintf(
'Enum "%s" does not have "%s" value.',
$enum->getName(),
$value
Expand Down
Loading

0 comments on commit c256c9c

Please sign in to comment.