bearsunday /
BEAR.Resource
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace BEAR\Resource; |
||||
| 6 | |||||
| 7 | use LogicException; |
||||
| 8 | use ReflectionClass; |
||||
| 9 | use ReflectionMethod; |
||||
| 10 | |||||
| 11 | use function array_shift; |
||||
| 12 | use function assert; |
||||
| 13 | use function class_exists; |
||||
| 14 | use function explode; |
||||
| 15 | use function implode; |
||||
| 16 | use function is_string; |
||||
| 17 | use function str_starts_with; |
||||
| 18 | use function strtolower; |
||||
| 19 | use function substr; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * @psalm-import-type PackageMetadata from Types |
||||
| 23 | * @psalm-import-type ResourceClassName from Types |
||||
| 24 | */ |
||||
| 25 | final class Meta |
||||
| 26 | { |
||||
| 27 | private const EXTRAS_VENDOR = 'vendor'; |
||||
| 28 | private const EXTRAS_PACKAGE = 'package'; |
||||
| 29 | |||||
| 30 | /** @var string */ |
||||
| 31 | public $uri; |
||||
| 32 | |||||
| 33 | /** @var Options */ |
||||
| 34 | public $options; |
||||
| 35 | |||||
| 36 | /** @var PackageMetadata */ |
||||
|
0 ignored issues
–
show
|
|||||
| 37 | public $extras = []; |
||||
| 38 | |||||
| 39 | /** @param ResourceClassName $class */ |
||||
|
0 ignored issues
–
show
The type
BEAR\Resource\ResourceClassName was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 40 | public function __construct(string $class) |
||||
| 41 | { |
||||
| 42 | $this->uri = $this->getUri($class); |
||||
| 43 | $this->options = $this->getOptions($class); |
||||
| 44 | } |
||||
| 45 | |||||
| 46 | private function getUri(string $class): string |
||||
| 47 | { |
||||
| 48 | /** @var list<string> $classPath */ |
||||
| 49 | $classPath = explode('\\', $class); |
||||
| 50 | // $class |
||||
| 51 | $vendor = array_shift($classPath); |
||||
|
0 ignored issues
–
show
$classPath of type BEAR\Resource\list is incompatible with the type array expected by parameter $array of array_shift().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 52 | assert(is_string($vendor)); |
||||
| 53 | $this->extras[self::EXTRAS_VENDOR] = $vendor; |
||||
| 54 | $package = array_shift($classPath); |
||||
| 55 | assert(is_string($package)); |
||||
| 56 | $this->extras[self::EXTRAS_PACKAGE] = $package; |
||||
| 57 | array_shift($classPath); // "/Resource/" |
||||
| 58 | $scheme = array_shift($classPath); |
||||
| 59 | assert(is_string($scheme)); |
||||
| 60 | |||||
| 61 | return strtolower("{$scheme}://self/" . implode('/', $classPath)); |
||||
| 62 | } |
||||
| 63 | |||||
| 64 | /** |
||||
| 65 | * Return available resource request method |
||||
| 66 | * |
||||
| 67 | * @param ResourceClassName $class |
||||
| 68 | */ |
||||
| 69 | private function getOptions(string $class): Options |
||||
| 70 | { |
||||
| 71 | if (! class_exists($class)) { |
||||
| 72 | throw new LogicException(); // @codeCoverageIgnore |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | $ref = new ReflectionClass($class); |
||||
| 76 | $allows = $this->getAllows($ref->getMethods()); |
||||
| 77 | $params = []; |
||||
| 78 | foreach ($allows as $method) { |
||||
| 79 | $params[] = $this->getParams($class, $method); |
||||
| 80 | } |
||||
| 81 | |||||
| 82 | return new Options($allows, $params); |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | /** |
||||
| 86 | * @param ReflectionMethod[] $methods |
||||
| 87 | * |
||||
| 88 | * @return string[] |
||||
| 89 | * @psalm-return list<string> |
||||
| 90 | */ |
||||
| 91 | private function getAllows(array $methods): array |
||||
| 92 | { |
||||
| 93 | $allows = []; |
||||
| 94 | foreach ($methods as $method) { |
||||
| 95 | $isRequestMethod = str_starts_with($method->name, 'on') && ! str_starts_with($method->name, 'onLink'); |
||||
| 96 | if (! $isRequestMethod) { |
||||
| 97 | continue; |
||||
| 98 | } |
||||
| 99 | |||||
| 100 | $allows[] = strtolower(substr($method->name, 2)); |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | return $allows; |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | /** @param ResourceClassName $class */ |
||||
| 107 | private function getParams(string $class, string $method): Params |
||||
| 108 | { |
||||
| 109 | $refMethod = new ReflectionMethod($class, 'on' . $method); |
||||
| 110 | $parameters = $refMethod->getParameters(); |
||||
| 111 | $optionalParams = $requiredParams = []; |
||||
| 112 | foreach ($parameters as $parameter) { |
||||
| 113 | $name = $parameter->name; |
||||
| 114 | if ($parameter->isOptional()) { |
||||
| 115 | $optionalParams[] = $name; |
||||
| 116 | |||||
| 117 | continue; |
||||
| 118 | } |
||||
| 119 | |||||
| 120 | $requiredParams[] = $name; |
||||
| 121 | } |
||||
| 122 | |||||
| 123 | return new Params($method, $requiredParams, $optionalParams); |
||||
| 124 | } |
||||
| 125 | } |
||||
| 126 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths