diff --git a/.github/workflows/grumphp.yaml b/.github/workflows/grumphp.yaml index b99ffd2..d2d5da9 100644 --- a/.github/workflows/grumphp.yaml +++ b/.github/workflows/grumphp.yaml @@ -7,7 +7,7 @@ jobs: strategy: matrix: operating-system: [ubuntu-latest] - php-versions: ['7.3', '7.4', '8.0'] + php-versions: ['8.0', '8.1'] composer-options: ['', '--prefer-lowest'] fail-fast: false name: PHP ${{ matrix.php-versions }} @ ${{ matrix.operating-system }} with ${{ matrix.composer-options }} @@ -34,14 +34,7 @@ jobs: path: ${{ steps.composercache.outputs.dir }} key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} restore-keys: ${{ runner.os }}-composer- - - name: Install dependencies setFinder( + \Symfony\Component\Finder\Finder::create() + ->in([ + __DIR__ . '/src', + __DIR__ . '/test', + ]) + ) + ->setRiskyAllowed(true) + ->setRules([ + '@Symfony' => true, + 'align_multiline_comment' => true, + 'array_indentation' => true, + 'array_syntax' => ['syntax' => 'short'], + 'backtick_to_shell_exec' => true, + 'blank_line_before_statement' => ['statements' => ['return']], + 'class_keyword_remove' => false, + 'combine_consecutive_issets' => true, + 'combine_consecutive_unsets' => true, + 'comment_to_phpdoc' => true, + 'compact_nullable_typehint' => true, + 'date_time_immutable' => true, + 'declare_strict_types' => true, + 'doctrine_annotation_array_assignment' => true, + 'doctrine_annotation_braces' => true, + 'doctrine_annotation_indentation' => true, + 'doctrine_annotation_spaces' => true, + 'escape_implicit_backslashes' => true, + 'explicit_indirect_variable' => true, + 'explicit_string_variable' => true, + 'final_internal_class' => true, + 'fully_qualified_strict_types' => true, + 'general_phpdoc_annotation_remove' => false, + 'header_comment' => false, + 'heredoc_to_nowdoc' => false, + 'linebreak_after_opening_tag' => true, + 'list_syntax' => ['syntax' => 'short'], + 'mb_str_functions' => true, + 'method_chaining_indentation' => true, + 'multiline_comment_opening_closing' => true, + 'multiline_whitespace_before_semicolons' => true, + 'native_function_invocation' => false, + 'no_alternative_syntax' => true, + 'no_blank_lines_before_namespace' => false, + 'no_null_property_initialization' => true, + 'no_php4_constructor' => true, + 'echo_tag_syntax' => ['format'=> 'long'], + 'no_superfluous_elseif' => true, + 'no_unreachable_default_argument_value' => true, + 'no_useless_else' => true, + 'no_useless_return' => true, + 'not_operator_with_space' => false, + 'not_operator_with_successor_space' => false, + 'ordered_class_elements' => false, + 'ordered_imports' => true, + 'php_unit_dedicate_assert' => false, + 'php_unit_expectation' => false, + 'php_unit_mock' => false, + 'php_unit_namespaced' => false, + 'php_unit_no_expectation_annotation' => false, + 'phpdoc_order_by_value' => ['annotations' => ['covers']], + 'php_unit_set_up_tear_down_visibility' => true, + 'php_unit_strict' => false, + 'php_unit_test_annotation' => false, + 'php_unit_test_class_requires_covers' => false, + 'php_unit_method_casing' => ['case' => 'snake_case'], + 'phpdoc_add_missing_param_annotation' => true, + 'phpdoc_order' => true, + 'phpdoc_types_order' => ['null_adjustment' => 'always_last'], + 'pow_to_exponentiation' => true, + 'psr_autoloading' => ['dir' => 'src'], + 'random_api_migration' => false, + 'simplified_null_return' => true, + 'static_lambda' => false, + 'strict_comparison' => true, + 'strict_param' => true, + 'string_line_ending' => true, + 'ternary_to_null_coalescing' => true, + 'void_return' => true, + 'yoda_style' => true, + 'single_line_throw' => false, + 'phpdoc_align' => ['align' => 'left'], + 'phpdoc_to_comment' => false, + 'global_namespace_import' => [ + 'import_classes' => true, + 'import_constants' => true, + 'import_functions' => true, + ], + ]) +; diff --git a/.php_cs.dist b/.php_cs.dist deleted file mode 100644 index 0372e68..0000000 --- a/.php_cs.dist +++ /dev/null @@ -1,83 +0,0 @@ -setFinder( - \Symfony\Component\Finder\Finder::create() - ->in([ - __DIR__ . '/src', - __DIR__ . '/test', - ]) - ) - ->setRiskyAllowed(true) - ->setRules([ - '@Symfony' => true, - 'align_multiline_comment' => true, - 'array_indentation' => true, - 'array_syntax' => ['syntax' => 'short'], - 'backtick_to_shell_exec' => true, - 'blank_line_before_statement' => true, - 'class_keyword_remove' => false, - 'combine_consecutive_issets' => true, - 'combine_consecutive_unsets' => true, - 'comment_to_phpdoc' => true, - 'compact_nullable_typehint' => true, - 'date_time_immutable' => true, - 'declare_strict_types' => true, - 'doctrine_annotation_array_assignment' => true, - 'doctrine_annotation_braces' => true, - 'doctrine_annotation_indentation' => true, - 'doctrine_annotation_spaces' => true, - 'escape_implicit_backslashes' => true, - 'explicit_indirect_variable' => true, - 'explicit_string_variable' => true, - 'final_internal_class' => false, - 'fully_qualified_strict_types' => true, - 'general_phpdoc_annotation_remove' => false, - 'header_comment' => false, - 'heredoc_to_nowdoc' => false, - 'linebreak_after_opening_tag' => true, - 'list_syntax' => true, - 'mb_str_functions' => true, - 'method_chaining_indentation' => true, - 'multiline_comment_opening_closing' => true, - 'multiline_whitespace_before_semicolons' => true, - 'native_function_invocation' => false, - 'no_alternative_syntax' => true, - 'no_blank_lines_before_namespace' => false, - 'no_null_property_initialization' => true, - 'no_php4_constructor' => true, - 'no_short_echo_tag' => false, - 'no_superfluous_elseif' => true, - 'no_unreachable_default_argument_value' => true, - 'no_useless_else' => true, - 'no_useless_return' => true, - 'not_operator_with_space' => false, - 'not_operator_with_successor_space' => false, - 'ordered_class_elements' => false, - 'ordered_imports' => true, - 'php_unit_dedicate_assert' => false, - 'php_unit_expectation' => false, - 'php_unit_mock' => false, - 'php_unit_namespaced' => false, - 'php_unit_no_expectation_annotation' => false, - 'php_unit_ordered_covers' => true, - 'php_unit_set_up_tear_down_visibility' => true, - 'php_unit_strict' => false, - 'php_unit_test_annotation' => false, - 'php_unit_test_class_requires_covers' => false, - 'phpdoc_add_missing_param_annotation' => true, - 'phpdoc_order' => true, - 'phpdoc_types_order' => ['null_adjustment' => 'always_last'], - 'pow_to_exponentiation' => true, - 'psr0' => true, - 'random_api_migration' => false, - 'simplified_null_return' => true, - 'static_lambda' => false, - 'strict_comparison' => true, - 'strict_param' => true, - 'string_line_ending' => true, - 'ternary_to_null_coalescing' => true, - 'void_return' => true, - 'yoda_style' => true, - ]) -; diff --git a/composer.json b/composer.json index 47097af..a3e63b4 100644 --- a/composer.json +++ b/composer.json @@ -10,19 +10,19 @@ } ], "require": { - "php": "^7.3 || ^8.0", + "php": "^8.0", "phpro/api-problem": "^1.0", - "symfony/dependency-injection": "^4.4|^5.0", - "symfony/event-dispatcher": "^4.4|^5.0", - "symfony/http-kernel": "^4.4|^5.0" + "symfony/dependency-injection": "^5.3 || ^6.0", + "symfony/event-dispatcher": "^5.3 || ^6.0", + "symfony/http-kernel": "^5.3 || ^6.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.16.7", + "friendsofphp/php-cs-fixer": "^3.3", "matthiasnoback/symfony-dependency-injection-test": "^4.2", - "phpro/grumphp-shim": "^1.2.0", + "phpro/grumphp-shim": "^1.6.0", "phpspec/prophecy-phpunit": "^2.0", "phpunit/phpunit": "^9.4", - "symfony/security-core": "^4.4|^5.0" + "symfony/security-core": "^5.3 || ^6.0" }, "config": { "sort-packages": true diff --git a/grumphp.yml.dist b/grumphp.yml.dist index 26e8f1a..beb9e7e 100644 --- a/grumphp.yml.dist +++ b/grumphp.yml.dist @@ -1,7 +1,7 @@ grumphp: tasks: phpcsfixer2: - config: ".php_cs.dist" + config: ".php-cs-fixer.dist.php" config_contains_finder: true phpunit: ~ clover_coverage: diff --git a/src/EventListener/JsonApiProblemExceptionListener.php b/src/EventListener/JsonApiProblemExceptionListener.php index 3b8d8c7..d2a1f78 100644 --- a/src/EventListener/JsonApiProblemExceptionListener.php +++ b/src/EventListener/JsonApiProblemExceptionListener.php @@ -11,6 +11,7 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\ExceptionEvent; +use Throwable; class JsonApiProblemExceptionListener { @@ -45,7 +46,7 @@ public function onKernelException(ExceptionEvent $event): void $event->setResponse($this->generateResponse($apiProblem)); } - private function convertExceptionToProblem(\Throwable $exception): ApiProblemInterface + private function convertExceptionToProblem(Throwable $exception): ApiProblemInterface { if (!$this->exceptionTransformer->accepts($exception)) { return new ExceptionApiProblem($exception); diff --git a/src/Transformer/ApiProblemExceptionTransformer.php b/src/Transformer/ApiProblemExceptionTransformer.php index bbf0311..9315f9c 100644 --- a/src/Transformer/ApiProblemExceptionTransformer.php +++ b/src/Transformer/ApiProblemExceptionTransformer.php @@ -7,18 +7,19 @@ use Phpro\ApiProblem\ApiProblemInterface; use Phpro\ApiProblem\Exception\ApiProblemException; use Phpro\ApiProblemBundle\Exception\ApiProblemHttpException; +use Throwable; class ApiProblemExceptionTransformer implements ExceptionTransformerInterface { /** * @param ApiProblemException|ApiProblemHttpException $exception */ - public function transform(\Throwable $exception): ApiProblemInterface + public function transform(Throwable $exception): ApiProblemInterface { return $exception->getApiProblem(); } - public function accepts(\Throwable $exception): bool + public function accepts(Throwable $exception): bool { return $exception instanceof ApiProblemException || $exception instanceof ApiProblemHttpException; } diff --git a/src/Transformer/Chain.php b/src/Transformer/Chain.php index ddd7165..7438d3b 100644 --- a/src/Transformer/Chain.php +++ b/src/Transformer/Chain.php @@ -6,6 +6,7 @@ use Phpro\ApiProblem\ApiProblemInterface; use Phpro\ApiProblem\Http\ExceptionApiProblem; +use Throwable; class Chain implements ExceptionTransformerInterface { @@ -21,7 +22,7 @@ public function __construct(iterable $transformers) } } - public function transform(\Throwable $exception): ApiProblemInterface + public function transform(Throwable $exception): ApiProblemInterface { foreach ($this->transformers as $transformer) { if ($transformer->accepts($exception)) { @@ -32,7 +33,7 @@ public function transform(\Throwable $exception): ApiProblemInterface return new ExceptionApiProblem($exception); } - public function accepts(\Throwable $exception): bool + public function accepts(Throwable $exception): bool { return true; } diff --git a/src/Transformer/ExceptionTransformerInterface.php b/src/Transformer/ExceptionTransformerInterface.php index 7e3a551..9c58782 100644 --- a/src/Transformer/ExceptionTransformerInterface.php +++ b/src/Transformer/ExceptionTransformerInterface.php @@ -5,10 +5,11 @@ namespace Phpro\ApiProblemBundle\Transformer; use Phpro\ApiProblem\ApiProblemInterface; +use Throwable; interface ExceptionTransformerInterface { - public function transform(\Throwable $exception): ApiProblemInterface; + public function transform(Throwable $exception): ApiProblemInterface; - public function accepts(\Throwable $exception): bool; + public function accepts(Throwable $exception): bool; } diff --git a/src/Transformer/HttpExceptionTransformer.php b/src/Transformer/HttpExceptionTransformer.php index d26aaba..acd1ff6 100644 --- a/src/Transformer/HttpExceptionTransformer.php +++ b/src/Transformer/HttpExceptionTransformer.php @@ -7,13 +7,14 @@ use Phpro\ApiProblem\ApiProblemInterface; use Phpro\ApiProblem\Http\ExceptionApiProblem; use Symfony\Component\HttpKernel\Exception\HttpException; +use Throwable; class HttpExceptionTransformer implements ExceptionTransformerInterface { /** * @param HttpException $exception */ - public function transform(\Throwable $exception): ApiProblemInterface + public function transform(Throwable $exception): ApiProblemInterface { return new ExceptionApiProblem( new HttpException( @@ -26,7 +27,7 @@ public function transform(\Throwable $exception): ApiProblemInterface ); } - public function accepts(\Throwable $exception): bool + public function accepts(Throwable $exception): bool { return $exception instanceof HttpException; } diff --git a/src/Transformer/SecurityExceptionTransformer.php b/src/Transformer/SecurityExceptionTransformer.php index 6f16ec1..5eb9855 100644 --- a/src/Transformer/SecurityExceptionTransformer.php +++ b/src/Transformer/SecurityExceptionTransformer.php @@ -9,10 +9,11 @@ use Phpro\ApiProblem\Http\UnauthorizedProblem; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\ExceptionInterface as SecurityException; +use Throwable; class SecurityExceptionTransformer implements ExceptionTransformerInterface { - public function transform(\Throwable $exception): ApiProblemInterface + public function transform(Throwable $exception): ApiProblemInterface { if ($exception instanceof AuthenticationException) { return new UnauthorizedProblem($exception->getMessage()); @@ -21,7 +22,7 @@ public function transform(\Throwable $exception): ApiProblemInterface return new ForbiddenProblem($exception->getMessage()); } - public function accepts(\Throwable $exception): bool + public function accepts(Throwable $exception): bool { return $exception instanceof SecurityException; } diff --git a/test/EventListener/JsonApiProblemExceptionListenerTest.php b/test/EventListener/JsonApiProblemExceptionListenerTest.php index 3c68ef0..61e145b 100644 --- a/test/EventListener/JsonApiProblemExceptionListenerTest.php +++ b/test/EventListener/JsonApiProblemExceptionListenerTest.php @@ -4,6 +4,8 @@ namespace PhproTest\ApiProblemBundle\EventListener; +use Exception; +use function json_encode; use Phpro\ApiProblem\ApiProblemInterface; use Phpro\ApiProblem\DebuggableApiProblemInterface; use Phpro\ApiProblem\Http\HttpApiProblem; @@ -17,6 +19,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\ExceptionEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Throwable; /** @covers \Phpro\ApiProblemBundle\EventListener\JsonApiProblemExceptionListener */ class JsonApiProblemExceptionListenerTest extends TestCase @@ -34,7 +37,7 @@ class JsonApiProblemExceptionListenerTest extends TestCase private $event; /** - * @var \Throwable + * @var Throwable */ private $exception; @@ -47,7 +50,7 @@ protected function setUp(): void { $this->request = $this->prophesize(Request::class); $httpKernel = $this->prophesize(HttpKernelInterface::class); - $this->exception = new \Exception('error'); + $this->exception = new Exception('error'); $this->event = new ExceptionEvent( $httpKernel->reveal(), $this->request->reveal(), @@ -123,8 +126,8 @@ public function it_uses_an_exception_transformer(): void $apiProblem = $this->prophesize(ApiProblemInterface::class); $apiProblem->toArray()->willReturn([]); - $this->exceptionTransformer->accepts(Argument::type(\Exception::class))->willReturn(true); - $this->exceptionTransformer->transform(Argument::type(\Exception::class))->willReturn($apiProblem->reveal()); + $this->exceptionTransformer->accepts(Argument::type(Exception::class))->willReturn(true); + $this->exceptionTransformer->transform(Argument::type(Exception::class))->willReturn($apiProblem->reveal()); $listener->onKernelException($this->event); $this->assertApiProblemWithResponseBody(400, []); @@ -140,8 +143,8 @@ public function it_returns_the_status_code_from_the_api_problem(): void $apiProblem = $this->prophesize(ApiProblemInterface::class); $apiProblem->toArray()->willReturn(['status' => 123]); - $this->exceptionTransformer->accepts(Argument::type(\Exception::class))->willReturn(true); - $this->exceptionTransformer->transform(Argument::type(\Exception::class))->willReturn($apiProblem->reveal()); + $this->exceptionTransformer->accepts(Argument::type(Exception::class))->willReturn(true); + $this->exceptionTransformer->transform(Argument::type(Exception::class))->willReturn($apiProblem->reveal()); $listener->onKernelException($this->event); $this->assertApiProblemWithResponseBody(123, ['status' => 123]); @@ -174,7 +177,7 @@ private function assertApiProblemWithResponseBody(int $expectedResponseCode, arr $this->assertSame($expectedResponseCode, $response->getStatusCode()); $this->assertSame('application/problem+json', $response->headers->get('Content-Type')); $this->assertJsonStringEqualsJsonString( - \json_encode($expectedData), + json_encode($expectedData), $this->event->getResponse()->getContent() ); } diff --git a/test/Exception/ApiProblemHttpExceptionTest.php b/test/Exception/ApiProblemHttpExceptionTest.php index c6ed8fa..f7ddd77 100644 --- a/test/Exception/ApiProblemHttpExceptionTest.php +++ b/test/Exception/ApiProblemHttpExceptionTest.php @@ -28,7 +28,7 @@ protected function setUp(): void/* The :void return type declaration that should } /** @test */ - public function it_is_accepted_by_the_ApiProblemExceptionTransformer(): void + public function it_is_accepted_by_the__api_problem_exception_transformer(): void { $transformer = new ApiProblemExceptionTransformer(); @@ -36,7 +36,7 @@ public function it_is_accepted_by_the_ApiProblemExceptionTransformer(): void } /** @test */ - public function it_is_an_instance_of_HttpException(): void + public function it_is_an_instance_of__http_exception(): void { $exception = new ApiProblemHttpException($this->apiProblem->reveal()); diff --git a/test/Transformer/ApiProblemExceptionTransformerTest.php b/test/Transformer/ApiProblemExceptionTransformerTest.php index 06afad3..43eced2 100644 --- a/test/Transformer/ApiProblemExceptionTransformerTest.php +++ b/test/Transformer/ApiProblemExceptionTransformerTest.php @@ -4,6 +4,7 @@ namespace PhproTest\ApiProblemBundle\Transformer; +use Exception; use Phpro\ApiProblem\ApiProblemInterface; use Phpro\ApiProblem\Exception\ApiProblemException; use Phpro\ApiProblemBundle\Transformer\ApiProblemExceptionTransformer; @@ -43,7 +44,7 @@ public function it_accepts_api_problem_exceptions(): void $transformer = new ApiProblemExceptionTransformer(); $this->assertTrue($transformer->accepts(new ApiProblemException($this->apiProblem->reveal()))); - $this->assertFalse($transformer->accepts(new \Exception())); + $this->assertFalse($transformer->accepts(new Exception())); } /** @test */ diff --git a/test/Transformer/ChainTest.php b/test/Transformer/ChainTest.php index aac43fc..e5a8545 100644 --- a/test/Transformer/ChainTest.php +++ b/test/Transformer/ChainTest.php @@ -4,6 +4,7 @@ namespace PhproTest\ApiProblemBundle\Transformer; +use Exception; use Phpro\ApiProblem\ApiProblemInterface; use Phpro\ApiProblem\Http\ExceptionApiProblem; use Phpro\ApiProblemBundle\Transformer\Chain; @@ -31,7 +32,7 @@ public function it_is_an_exception_transformer(): void public function it_accepts_any_exception(): void { $transformer = new Chain([]); - $this->assertTrue($transformer->accepts(new \Exception())); + $this->assertTrue($transformer->accepts(new Exception())); } /** @test */ @@ -43,7 +44,7 @@ public function it_transforms_with_first_acceptable_transformer(): void $this->mockTransformer(true, $apiProblem2 = $this->prophesize(ApiProblemInterface::class)->reveal()), ]); - $this->assertEquals($apiProblem1, $transformer->transform(new \Exception())); + $this->assertEquals($apiProblem1, $transformer->transform(new Exception())); } /** @test */ @@ -51,7 +52,7 @@ public function it_transforms_to_basic_exception_problem_when_no_transformer_mat { $transformer = new Chain([$this->mockTransformer(false)]); - $this->assertInstanceOf(ExceptionApiProblem::class, $transformer->transform(new \Exception())); + $this->assertInstanceOf(ExceptionApiProblem::class, $transformer->transform(new Exception())); } private function mockTransformer(bool $accepts, ?ApiProblemInterface $apiProblem = null): ExceptionTransformerInterface diff --git a/test/Transformer/HttpExceptionTransformerTest.php b/test/Transformer/HttpExceptionTransformerTest.php index da92fa8..b58fe14 100644 --- a/test/Transformer/HttpExceptionTransformerTest.php +++ b/test/Transformer/HttpExceptionTransformerTest.php @@ -4,6 +4,7 @@ namespace PhproTest\ApiProblemBundle\Transformer; +use Exception; use Phpro\ApiProblem\Http\ExceptionApiProblem; use Phpro\ApiProblem\Http\HttpApiProblem; use Phpro\ApiProblemBundle\Transformer\ExceptionTransformerInterface; @@ -29,7 +30,7 @@ public function it_accepts_api_problem_exceptions(): void $transformer = new HttpExceptionTransformer(); $this->assertTrue($transformer->accepts(new HttpException(400, 'Bad Request'))); - $this->assertFalse($transformer->accepts(new \Exception())); + $this->assertFalse($transformer->accepts(new Exception())); } /** @test */ diff --git a/test/Transformer/SecurityExceptionTransformerTest.php b/test/Transformer/SecurityExceptionTransformerTest.php index c09f04c..5e81857 100644 --- a/test/Transformer/SecurityExceptionTransformerTest.php +++ b/test/Transformer/SecurityExceptionTransformerTest.php @@ -4,6 +4,7 @@ namespace PhproTest\ApiProblemBundle\Transformer; +use Exception; use Phpro\ApiProblem\Http\ForbiddenProblem; use Phpro\ApiProblem\Http\HttpApiProblem; use Phpro\ApiProblem\Http\UnauthorizedProblem; @@ -32,7 +33,7 @@ public function it_accepts_api_problem_exceptions(): void $transformer = new SecurityExceptionTransformer(); $this->assertTrue($transformer->accepts(new RuntimeException())); - $this->assertFalse($transformer->accepts(new \Exception())); + $this->assertFalse($transformer->accepts(new Exception())); } /** @test */