Passed
Pull Request — 1.x (#321)
by Akihito
02:24
created

OptionalParam   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 22
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 11 2
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
10
use function ltrim;
11
use function preg_replace;
12
use function strtolower;
13
14
/** @template T */
15
final class OptionalParam implements ParamInterface
16
{
17
    /** @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...
18
    public function __construct(
19
        private $defaultValue,
20
    ) {
21
    }
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    public function __invoke(string $varName, array $query, InjectorInterface $injector)
27
    {
28
        unset($injector);
29
        if (isset($query[$varName])) {
30
            return $query[$varName];
31
        }
32
33
        // try camelCase variable name
34
        $snakeName = ltrim(strtolower((string) preg_replace('/[A-Z]/', '_\0', $varName)), '_');
35
36
        return $query[$snakeName] ?? $this->defaultValue;
37
    }
38
}
39