StringPhpTypeSchemaResolver::getWeight()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Router\OpenApi\PhpTypeSchemaResolver;
15
16
use ReflectionParameter;
17
use ReflectionProperty;
18
use Reflector;
19
use SensitiveParameter;
0 ignored issues
show
Bug introduced by
The type SensitiveParameter 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...
20
use Sunrise\Http\Router\OpenApi\Exception\UnsupportedPhpTypeException;
21
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverInterface;
22
use Sunrise\Http\Router\OpenApi\Type;
23
24
/**
25
 * @since 3.0.0
26
 */
27
final class StringPhpTypeSchemaResolver implements OpenApiPhpTypeSchemaResolverInterface
28
{
29 3
    public function supportsPhpType(Type $phpType, Reflector $phpTypeHolder): bool
30
    {
31 3
        return $phpType->name === Type::PHP_TYPE_NAME_STRING;
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37 3
    public function resolvePhpTypeSchema(Type $phpType, Reflector $phpTypeHolder): array
38
    {
39 3
        $this->supportsPhpType($phpType, $phpTypeHolder) or throw new UnsupportedPhpTypeException();
40
41 3
        $phpTypeSchema = [
42 3
            'type' => Type::OAS_TYPE_NAME_STRING,
43 3
        ];
44
45 3
        if ($phpTypeHolder instanceof ReflectionParameter) {
46 3
            if ($phpTypeHolder->getAttributes(SensitiveParameter::class) !== []) {
47 3
                $phpTypeSchema['format'] = 'password';
48
            }
49
        }
50
51 3
        if ($phpTypeHolder instanceof ReflectionProperty) {
52 3
            foreach ($phpTypeHolder->getDeclaringClass()->getConstructor()?->getParameters() ?? [] as $parameter) {
53 3
                if ($parameter->name === $phpTypeHolder->name) {
54 3
                    if ($parameter->isPromoted() && $parameter->getAttributes(SensitiveParameter::class) !== []) {
55 3
                        $phpTypeSchema['format'] = 'password';
56
                    }
57
58 3
                    break;
59
                }
60
            }
61
        }
62
63 3
        return $phpTypeSchema;
64
    }
65
66 3
    public function getWeight(): int
67
    {
68 3
        return 0;
69
    }
70
}
71