Skip to content

Commit

Permalink
Php 8.2 & phpstan (#13)
Browse files Browse the repository at this point in the history
* Add PHPStan + type hints
* Add support for PHP 8.2
  • Loading branch information
juampi92 authored Jan 23, 2023
1 parent da19e0e commit 7587fbb
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 27 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }}

Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: 8
paths:
- src
ignoreErrors:
- '#Parameter \#2 \$args of function forward_static_call_array expects array\<int, mixed\>, array\<int\|string, mixed\> given.#'
20 changes: 13 additions & 7 deletions src/APIResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,48 @@

namespace Juampi92\APIResources;

use Illuminate\Http\Resources\Json\JsonResource;

/**
* @template TResource of JsonResource
*/
class APIResource
{
/** @var class-string<TResource> */
protected $path;

/**
* @param $path
* @param class-string<TResource> $path
*/
public function __construct($path)
{
$this->path = $path;
}

/**
* @param array ...$args
* @param mixed ...$args
*
* @return \Illuminate\Http\Resources\Json\Resource
* @return JsonResource
*/
public function with(...$args)
{
return forward_static_call_array([$this->path, 'make'], $args);
}

/**
* @param array ...$args
* @param mixed ...$args
*
* @return \Illuminate\Http\Resources\Json\Resource
* @return JsonResource
*/
public function make(...$args)
{
return forward_static_call_array([$this->path, 'make'], $args);
}

/**
* @param array ...$args
* @param mixed ...$args
*
* @return \Illuminate\Http\Resources\Json\Resource
* @return JsonResource
*/
public function collection(...$args)
{
Expand Down
28 changes: 15 additions & 13 deletions src/APIResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Juampi92\APIResources;

use Exception;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;
use Juampi92\APIResources\Exceptions\ResourceNotFoundException;

Expand All @@ -19,7 +20,7 @@ class APIResourceManager
protected $path;

/**
* @var string
* @var string|null
*/
protected $apiName;

Expand All @@ -34,7 +35,7 @@ class APIResourceManager
protected $latest;

/**
* @var string
* @var string|null
*/
protected $routePath;

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -164,7 +165,7 @@ public function isLatest($current = null)
*
* @param string $classname
*
* @return string
* @return class-string<JsonResource>
* @throws ResourceNotFoundException
*/
public function resolveClassname($classname)
Expand Down Expand Up @@ -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<JsonResource>
*/
protected function parseClassname($classname, $forceLatest = false)
{
Expand All @@ -212,6 +213,7 @@ protected function parseClassname($classname, $forceLatest = false)

$path = "\\{$this->path}\\{$path}";

// @phpstan-ignore-next-line
return $path;
}

Expand All @@ -222,7 +224,7 @@ protected function parseClassname($classname, $forceLatest = false)
*
* @param string $classname
*
* @return APIResource
* @return APIResource<JsonResource>
* @throws Exceptions\ResourceNotFoundException
*/
public function resolve($classname)
Expand All @@ -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);
}
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions src/Exceptions/APIDeprecatedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 4 additions & 0 deletions src/Exceptions/ResourceNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
4 changes: 2 additions & 2 deletions src/Facades/APIResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down
3 changes: 2 additions & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Http\Resources\Json\JsonResource;
use Juampi92\APIResources\Facades\APIResource;

if (! function_exists('api_resource')) {
Expand All @@ -8,7 +9,7 @@
*
* @param string $classname
*
* @return \Juampi92\APIResources\APIResource
* @return \Juampi92\APIResources\APIResource<JsonResource>
*/
function api_resource($classname)
{
Expand Down

0 comments on commit 7587fbb

Please sign in to comment.