Skip to content

Commit ed9bf6f

Browse files
authored
Use php-cs-fixer (#323)
1 parent 278b58b commit ed9bf6f

File tree

71 files changed

+436
-494
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+436
-494
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ tests export-ignore
77
.editorconfig export-ignore
88
.gitattributes export-ignore
99
.gitignore export-ignore
10+
.php-cs-fixer.php
1011
CONTRIBUTING.md export-ignore
1112
enum-library.md export-ignore
1213
phpunit.xml export-ignore

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ composer.lock
44
# Dependencies
55
vendor
66

7-
# PHPUnit
7+
# Cache
8+
.php-cs-fixer.cache
89
.phpunit.result.cache
910

1011
# IDE stuff

.php-cs-fixer.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
use function MLL\PhpCsFixerConfig\risky;
4+
5+
$finder = PhpCsFixer\Finder::create()
6+
->in(__DIR__)
7+
->name('*.php')
8+
->notPath('vendor')
9+
->notPath('tests/Enums/AnnotateFixtures') // Matches laminas/laminas-code
10+
->notPath('tests/Enums/ToNativeFixtures') // Matches laminas/laminas-code
11+
->ignoreDotFiles(false)
12+
->ignoreVCS(true);
13+
14+
return risky($finder);

CONTRIBUTING.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,29 @@ back is to write a failing test for it and then make it pass. If you can
2525
not figure out how to fix it yourself, feel free to submit a PR with a
2626
failing test.
2727

28-
Run the testsuite
28+
Run the testsuite:
2929

30-
```bash
30+
```sh
3131
composer test
3232
```
3333

34+
## Codestyle
35+
36+
Formatting is automated through [php-cs-fixer](https://github.com/friendsofphp/php-cs-fixer).
37+
38+
Apply automated fixes:
39+
40+
```bash
41+
composer fix
42+
```
43+
44+
3445
## Static Analysis
3546

3647
We use [PHPStan](https://phpstan.org) for static analysis.
3748

38-
Run static analysis
49+
Run static analysis:
3950

40-
```bash
51+
```sh
4152
composer stan
4253
```

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"require-dev": {
3030
"doctrine/dbal": "^3.4",
3131
"ergebnis/composer-normalize": "^2.28.3",
32+
"mll-lab/php-cs-fixer-config": "^5.4",
3233
"mockery/mockery": "^1.5",
3334
"nunomaduro/larastan": "^2.1.12",
3435
"orchestra/testbench": "^7.6.1 || ^8",
@@ -73,9 +74,11 @@
7374
],
7475
"all": [
7576
"composer normalize",
77+
"@fix",
7678
"@test",
7779
"@stan"
7880
],
81+
"fix": "php-cs-fixer fix",
7982
"stan": "phpstan",
8083
"test": "phpunit"
8184
}

src/Attributes/Description.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
use Attribute;
66

7-
#[Attribute(Attribute::TARGET_CLASS_CONSTANT | Attribute::TARGET_CLASS)]
7+
#[\Attribute(\Attribute::TARGET_CLASS_CONSTANT | \Attribute::TARGET_CLASS)]
88
class Description
99
{
1010
public function __construct(
1111
public string $description,
12-
) {
13-
}
12+
) {}
1413
}

src/Casts/EnumCast.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ class EnumCast implements CastsAttributes
1313
{
1414
public function __construct(
1515
protected string $enumClass
16-
) {
17-
}
16+
) {}
1817

1918
/**
2019
* @template TValue
20+
*
2121
* @param TValue $value
2222
* @param array<string, mixed> $attributes
23+
*
2324
* @return Enum<TValue>|null
2425
*/
2526
public function get($model, string $key, $value, array $attributes): ?Enum
@@ -29,6 +30,7 @@ public function get($model, string $key, $value, array $attributes): ?Enum
2930

3031
/**
3132
* @param array<string, mixed> $attributes
33+
*
3234
* @return array<string, mixed>
3335
*/
3436
public function set($model, string $key, $value, array $attributes): array
@@ -40,7 +42,9 @@ public function set($model, string $key, $value, array $attributes): array
4042

4143
/**
4244
* @template TValue
45+
*
4346
* @param TValue $value
47+
*
4448
* @return Enum<TValue>|null
4549
*/
4650
protected function castEnum(mixed $value): ?Enum

src/Commands/EnumAnnotateCommand.php

+17-27
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
namespace BenSampo\Enum\Commands;
44

