From 7587fbb32b239a99621df1ec807ecf50802dc4b5 Mon Sep 17 00:00:00 2001 From: Juan Pablo Barreto <2080215+juampi92@users.noreply.github.com> Date: Mon, 23 Jan 2023 08:47:47 +0100 Subject: [PATCH] Php 8.2 & phpstan (#13) * Add PHPStan + type hints * Add support for PHP 8.2 --- .github/workflows/run-tests.yml | 4 ++- composer.json | 8 +++--- phpstan.neon | 6 +++++ src/APIResource.php | 20 +++++++++----- src/APIResourceManager.php | 28 +++++++++++--------- src/Exceptions/APIDeprecatedException.php | 1 + src/Exceptions/ResourceNotFoundException.php | 4 +++ src/Facades/APIResource.php | 4 +-- src/helpers.php | 3 ++- 9 files changed, 51 insertions(+), 27 deletions(-) create mode 100644 phpstan.neon diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index dfafb2f..5ed8be6 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -9,7 +9,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - php: [8.1, 8.0, 7.4] + php: [8.2, 8.1, 8.0, 7.4] laravel: [9.*, 8.*, 7.*] stability: [prefer-stable] include: @@ -24,6 +24,8 @@ jobs: php: 7.4 - laravel: 7.* php: 8.1 + - laravel: 7.* + php: 8.2 name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} diff --git a/composer.json b/composer.json index f8b2dd1..faae4a6 100644 --- a/composer.json +++ b/composer.json @@ -11,14 +11,15 @@ } ], "require": { - "php" : "^7.4|^8.0|^8.1", + "php" : "^7.4|^8.0|^8.1|^8.2", "illuminate/support": "^7.0|^8.0|^9.0", "illuminate/http": "^7.0|^8.0|^9.0" }, "require-dev": { "orchestra/testbench": "^5.0|^6.0|^7.0", "phpunit/phpunit": "^9.4", - "friendsofphp/php-cs-fixer": "^3.8" + "friendsofphp/php-cs-fixer": "^3.8", + "phpstan/phpstan": "^1.9" }, "autoload": { "psr-4": { @@ -36,7 +37,8 @@ "scripts": { "test": "vendor/bin/phpunit --colors=always", "test-coverage": "vendor/bin/phpunit --coverage-html coverage", - "php-cs-fix": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php" + "php-cs-fix": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php", + "phpstan": "vendor/bin/phpstan analyse -c phpstan.neon" }, "extra": { "laravel": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..43eda1e --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,6 @@ +parameters: + level: 8 + paths: + - src + ignoreErrors: + - '#Parameter \#2 \$args of function forward_static_call_array expects array\, array\ given.#' diff --git a/src/APIResource.php b/src/APIResource.php index 3f66a43..305173b 100644 --- a/src/APIResource.php +++ b/src/APIResource.php @@ -2,12 +2,18 @@ namespace Juampi92\APIResources; +use Illuminate\Http\Resources\Json\JsonResource; + +/** + * @template TResource of JsonResource + */ class APIResource { + /** @var class-string */ protected $path; /** - * @param $path + * @param class-string $path */ public function __construct($path) { @@ -15,9 +21,9 @@ public function __construct($path) } /** - * @param array ...$args + * @param mixed ...$args * - * @return \Illuminate\Http\Resources\Json\Resource + * @return JsonResource */ public function with(...$args) { @@ -25,9 +31,9 @@ public function with(...$args) } /** - * @param array ...$args + * @param mixed ...$args * - * @return \Illuminate\Http\Resources\Json\Resource + * @return JsonResource */ public function make(...$args) { @@ -35,9 +41,9 @@ public function make(...$args) } /** - * @param array ...$args + * @param mixed ...$args * - * @return \Illuminate\Http\Resources\Json\Resource + * @return JsonResource */ public function collection(...$args) { diff --git a/src/APIResourceManager.php b/src/APIResourceManager.php index c7e7ce3..071d472 100644 --- a/src/APIResourceManager.php +++ b/src/APIResourceManager.php @@ -3,6 +3,7 @@ namespace Juampi92\APIResources; use Exception; +use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\Str; use Juampi92\APIResources\Exceptions\ResourceNotFoundException; @@ -19,7 +20,7 @@ class APIResourceManager protected $path; /** - * @var string + * @var string|null */ protected $apiName; @@ -34,7 +35,7 @@ class APIResourceManager protected $latest; /** - * @var string + * @var string|null */ protected $routePath; @@ -74,7 +75,7 @@ public function getRouteName($route) * Returns the versioned url. * * @param string $name - * @param array $parameters + * @param array<\Illuminate\Contracts\Routing\UrlRoutable|string|\BackedEnum> $parameters * @param bool $absolute * @return string */ @@ -106,7 +107,7 @@ protected function getConfig($cfg, $name = null) * Sets the current API version. * * @param string $current - * @param string $apiName = null + * @param string|null $apiName = null * * @return $this */ @@ -164,7 +165,7 @@ public function isLatest($current = null) * * @param string $classname * - * @return string + * @return class-string * @throws ResourceNotFoundException */ public function resolveClassname($classname) @@ -198,7 +199,7 @@ public function resolveClassname($classname) * @param string $classname * @param bool $forceLatest Set to true if last version is required * - * @return string + * @return class-string */ protected function parseClassname($classname, $forceLatest = false) { @@ -212,6 +213,7 @@ protected function parseClassname($classname, $forceLatest = false) $path = "\\{$this->path}\\{$path}"; + // @phpstan-ignore-next-line return $path; } @@ -222,7 +224,7 @@ protected function parseClassname($classname, $forceLatest = false) * * @param string $classname * - * @return APIResource + * @return APIResource * @throws Exceptions\ResourceNotFoundException */ public function resolve($classname) @@ -237,9 +239,9 @@ public function resolve($classname) } // Search on the latest version - $path = $this->resolveClassname($classname, true); + $path = $this->resolveClassname($classname); - // If still does not exists, fail + // If still does not exist, fail if (! class_exists($path)) { throw new Exceptions\ResourceNotFoundException($classname, $path); } @@ -250,9 +252,9 @@ public function resolve($classname) /** * @param string $classname - * @param array $args + * @param mixed ...$args * - * @return \Illuminate\Http\Resources\Json\Resource + * @return JsonResource */ public function make($classname, ...$args) { @@ -263,9 +265,9 @@ public function make($classname, ...$args) /** * @param string $classname - * @param array ...$args + * @param mixed ...$args * - * @return \Illuminate\Http\Resources\Json\Resource + * @return JsonResource */ public function collection($classname, ...$args) { diff --git a/src/Exceptions/APIDeprecatedException.php b/src/Exceptions/APIDeprecatedException.php index 0fd7310..c1075cb 100644 --- a/src/Exceptions/APIDeprecatedException.php +++ b/src/Exceptions/APIDeprecatedException.php @@ -12,6 +12,7 @@ class APIDeprecatedException extends \Exception */ public function __construct() { + // @phpstan-ignore-next-line parent::__construct(trans('errors.api.deprecated'), IlluminateResponse::HTTP_MOVED_PERMANENTLY); } } diff --git a/src/Exceptions/ResourceNotFoundException.php b/src/Exceptions/ResourceNotFoundException.php index b2574a2..1171f25 100644 --- a/src/Exceptions/ResourceNotFoundException.php +++ b/src/Exceptions/ResourceNotFoundException.php @@ -4,6 +4,10 @@ class ResourceNotFoundException extends \Exception { + /** + * @param string $classname + * @param string $path + */ public function __construct($classname, $path) { parent::__construct("The resource {$classname} was not found. Path: {$path}"); diff --git a/src/Facades/APIResource.php b/src/Facades/APIResource.php index 7740cdd..8b3279e 100644 --- a/src/Facades/APIResource.php +++ b/src/Facades/APIResource.php @@ -10,8 +10,8 @@ * @method static bool isLatest(string $c = null) * @method static string resolveClassname(string $classname, bool $forceLatest = null) Returns formatted classname using current version * @method static \Juampi92\APIResources\APIResource resolve(string $classname) - * @method static \Illuminate\Http\Resources\Json\Resource make(string $classname, ...$args) Resolves the classname and instantiates the resource - * @method static \Illuminate\Http\Resources\Json\Resource collection(string $classname, ...$args) Resolves the classname and instantiates the resource as a collection + * @method static \Illuminate\Http\Resources\Json\JsonResource make(string $classname, ...$args) Resolves the classname and instantiates the resource + * @method static \Illuminate\Http\Resources\Json\JsonResource collection(string $classname, ...$args) Resolves the classname and instantiates the resource as a collection * @method static string getRoute(string $name, array $parameters, bool $absolute) * @method static string getRouteName(string $name) */ diff --git a/src/helpers.php b/src/helpers.php index 0ee6f75..036fd39 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,5 +1,6 @@ */ function api_resource($classname) {