OptionsMethodRequest::getRequired()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use ReflectionException;
8
use ReflectionMethod;
9
use ReflectionNamedType;
10
use ReflectionParameter;
11
12
use function assert;
13
use function is_array;
14
use function is_string;
15 7
use function method_exists;
16
17 7
/**
18 7
 * @psalm-import-type OptionsResponse from Types
19
 * @psalm-import-type InsMap from Types
20 7
 * @psalm-import-type ParameterMetadata from Types
21
 * @psalm-import-type ParametersMap from Types
22 7
 * @psalm-import-type RequiredParameterList from Types
23
 * @psalm-import-type ReflectionParameterList from Types
24 7
 */
25
final class OptionsMethodRequest
26
{
27
    /**
28
     * @param ParametersMap $paramDoc
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\ParametersMap 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
     * @param InsMap        $ins
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\InsMap 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30 7
     *
31
     * @return OptionsResponse
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\OptionsResponse 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32 7
     */
33 7
    public function __invoke(ReflectionMethod $method, array $paramDoc, array $ins): array
34 7
    {
35
        return $this->getParamMetas($method->getParameters(), $paramDoc, $ins);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getParamMe...ers(), $paramDoc, $ins) returns the type array which is incompatible with the documented return type BEAR\Resource\OptionsResponse.
Loading history...
36 3
    }
37 2
38
    /**
39 1
     * @param ParametersMap $paramDoc
40
     *
41 7
     * @psalm-suppress RedundantCondition for BC
42
     */
43 7
    private function getParameterType(ReflectionParameter $parameter, array $paramDoc, string $name): string|null
44 7
    {
45 2
        /** @phpstan-ignore function.alreadyNarrowedType */
46
        $hasType = method_exists($parameter, 'getType') && $parameter->getType();
47 7
        if ($hasType) {
48 4
            return $this->getType($parameter);
49
        }
50 7
51 7
        return $paramDoc[$name]['type'] ?? null;
52
    }
53 7
54
    /**
55 7
     * @param ReflectionParameterList $parameters
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\ReflectionParameterList 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
     * @param ParametersMap           $paramDoc
57
     * @param InsMap                  $ins
58
     *
59
     * @return OptionsResponse
60
     */
61 7
    private function getParamMetas(array $parameters, array $paramDoc, array $ins): array
62
    {
63 7
        foreach ($parameters as $parameter) {
64 7
            $name = (string) $parameter->name;
65 7
            if (isset($ins[$name])) {
66 7
                $paramDoc[$name]['in'] = $ins[$parameter->name];
67
            }
68
69
            if (! isset($paramDoc[$parameter->name])) {
70 7
                $paramDoc[$name] = [];
71
            }
72
73 7
            $paramDoc = $this->paramType($paramDoc, $parameter);
74
            $paramDoc = $this->paramDefault($paramDoc, $parameter);
75 7
        }
76 7
77 2
        $required = $this->getRequired($parameters);
78
79
        return $this->setParamMetas($paramDoc, $required);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->setParamMetas($paramDoc, $required) returns the type array which is incompatible with the documented return type BEAR\Resource\OptionsResponse.
Loading history...
80 7
    }
81
82
    /**
83 7
     * @param ReflectionParameterList $parameters
84
     *
85 7
     * @return RequiredParameterList
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\RequiredParameterList 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
86 7
     */
87 7
    private function getRequired(array $parameters): array
88
    {
89
        $required = [];
90 7
        foreach ($parameters as $parameter) {
91
            if ($parameter->isOptional()) {
92
                continue;
93 7
            }
94
95 7
            $required[] = $parameter->name;
96 7
        }
97 4
98
        return $required;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $required returns the type array which is incompatible with the documented return type BEAR\Resource\RequiredParameterList.
Loading history...
99
    }
100 7
101
    /**
102
     * @param ParametersMap $paramDoc
103
     *
104
     * @return ParametersMap
105
     *
106 7
     * @throws ReflectionException
107
     */
108 7
    private function paramDefault(array $paramDoc, ReflectionParameter $parameter): array
109 7
    {
110 5
        $hasDefault = $parameter->isDefaultValueAvailable() && $parameter->getDefaultValue() !== null;
111 1
        if ($hasDefault) {
112 1
            $default = $parameter->getDefaultValue();
113
            $paramDoc[(string) $parameter->name]['default'] = is_array($default) ? '[]' : (string) $parameter->getDefaultValue(); // @phpstan-ignore-lines
114 5
        }
115 5
116
        return $paramDoc;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $paramDoc returns the type array which is incompatible with the documented return type BEAR\Resource\ParametersMap.
Loading history...
117
    }
118
119 7
    /**
120
     * @param ParametersMap $paramDoc
121
     *
122
     * @return ParametersMap
123
     */
124
    private function paramType(array $paramDoc, ReflectionParameter $parameter): array
125 1
    {
126
        $type = $this->getParameterType($parameter, $paramDoc, $parameter->name);
127 1
        if (is_string($type)) {
128 1
            $paramDoc[(string) $parameter->name]['type'] = $type; // override type parameter by reflection over phpdoc param type
129 1
        }
130
131
        return $paramDoc;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $paramDoc returns the type array which is incompatible with the documented return type BEAR\Resource\ParametersMap.
Loading history...
132 1
    }
133
134
    private function getType(ReflectionParameter $parameter): string
135 7
    {
136
        $namedType = $parameter->getType();
137 7
        assert($namedType instanceof ReflectionNamedType);
138 7
        $type = $namedType->getName();
139 7
        if ($type === 'int') {
140
            $type = 'integer';
141 7
        }
142 7
143
        return $type;
144
    }
145 7
146
    /**
147
     * @param ParametersMap         $paramDoc
148
     * @param RequiredParameterList $required
149
     *
150
     * @return OptionsResponse
151
     */
152
    private function setParamMetas(array $paramDoc, array $required): array
153
    {
154
        $paramMetas = [];
155
        if ((bool) $paramDoc) {
156
            $paramMetas['parameters'] = $paramDoc;
157
        }
158
159
        if ((bool) $required) {
160
            $paramMetas['required'] = $required;
161
        }
162
163
        return $paramMetas;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $paramMetas returns the type array which is incompatible with the documented return type BEAR\Resource\OptionsResponse.
Loading history...
164
    }
165
}
166