From 22cd00a7ee5657d843d4d23a321c7f3d1b3734b7 Mon Sep 17 00:00:00 2001 From: Martin Rademacher Date: Fri, 2 Sep 2022 16:09:01 +1200 Subject: [PATCH] Tweak CS rules around global imports and enforce (#1305) --- .github/workflows/security-checks.yml | 2 +- .php-cs-fixer.dist.php | 16 +++++++++++++--- Examples/misc/OpenApiSpec.php | 9 ++++++--- Examples/nesting/ActualModel.php | 6 ++++++ Examples/nesting/AlmostModel.php | 6 ++++++ Examples/nesting/ApiController.php | 6 ++++++ Examples/nesting/BaseModel.php | 6 ++++++ Examples/nesting/IntermediateModel.php | 6 ++++++ Examples/nesting/SoCloseModel.php | 6 ++++++ Examples/petstore-3.0/Controllers/Pet.php | 2 ++ Examples/petstore-3.0/Controllers/Store.php | 2 ++ Examples/petstore-3.0/Controllers/User.php | 2 ++ Examples/petstore-3.0/Models/ApiResponse.php | 2 ++ Examples/petstore-3.0/Petstore.php | 2 ++ Examples/petstore-3.0/Security.php | 2 ++ .../Controllers/PetController.php | 2 ++ .../Controllers/StoreController.php | 2 ++ .../Controllers/UserController.php | 2 ++ Examples/petstore.swagger.io/Models/Order.php | 2 ++ Examples/petstore.swagger.io/Models/Pet.php | 4 ++++ Examples/petstore.swagger.io/Models/User.php | 4 ++++ Examples/petstore.swagger.io/OpenApiSpec.php | 2 ++ Examples/petstore.swagger.io/OpenApiTags.php | 2 ++ Examples/petstore.swagger.io/PetstoreAuth.php | 2 ++ .../schema-query-parameter/app/Product.php | 2 ++ .../swagger-spec/petstore-simple/OpenApiSpec.php | 2 ++ .../swagger-spec/petstore-simple/SimplePet.php | 3 +++ .../petstore-simple/SimplePetsController.php | 2 ++ .../Controllers/PetWithDocsController.php | 2 ++ .../Models/ErrorModel.php | 2 ++ .../petstore-with-external-docs/Models/Pet.php | 2 ++ .../petstore-with-external-docs/OpenApiSpec.php | 2 ++ Examples/swagger-spec/petstore/OpenApiSpec.php | 2 ++ .../swagger-spec/petstore/PetsController.php | 2 ++ Examples/using-interfaces/ColorInterface.php | 2 ++ Examples/using-interfaces/GreenProduct.php | 2 ++ Examples/using-interfaces/OpenApiSpec.php | 2 ++ Examples/using-interfaces/Product.php | 1 + Examples/using-interfaces/ProductController.php | 2 ++ Examples/using-interfaces/ProductInterface.php | 2 ++ Examples/using-refs/Model.php | 2 ++ Examples/using-refs/OpenApiSpec.php | 2 ++ Examples/using-refs/Product.php | 3 +++ Examples/using-refs/ProductController.php | 2 ++ Examples/using-refs/PropertyRefController.php | 2 ++ Examples/using-refs/StockLevel.php | 2 ++ Examples/using-traits/BellsAndWhistles.php | 1 + Examples/using-traits/Decoration/Whistles.php | 2 ++ Examples/using-traits/DeleteEntity.php | 2 ++ Examples/using-traits/OpenApiSpec.php | 2 ++ Examples/using-traits/Product.php | 3 +++ Examples/using-traits/SimpleProduct.php | 3 +++ Examples/using-traits/TrickyProduct.php | 1 + src/Util.php | 7 +++---- .../Analysers/AttributeAnnotationFactoryTest.php | 4 ++++ tests/Analysers/ReflectionAnalyserTest.php | 3 +++ tests/Analysers/TokenScannerTest.php | 1 + tests/Annotations/AttributesSyncTest.php | 4 ++++ tests/Annotations/ComponentsTest.php | 4 ++++ tests/Fixtures/Annotations/CustomAttachable.php | 4 ++++ .../AnotherNamespace/Annotations/Constants.php | 2 +- .../AnotherNamespace/Annotations/Unrelated.php | 2 +- tests/Fixtures/AnotherNamespace/Child.php | 1 + .../AnotherNamespace/ChildWithDocBlocks.php | 1 + tests/Fixtures/Apis/DocBlocks/basic.php | 1 - tests/Fixtures/Attributes/CustomAttachable.php | 4 ++++ tests/Fixtures/CustomerInterface.php | 5 +++++ tests/Fixtures/ExpandClasses/Base.php | 2 +- tests/Fixtures/ExpandClasses/BaseInterface.php | 4 +++- .../ExpandClasses/BaseThatImplements.php | 2 +- tests/Fixtures/ExpandClasses/Extended.php | 2 +- .../ExpandClasses/ExtendedWithTwoSchemas.php | 2 +- .../ExpandClasses/ExtendedWithoutAllOf.php | 2 +- .../ExpandClasses/ExtendsBaseThatImplements.php | 4 +++- .../TraitUsedByExtendsBaseThatImplements.php | 4 +++- tests/Fixtures/NestedProperty.php | 1 + .../PHP/Inheritance/ExtenedsBaseInterface.php | 4 ++++ tests/Fixtures/PHP/Php8PromotedProperties.php | 2 ++ tests/Fixtures/PHP/StatusEnumBacked.php | 4 ++++ tests/Fixtures/PHP/StatusEnumIntegerBacked.php | 4 ++++ tests/Fixtures/PHP/StatusEnumStringBacked.php | 4 ++++ tests/Fixtures/PHP/namespaces1.php | 6 +++++- tests/Fixtures/PHP/namespaces2.php | 6 +++++- tests/Fixtures/Parser/AllTraits.php | 4 ++++ tests/Fixtures/Parser/UserInterface.php | 1 + .../Processors/EntityControllerClass.php | 2 ++ tests/Fixtures/ThirdPartyAnnotations.php | 1 + tests/Fixtures/TypedProperties.php | 10 ++++++++-- tests/Fixtures/UsingAttributes.php | 4 ++++ tests/Fixtures/UsingCustomAttachables.php | 6 ++++-- tests/Fixtures/UsingPhpDoc.php | 2 ++ tests/Fixtures/UsingRefs.php | 2 ++ tests/Fixtures/UsingVar.php | 4 ++++ tests/OpenApiTestCase.php | 8 +++----- tests/Processors/CleanUnusedComponentsTest.php | 4 ++++ 95 files changed, 267 insertions(+), 33 deletions(-) diff --git a/.github/workflows/security-checks.yml b/.github/workflows/security-checks.yml index 50b0911e7..101f3ff7c 100644 --- a/.github/workflows/security-checks.yml +++ b/.github/workflows/security-checks.yml @@ -41,4 +41,4 @@ jobs: path: ~/.symfony/cache key: security-checker-db - - uses: symfonycorp/security-checker-action@v2 + - uses: symfonycorp/security-checker-action@v3 diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 4c024e3e0..c0e44d558 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -1,4 +1,4 @@ -path('src')->name('*.php') ->path('tests')->name('*.php') + // ContextTest::testFullyQualifiedName relies on the 'use Exception' statement... + ->filter(function (\SplFileInfo $file) { + return !strpos($file->getPathname(), 'tests/Fixtures/Customer.php'); + }) ->path('Examples')->name('*.php') + ->filter(function (\SplFileInfo $file) { + return !strpos($file->getPathname(), 'Examples/petstore-3.0/Petstore.php') + && !strpos($file->getPathname(), 'Examples/misc/OpenApiSpec.php'); + }) ->path('tools')->name('*.php') ->in(__DIR__) ; return (new PhpCsFixer\Config()) ->registerCustomFixers([ - (new ScopedLicenseFixer())->scope(['/src/']), - (new ScopedDeclareStrictTypesFixer())->scope(['/src/']), + (new ScopedLicenseFixer())->scope(['/src/', '/tests/']), //, '/Examples/']), + (new ScopedDeclareStrictTypesFixer())->scope(['/src/', '/tests/']), ]) ->setRules([ '@PSR2' => true, @@ -52,6 +60,8 @@ 'trim_array_spaces' => true, 'single_space_after_construct' => true, 'single_line_comment_spacing' => true, + 'fully_qualified_strict_types' => true, + 'global_namespace_import' => ['import_classes' => false, 'import_constants' => null, 'import_functions' => null], 'no_empty_phpdoc' => true, // 7.3 only 'no_superfluous_phpdoc_tags' => true, diff --git a/Examples/misc/OpenApiSpec.php b/Examples/misc/OpenApiSpec.php index 00a386582..6397e7297 100644 --- a/Examples/misc/OpenApiSpec.php +++ b/Examples/misc/OpenApiSpec.php @@ -1,7 +1,13 @@ notPath(Util::getRelativePath($path, $directory)); } } else { - throw new InvalidArgumentException('Unexpected $exclude value:' . gettype($exclude)); + throw new \InvalidArgumentException('Unexpected $exclude value:' . gettype($exclude)); } } diff --git a/tests/Analysers/AttributeAnnotationFactoryTest.php b/tests/Analysers/AttributeAnnotationFactoryTest.php index d68c2026f..dcc1fe2c9 100644 --- a/tests/Analysers/AttributeAnnotationFactoryTest.php +++ b/tests/Analysers/AttributeAnnotationFactoryTest.php @@ -1,5 +1,9 @@ [ 'uses' => [ 'Property' => 'OpenApi\\Attributes\\Property', + 'OA' => 'OpenApi\Annotations', ], 'interfaces' => [], 'traits' => [], diff --git a/tests/Annotations/AttributesSyncTest.php b/tests/Annotations/AttributesSyncTest.php index 5c5594dbe..7a2a73151 100644 --- a/tests/Annotations/AttributesSyncTest.php +++ b/tests/Annotations/AttributesSyncTest.php @@ -1,5 +1,9 @@ expectedLogMessages[] = [function ($entry, $type) use ($needle, $message) { - if ($entry instanceof Exception) { + if ($entry instanceof \Exception) { $entry = $entry->getMessage(); } $this->assertStringContainsString($needle, $entry, $message); @@ -255,7 +253,7 @@ protected function annotationsFromDocBlockParser(string $docBlock, array $extraA public function allAnnotationClasses(): array { $classes = []; - $dir = new DirectoryIterator(__DIR__ . '/../src/Annotations'); + $dir = new \DirectoryIterator(__DIR__ . '/../src/Annotations'); foreach ($dir as $entry) { if (!$entry->isFile() || $entry->getExtension() != 'php') { continue; @@ -278,7 +276,7 @@ public function allAnnotationClasses(): array public function allAttributeClasses(): array { $classes = []; - $dir = new DirectoryIterator(__DIR__ . '/../src/Attributes'); + $dir = new \DirectoryIterator(__DIR__ . '/../src/Attributes'); foreach ($dir as $entry) { if (!$entry->isFile() || $entry->getExtension() != 'php') { continue; diff --git a/tests/Processors/CleanUnusedComponentsTest.php b/tests/Processors/CleanUnusedComponentsTest.php index c8c5131a1..bbfd1a459 100644 --- a/tests/Processors/CleanUnusedComponentsTest.php +++ b/tests/Processors/CleanUnusedComponentsTest.php @@ -1,5 +1,9 @@