From ede452533274fe7ec4dc76dcb5bcde7f261fb145 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 30 Dec 2017 21:35:26 +0100 Subject: [PATCH 1/3] Refactored exceptions to properly use package exceptions --- .../src/Exception/ExceptionInterface.php | 2 +- .../Exception/EntityDoesNotExistException.php | 4 +--- .../Core/src/Exception/ExceptionInterface.php | 8 +++++++ .../Exception/InvalidArgumentException.php | 8 +++++++ .../Exception/InvalidShortCodeException.php | 2 -- .../src/Exception/InvalidUrlException.php | 2 -- .../src/Exception/NonUniqueSlugException.php | 2 -- .../Core/src/Exception/RuntimeException.php | 8 +++++++ module/Core/src/Service/UrlShortener.php | 2 +- .../src/Service/UrlShortenerInterface.php | 2 +- module/Core/src/Service/VisitsTracker.php | 2 +- .../src/Service/VisitsTrackerInterface.php | 2 +- module/Core/test/Service/UrlShortenerTest.php | 2 +- .../src/Exception/AuthenticationException.php | 4 +--- .../Rest/src/Exception/ExceptionInterface.php | 8 +++++++ .../Rest/src/Exception/RuntimeException.php | 8 +++++++ .../src/Middleware/BodyParserMiddleware.php | 23 ++++++++++--------- module/Rest/src/Util/RestUtils.php | 2 +- 18 files changed, 61 insertions(+), 30 deletions(-) create mode 100644 module/Core/src/Exception/ExceptionInterface.php create mode 100644 module/Core/src/Exception/InvalidArgumentException.php create mode 100644 module/Core/src/Exception/RuntimeException.php create mode 100644 module/Rest/src/Exception/ExceptionInterface.php create mode 100644 module/Rest/src/Exception/RuntimeException.php diff --git a/module/Common/src/Exception/ExceptionInterface.php b/module/Common/src/Exception/ExceptionInterface.php index 72e53d0f..349f3f4b 100644 --- a/module/Common/src/Exception/ExceptionInterface.php +++ b/module/Common/src/Exception/ExceptionInterface.php @@ -3,6 +3,6 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Common\Exception; -interface ExceptionInterface +interface ExceptionInterface extends \Throwable { } diff --git a/module/Core/src/Exception/EntityDoesNotExistException.php b/module/Core/src/Exception/EntityDoesNotExistException.php index 732eaeba..ba4d233d 100644 --- a/module/Core/src/Exception/EntityDoesNotExistException.php +++ b/module/Core/src/Exception/EntityDoesNotExistException.php @@ -3,9 +3,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Exception; -use Shlinkio\Shlink\Common\Exception\ExceptionInterface; - -class EntityDoesNotExistException extends \RuntimeException implements ExceptionInterface +class EntityDoesNotExistException extends RuntimeException { public static function createFromEntityAndConditions($entityName, array $conditions) { diff --git a/module/Core/src/Exception/ExceptionInterface.php b/module/Core/src/Exception/ExceptionInterface.php new file mode 100644 index 00000000..2901b7cb --- /dev/null +++ b/module/Core/src/Exception/ExceptionInterface.php @@ -0,0 +1,8 @@ +getParsedBody(); // In requests that do not allow body or if the body has already been parsed, continue to next middleware - if (! empty($currentParams) || in_array($method, [ + if (! empty($currentParams) || \in_array($method, [ self::METHOD_GET, self::METHOD_HEAD, self::METHOD_OPTIONS, @@ -37,7 +37,7 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac // If the accepted content is JSON, try to parse the body from JSON $contentType = $this->getRequestContentType($request); - if (in_array($contentType, ['application/json', 'text/json', 'application/x-json'], true)) { + if (\in_array($contentType, ['application/json', 'text/json', 'application/x-json'], true)) { return $delegate->process($this->parseFromJson($request)); } @@ -48,27 +48,28 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac * @param Request $request * @return string */ - protected function getRequestContentType(Request $request) + private function getRequestContentType(Request $request): string { $contentType = $request->getHeaderLine('Content-type'); - $contentTypes = explode(';', $contentType); - return trim(array_shift($contentTypes)); + $contentTypes = \explode(';', $contentType); + return \trim(\array_shift($contentTypes)); } /** * @param Request $request * @return Request + * @throws RuntimeException */ - protected function parseFromJson(Request $request) + private function parseFromJson(Request $request): Request { $rawBody = (string) $request->getBody(); if (empty($rawBody)) { return $request; } - $parsedJson = json_decode($rawBody, true); - if (json_last_error() !== JSON_ERROR_NONE) { - throw new RuntimeException(sprintf('Error when parsing JSON request body: %s', json_last_error_msg())); + $parsedJson = \json_decode($rawBody, true); + if (\json_last_error() !== JSON_ERROR_NONE) { + throw new RuntimeException(\sprintf('Error when parsing JSON request body: %s', \json_last_error_msg())); } return $request->withParsedBody($parsedJson); @@ -78,7 +79,7 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac * @param Request $request * @return Request */ - protected function parseFromUrlEncoded(Request $request) + private function parseFromUrlEncoded(Request $request): Request { $rawBody = (string) $request->getBody(); if (empty($rawBody)) { diff --git a/module/Rest/src/Util/RestUtils.php b/module/Rest/src/Util/RestUtils.php index dcd6632e..407b0845 100644 --- a/module/Rest/src/Util/RestUtils.php +++ b/module/Rest/src/Util/RestUtils.php @@ -20,7 +20,7 @@ class RestUtils const NOT_FOUND_ERROR = 'NOT_FOUND'; const UNKNOWN_ERROR = 'UNKNOWN_ERROR'; - public static function getRestErrorCodeFromException(Common\ExceptionInterface $e) + public static function getRestErrorCodeFromException(\Throwable $e) { switch (true) { case $e instanceof Core\InvalidShortCodeException: From 2ec807ba7018415e9b95d3a0cdf785d9460c6c28 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 30 Dec 2017 21:36:33 +0100 Subject: [PATCH 2/3] Increased phpstan required level --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f3606d17..6e5357c0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ before_script: script: - mkdir build - composer check - - if [[ $TRAVIS_PHP_VERSION = 7.1 ]] || [[ $TRAVIS_PHP_VERSION = 7.2 ]]; then ~/.composer/vendor/bin/phpstan analyse module/*/src/ --level=5 -c phpstan.neon; fi + - if [[ $TRAVIS_PHP_VERSION = 7.1 ]] || [[ $TRAVIS_PHP_VERSION = 7.2 ]]; then ~/.composer/vendor/bin/phpstan analyse module/*/src/ --level=6 -c phpstan.neon; fi after_script: - vendor/bin/phpcov merge build --clover build/clover.xml From 79427d08d7082cb7f9d669039baeef6ab6aff202 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 30 Dec 2017 21:39:18 +0100 Subject: [PATCH 3/3] Added phpstan config file to export-ignore list --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index 3946beb0..13bb2a91 100644 --- a/.gitattributes +++ b/.gitattributes @@ -22,3 +22,4 @@ indocker export-ignore phpcs.xml export-ignore phpunit.xml.dist export-ignore phpunit-func.xml export-ignore +phpstan.neon