ResolverTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 33
ccs 11
cts 15
cp 0.7332
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B resolveValue() 0 30 7
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\RestBundle\Util;
13
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
16
/**
17
 * @author Ener-Getick <[email protected]>
18
 *
19
 * @internal do not use this trait or its functions in your code
20
 */
21
trait ResolverTrait
22
{
23 29
    private function resolveValue(ContainerInterface $container, $value)
24
    {
25 29
        if (is_array($value)) {
26
            foreach ($value as $key => $val) {
27
                $value[$key] = $this->resolveValue($container, $val);
28
            }
29
30
            return $value;
31
        }
32
33 29
        if (!is_string($value)) {
34 19
            return $value;
35
        }
36
37
        $escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($container, $value) {
38
            // skip %%
39 6
            if (!isset($match[1])) {
40 6
                return '%%';
41
            }
42
43 6
            $resolved = $container->getParameter($match[1]);
44 6
            if (is_string($resolved) || is_numeric($resolved)) {
45 6
                return (string) $resolved;
46
            }
47
48
            throw new \RuntimeException(sprintf('The container parameter "%s" must be a string or numeric, but it is of type %s.', $match[1], gettype($resolved)));
49 16
        }, $value);
50
51 16
        return str_replace('%%', '%', $escapedValue);
52
    }
53
}
54