Passed
Pull Request — 1.x (#321)
by Akihito
03:22 queued 57s
created

OptionalParam::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource\Options;
6
7
use BEAR\Resource\ParamInterface;
8
use Ray\Di\InjectorInterface;
9
use function ltrim;
10
use function preg_replace;
11
use function strtolower;
12
13
/** @template T */
14
final class OptionalParam implements ParamInterface
15
{
16
    /** @param T $defaultValue */
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\Options\T 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...
17
    public function __construct(
18
        private $defaultValue,
19
    ) {
20
    }
21
22
    /**
23
     * {@inheritDoc}
24
     */
25
    public function __invoke(string $varName, array $query, InjectorInterface $injector)
26
    {
27
        unset($injector);
28
        if (isset($query[$varName])) {
29
            return $query[$varName];
30
        }
31
32
        // try camelCase variable name
33
        $snakeName = ltrim(strtolower((string) preg_replace('/[A-Z]/', '_\0', $varName)), '_');
34
35
        return $query[$snakeName] ?? $this->defaultValue;
36
    }
37
}
38