From 516e6f3158253c7c98f19479998eb804116dd576 Mon Sep 17 00:00:00 2001 From: Ion Bazan Date: Mon, 20 Jun 2022 16:34:13 +0800 Subject: [PATCH 1/2] Add PHPStan --- .github/workflows/ci.yml | 4 ++++ composer.json | 2 ++ phpstan.neon | 10 ++++++++++ src/Bridge/Doctrine/Common/AbstractTypesDumper.php | 2 +- src/Bridge/Symfony/Bundle/ElaoEnumBundle.php | 2 +- src/Bridge/Symfony/Form/Type/EnumType.php | 2 +- src/Bridge/Symfony/Form/Type/FlagBagType.php | 4 ++-- src/Bridge/Symfony/VarDumper/Caster/FlagBagCaster.php | 2 +- .../Symfony/VarDumper/Caster/ReadableEnumCaster.php | 2 +- 9 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 phpstan.neon diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bb85b7cd..fb910db7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -168,3 +168,7 @@ jobs: - name: 'Run PHPUnit tests' run: vendor/bin/simple-phpunit --testdox --verbose ${{ matrix.code-coverage && '--coverage-text --coverage-clover build/logs/clover.xml' }} + + - name: 'Run PHPStan' + run: vendor/bin/phpstan + if: ${{ matrix.mongodb }} diff --git a/composer.json b/composer.json index 8315e6fd..e9135ace 100644 --- a/composer.json +++ b/composer.json @@ -47,6 +47,8 @@ "doctrine/dbal": "^3.2", "doctrine/doctrine-bundle": "^2.5", "doctrine/orm": "^2.10", + "phpstan/phpstan": "^1.7", + "phpunit/phpunit": "^9.5", "symfony/browser-kit": "^5.4|^6.0", "symfony/config": "^5.4|^6.0", "symfony/dependency-injection": "^5.4.2|^6.0.1", diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 00000000..ad755efd --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,10 @@ +includes: + - vendor/phpstan/phpstan/conf/bleedingEdge.neon +parameters: + level: 6 + paths: + - src +# - tests + checkGenericClassInNonGenericObjectType: false # https://phpstan.org/blog/generics-in-php-using-phpdocs + reportUnmatchedIgnoredErrors: false + checkMissingIterableValueType: false diff --git a/src/Bridge/Doctrine/Common/AbstractTypesDumper.php b/src/Bridge/Doctrine/Common/AbstractTypesDumper.php index f1301d93..bfa46e80 100644 --- a/src/Bridge/Doctrine/Common/AbstractTypesDumper.php +++ b/src/Bridge/Doctrine/Common/AbstractTypesDumper.php @@ -17,7 +17,7 @@ */ abstract class AbstractTypesDumper { - public function dumpToFile(string $file, array $types) + public function dumpToFile(string $file, array $types): void { file_put_contents($file, $this->dump($types)); } diff --git a/src/Bridge/Symfony/Bundle/ElaoEnumBundle.php b/src/Bridge/Symfony/Bundle/ElaoEnumBundle.php index cbf384fa..db57ffd9 100644 --- a/src/Bridge/Symfony/Bundle/ElaoEnumBundle.php +++ b/src/Bridge/Symfony/Bundle/ElaoEnumBundle.php @@ -19,7 +19,7 @@ class ElaoEnumBundle extends Bundle { - public function build(ContainerBuilder $container) + public function build(ContainerBuilder $container): void { parent::build($container); diff --git a/src/Bridge/Symfony/Form/Type/EnumType.php b/src/Bridge/Symfony/Form/Type/EnumType.php index dfeefb2b..512ab30d 100644 --- a/src/Bridge/Symfony/Form/Type/EnumType.php +++ b/src/Bridge/Symfony/Form/Type/EnumType.php @@ -25,7 +25,7 @@ class EnumType extends AbstractType /** * {@inheritdoc} */ - public function configureOptions(OptionsResolver $resolver) + public function configureOptions(OptionsResolver $resolver): void { $resolver // Override the original type logic for label for readable enums. diff --git a/src/Bridge/Symfony/Form/Type/FlagBagType.php b/src/Bridge/Symfony/Form/Type/FlagBagType.php index 806ec4c3..ba76c0e1 100644 --- a/src/Bridge/Symfony/Form/Type/FlagBagType.php +++ b/src/Bridge/Symfony/Form/Type/FlagBagType.php @@ -23,7 +23,7 @@ */ class FlagBagType extends AbstractType { - public function buildForm(FormBuilderInterface $builder, array $options) + public function buildForm(FormBuilderInterface $builder, array $options): void { if (!$options['multiple']) { throw new InvalidConfigurationException(sprintf( @@ -38,7 +38,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) /** * {@inheritdoc} */ - public function configureOptions(OptionsResolver $resolver) + public function configureOptions(OptionsResolver $resolver): void { $resolver ->setDefault('multiple', true) diff --git a/src/Bridge/Symfony/VarDumper/Caster/FlagBagCaster.php b/src/Bridge/Symfony/VarDumper/Caster/FlagBagCaster.php index 83106a74..597e9e12 100644 --- a/src/Bridge/Symfony/VarDumper/Caster/FlagBagCaster.php +++ b/src/Bridge/Symfony/VarDumper/Caster/FlagBagCaster.php @@ -18,7 +18,7 @@ final class FlagBagCaster { - public static function cast(FlagBag $bag) + public static function cast(FlagBag $bag): array { $a = []; diff --git a/src/Bridge/Symfony/VarDumper/Caster/ReadableEnumCaster.php b/src/Bridge/Symfony/VarDumper/Caster/ReadableEnumCaster.php index 1e84148c..9d05dd5d 100644 --- a/src/Bridge/Symfony/VarDumper/Caster/ReadableEnumCaster.php +++ b/src/Bridge/Symfony/VarDumper/Caster/ReadableEnumCaster.php @@ -17,7 +17,7 @@ final class ReadableEnumCaster { - public static function cast(ReadableEnumInterface $enum, $array) + public static function cast(ReadableEnumInterface $enum, $array): array { return $array + [ // Append the readable value; From d86f42c2ad6b8b5194c6dba02320c4b8ed3da7fc Mon Sep 17 00:00:00 2001 From: Ion Bazan Date: Mon, 20 Jun 2022 17:03:11 +0800 Subject: [PATCH 2/2] Add more return types --- phpstan.neon | 1 + src/Attribute/EnumCase.php | 3 +++ src/Bridge/Doctrine/Common/AbstractTypesDumper.php | 11 +++++++++++ 3 files changed, 15 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index ad755efd..1a40138b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -8,3 +8,4 @@ parameters: checkGenericClassInNonGenericObjectType: false # https://phpstan.org/blog/generics-in-php-using-phpdocs reportUnmatchedIgnoredErrors: false checkMissingIterableValueType: false + treatPhpDocTypesAsCertain: false diff --git a/src/Attribute/EnumCase.php b/src/Attribute/EnumCase.php index ff24740b..23b29cc6 100644 --- a/src/Attribute/EnumCase.php +++ b/src/Attribute/EnumCase.php @@ -15,6 +15,9 @@ #[\Attribute(\Attribute::TARGET_CLASS_CONSTANT)] class EnumCase { + /** + * @param array $extras + */ public function __construct(public readonly ?string $label = null, public readonly array $extras = []) { } diff --git a/src/Bridge/Doctrine/Common/AbstractTypesDumper.php b/src/Bridge/Doctrine/Common/AbstractTypesDumper.php index bfa46e80..b9b7cb8d 100644 --- a/src/Bridge/Doctrine/Common/AbstractTypesDumper.php +++ b/src/Bridge/Doctrine/Common/AbstractTypesDumper.php @@ -14,9 +14,14 @@ /** * @internal + * + * @phpstan-type Type = array{0: class-string<\BackedEnum>, 1: string, 2: string, 3: string|int|null} */ abstract class AbstractTypesDumper { + /** + * @param Type[] $types + */ public function dumpToFile(string $file, array $types): void { file_put_contents($file, $this->dump($types)); @@ -57,6 +62,9 @@ function (array $word): string { ); } + /** + * @param Type[] $types + */ private function dump(array $types): string { array_walk($types, static function (&$type) { @@ -92,6 +100,9 @@ private function dump(array $types): string abstract protected function getTypeCode(string $classname, string $enumClass, string $type, string $name, \BackedEnum|int|string|null $defaultOnNull = null): string; + /** + * @return array + */ abstract protected static function getSuffixes(): array; abstract protected static function getMarker(): string;