Completed
Push — master ( e9d1ee...cb5c09 )
by Théo
06:38
created

BuiltParameterResolver   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 118
Duplicated Lines 29.66 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 35
loc 118
ccs 38
cts 40
cp 0.95
rs 10
c 2
b 2
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B resolveValue() 16 16 5
A resolveArray() 9 9 2
A resolveEnvironmentValue() 10 10 2
A resolve() 0 4 1
B resolveString() 0 24 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the LaravelYaml package.
5
 *
6
 * (c) Théo FIDRY <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Fidry\LaravelYaml\DependencyInjection\Resolver;
13
14
use Fidry\LaravelYaml\Exception\DependencyInjection\Resolver\ParameterCircularReferenceException;
15
use Fidry\LaravelYaml\Exception\ParameterNotFoundException;
16
use Illuminate\Contracts\Config\Repository as ConfigRepository;
17
18
/**
19
 * @author Théo FIDRY <[email protected]>
20
 */
21
final class BuiltParameterResolver implements ParameterResolverInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    private $defaultValue;
27
28
    /**
29
     * @var ConfigRepository
30
     */
31
    private $config;
32
33
    /**
34
     * @var array
35
     */
36
    private $parameters;
37
38 6
    public function __construct(array $parameters, ConfigRepository $config)
39
    {
40 6
        $this->parameters = $parameters;
41 6
        $this->config = $config;
42 6
        $this->defaultValue = spl_object_hash(new \stdClass());
43 6
    }
44
45
    /**
46
     * {@inheritdoc}array
47
     */
48 6
    public function resolve($value)
49
    {
50 6
        return $this->resolveValue($value);
51
    }
52
53
    /**
54
     * @param mixed $value
55
     * @param array $resolving
56
     *
57
     * @return mixed
58
     *
59
     * @throws ParameterCircularReferenceException
60
     * @throws ParameterNotFoundException
61
     */
62 6 View Code Duplication
    private function resolveValue($value, $resolving = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64 6
        if (is_bool($value) || is_numeric($value)) {
65 3
            return $value;
66
        }
67
68 6
        if (is_array($value)) {
69 3
            return $this->resolveArray($value, $resolving);
70
        }
71
72 6
        if (is_string($value)) {
73 6
            return $this->resolveString($value, $resolving);
74
        }
75
76 3
        return $value;
77
    }
78
79 3 View Code Duplication
    private function resolveArray(array $arrayValue, array $resolving)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81 3
        $resolvedValue = [];
82 3
        foreach ($arrayValue as $key => $value) {
83 3
            $resolvedValue[$key] = $this->resolveValue($value, $resolving);
84 1
        }
85
86 3
        return $resolvedValue;
87
    }
88
89
    /**
90
     * @param $value
91
     * @param $resolving
92
     *
93
     * @return array|mixed
94
     * @throws ParameterCircularReferenceException
95
     * @throws ParameterNotFoundException
96
     */
97 6
    private function resolveString($value, array $resolving)
98
    {
99 6
        if (0 === preg_match('/^%([^%\s]+)%$/', $value, $match)) {
100 3
            if (false === array_key_exists($value, $resolving)) {
101 3
                return $value;
102
            }
103
104
            $key = $value;
105
        } else {
106 6
            $key = $match[1];
107
        }
108
109 6
        if (array_key_exists($key, $this->parameters)) {
110 3
            $resolving[$key] = true;
111
112 3
            return $this->resolveValue($this->parameters[$key], $resolving);
113
        }
114
115 6
        if ($this->config->has($key)) {
116 3
            return $this->config->get($key);
117
        }
118
119 6
        return $this->resolveEnvironmentValue($key);
120
    }
121
122
    /**
123
     * @param string $key
124
     *
125
     * @return string|int|bool|null
126
     * @throws ParameterNotFoundException
127
     */
128 6 View Code Duplication
    private function resolveEnvironmentValue($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130 6
        $environmentKey = strtoupper(str_replace('.', '_', $key));
131 6
        $value = env($environmentKey, $this->defaultValue);
132 6
        if ($this->defaultValue !== $value) {
133 3
            return $value;
134
        }
135
136 3
        throw new ParameterNotFoundException(sprintf('No parameter "%s" found', $key));
137
    }
138
}
139