55
use BenSampo\Enum\Enum;
6-
use Laminas\Code\Generator\DocBlock\Tag\MethodTag;
7-
use Laminas\Code\Generator\DocBlock\Tag\TagInterface;
8-
use ReflectionClass;
9-
use InvalidArgumentException;
6+
use Composer\ClassMapGenerator\ClassMapGenerator;
107
use Illuminate\Console\Command;
118
use Illuminate\Filesystem\Filesystem;
12-
use Composer\ClassMapGenerator\ClassMapGenerator;
9+
use Laminas\Code\Generator\DocBlock\Tag\MethodTag;
10+
use Laminas\Code\Generator\DocBlock\Tag\TagInterface;
1311
use Laminas\Code\Generator\DocBlockGenerator;
1412
use Laminas\Code\Reflection\DocBlockReflection;
15-
use Symfony\Component\Console\Input\InputOption;
1613
use Symfony\Component\Console\Input\InputArgument;
14+
use Symfony\Component\Console\Input\InputOption;
1715

1816
class EnumAnnotateCommand extends Command
1917
{
@@ -23,19 +21,15 @@ class EnumAnnotateCommand extends Command
2321

2422
protected Filesystem $filesystem;
2523

26-
/**
27-
* @return array<int, array<int, mixed>>
28-
*/
24+
/** @return array<int, array<int, mixed>> */
2925
protected function getArguments(): array
3026
{
3127
return [
3228
['class', InputArgument::OPTIONAL, 'The class name to generate annotations for'],
3329
];
3430
}
3531

36-
/**
37-
* @return array<int, array<int, mixed>>
38-
*/
32+
/** @return array<int, array<int, mixed>> */
3933
protected function getOptions(): array
4034
{
4135
return [
@@ -61,30 +55,28 @@ public function handle(Filesystem $filesystem): int
6155

6256
protected function annotateClass(string $className): void
6357
{
64-
if (!is_subclass_of($className, Enum::class)) {
58+
if (! is_subclass_of($className, Enum::class)) {
6559
$parentClass = Enum::class;
66-
throw new InvalidArgumentException("The given class {$className} must be an instance of {$parentClass}.");
60+
throw new \InvalidArgumentException("The given class {$className} must be an instance of {$parentClass}.");
6761
}
6862

69-
$reflection = new ReflectionClass($className);
63+
$reflection = new \ReflectionClass($className);
7064
$this->annotate($reflection);
7165
}
7266

7367
protected function annotateFolder(): void
7468
{
7569
foreach (ClassMapGenerator::createMap($this->searchDirectory()) as $class => $_) {
76-
$reflection = new ReflectionClass($class);
70+
$reflection = new \ReflectionClass($class);
7771

7872
if ($reflection->isSubclassOf(Enum::class)) {
7973
$this->annotate($reflection);
8074
}
8175
}
8276
}
8377

84-
/**
85-
* @param \ReflectionClass<\BenSampo\Enum\Enum<mixed>> $reflectionClass
86-
*/
87-
protected function annotate(ReflectionClass $reflectionClass): void
78+
/** @param \ReflectionClass<\BenSampo\Enum\Enum<mixed>> $reflectionClass */
79+
protected function annotate(\ReflectionClass $reflectionClass): void
8880
{
8981
$docBlock = $this->getDocBlock($reflectionClass);
9082
$shortName = $reflectionClass->getShortName();
@@ -119,10 +111,8 @@ protected function annotate(ReflectionClass $reflectionClass): void
119111
$this->info("Wrote new phpDocBlock to {$fileName}.");
120112
}
121113

122-
/**
123-
* @param \ReflectionClass<\BenSampo\Enum\Enum<mixed>> $reflectionClass
124-
*/
125-
protected function getDocBlock(ReflectionClass $reflectionClass): DocBlockGenerator
114+
/** @param \ReflectionClass<\BenSampo\Enum\Enum<mixed>> $reflectionClass */
115+
protected function getDocBlock(\ReflectionClass $reflectionClass): DocBlockGenerator
126116
{
127117
$docBlock = DocBlockGenerator::fromArray([])
128118
->setWordWrap(false);
@@ -156,9 +146,10 @@ protected function getDocblockWithoutTags(DocBlockReflection $docBlockReflection
156146

157147
/**
158148
* @param \ReflectionClass<\BenSampo\Enum\Enum<mixed>> $reflectionClass
149+
*
159150
* @return array<\Laminas\Code\Generator\DocBlock\Tag\TagInterface>
160151
*/
161-
protected function getDocblockTags(DocBlockGenerator|null $originalDocblock, ReflectionClass $reflectionClass): array
152+
protected function getDocblockTags(DocBlockGenerator|null $originalDocblock, \ReflectionClass $reflectionClass): array
162153
{
163154
$constants = $reflectionClass->getConstants();
164155
$constantKeys = array_keys($constants);
@@ -172,8 +163,7 @@ protected function getDocblockTags(DocBlockGenerator|null $originalDocblock, Ref
172163
if ($originalDocblock) {
173164
$tags = array_merge(
174165
$tags,
175-
array_filter($originalDocblock->getTags(), fn (TagInterface $tag): bool =>
176-
! $tag instanceof MethodTag
166+
array_filter($originalDocblock->getTags(), fn (TagInterface $tag): bool => ! $tag instanceof MethodTag
177167
|| ! in_array($tag->getMethodName(), $constantKeys, true))
178168
);
179169
}

src/Commands/EnumToNativeCommand.php

+11-19
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
namespace BenSampo\Enum\Commands;
44

55
use BenSampo\Enum\Enum;
6-
use Laminas\Code\Generator\EnumGenerator\EnumGenerator;
7-
use ReflectionClass;
8-
use InvalidArgumentException;
6+
use Composer\ClassMapGenerator\ClassMapGenerator;
97
use Illuminate\Console\Command;
108
use Illuminate\Filesystem\Filesystem;
11-
use Composer\ClassMapGenerator\ClassMapGenerator;
12-
use Symfony\Component\Console\Input\InputOption;
9+
use Laminas\Code\Generator\EnumGenerator\EnumGenerator;
1310
use Symfony\Component\Console\Input\InputArgument;
11+
use Symfony\Component\Console\Input\InputOption;
1412

1513
class EnumToNativeCommand extends Command
1614
{
@@ -20,19 +18,15 @@ class EnumToNativeCommand extends Command
2018

2119
protected Filesystem $filesystem;
2220

23-
/**
24-
* @return array<int, array<int, mixed>>
25-
*/
21+
/** @return array<int, array<int, mixed>> */
2622
protected function getArguments(): array
2723
{
2824
return [
2925
['class', InputArgument::OPTIONAL, 'The class name to convert'],
3026
];
3127
}
3228

33-
/**
34-
* @return array<int, array<int, mixed>>
35-
*/
29+
/** @return array<int, array<int, mixed>> */
3630
protected function getOptions(): array
3731
{
3832
return [
@@ -58,29 +52,27 @@ public function handle(Filesystem $filesystem): int
5852

5953
protected function annotateClass(string $className): void
6054
{
61-
if (!is_subclass_of($className, Enum::class)) {
55+
if (! is_subclass_of($className, Enum::class)) {
6256
$parentClass = Enum::class;
63-
throw new InvalidArgumentException("The given class {$className} must be an instance of {$parentClass}.");
57+
throw new \InvalidArgumentException("The given class {$className} must be an instance of {$parentClass}.");
6458
}
6559

66-
$this->convert(new ReflectionClass($className));
60+
$this->convert(new \ReflectionClass($className));
6761
}
6862

6963
protected function convertFolder(): void
7064
{
7165
foreach (ClassMapGenerator::createMap($this->searchDirectory()) as $class => $_) {
72-
$reflection = new ReflectionClass($class);
66+
$reflection = new \ReflectionClass($class);
7367

7468
if ($reflection->isSubclassOf(Enum::class)) {
7569
$this->convert($reflection);
7670
}
7771
}
7872
}
7973

80-
/**
81-
* @param \ReflectionClass<\BenSampo\Enum\Enum<mixed>> $reflectionClass
82-
*/
83-
protected function convert(ReflectionClass $reflectionClass): void
74+
/** @param \ReflectionClass<\BenSampo\Enum\Enum<mixed>> $reflectionClass */
75+
protected function convert(\ReflectionClass $reflectionClass): void
8476
{
8577
$type = null;
8678
$constants = $reflectionClass->getConstants();

src/Commands/MakeEnumCommand.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ protected function getDefaultNamespace($rootNamespace): string
3232
return "{$rootNamespace}\Enums";
3333
}
3434

35-
/**
36-
* @return array<int, array<int, mixed>>
37-
*/
35+
/** @return array<int, array<int, mixed>> */
3836
protected function getOptions(): array
3937
{
4038
return [

src/Contracts/EnumContract.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
interface EnumContract
66
{
7-
/**
8-
* Determine if this instance is equivalent to a given value.
9-
*/
7+
/** Determine if this instance is equivalent to a given value. */
108
public function is(mixed $enumValue): bool;
119
}

src/Contracts/LocalizedEnum.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
interface LocalizedEnum
66
{
7-
/**
8-
* Get the default localization key.
9-
*/
7+
/** Get the default localization key. */
108
public static function getLocalizationKey(): string;
119
}

0 commit comments

Comments
 (0)