GenericResolver   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 21
loc 105
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 29 5
A __invoke() 0 4 1
A resolve() 5 31 4

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
namespace Charcoal\Factory;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Converts the given **type** into a **class name**.
9
 */
10
class GenericResolver
11
{
12
    /**
13
     * @var string $prefix
14
     */
15
    private $prefix = '';
16
17
    /**
18
     * @var string $suffix
19
     */
20
    private $suffix = '';
21
22
    /**
23
     * @var array $capitals
24
     */
25
    private $capitals;
26
27
    /**
28
     * @var array $replacements
29
     */
30
    private $replacements;
31
32
    /**
33
     * @param array $data Optional class dependencies. Will use default values if none are provided.
34
     */
35
    public function __construct(array $data = null)
36
    {
37
        if (!isset($data['prefix'])) {
38
            $data['prefix'] = '';
39
        }
40
        if (!isset($data['suffix'])) {
41
            $data['suffix'] = '';
42
        }
43 View Code Duplication
        if (!isset($data['capitals'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
44
            $data['capitals'] = [
45
                '-',
46
                '\\',
47
                '/',
48
                '.',
49
                '_'
50
            ];
51
        }
52 View Code Duplication
        if (!isset($data['replacements'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
            $data['replacements'] = [
54
                '-'=>'',
55
                '/'=>'\\',
56
                '.'=>'_'
57
            ];
58
        }
59
        $this->prefix = $data['prefix'];
60
        $this->suffix = $data['suffix'];
61
        $this->capitals = $data['capitals'];
62
        $this->replacements = $data['replacements'];
63
    }
64
65
    /**
66
     * Resolver needs to be callable
67
     *
68
     * @param string $type The "type" of object to resolve (the object ident).
69
     * @return string The resolved class name (FQN).
70
     */
71
    public function __invoke($type)
72
    {
73
        return $this->resolve($type);
74
    }
75
76
    /**
77
     * Resolve the class name from the requested type.
78
     *
79
     * @param string $type The "type" of object to resolve (the object ident).
80
     * @throws InvalidArgumentException If the type parameter is not a string.
81
     * @return string The resolved class name (FQN).
82
     */
83
    public function resolve($type)
84
    {
85
        if (!is_string($type)) {
86
            throw new InvalidArgumentException(
87
                'Can not resolve class ident: type must be a string'
88
            );
89
        }
90
91
        // Normalize requested type with prefix / suffix, if applicable.
92
        $type = $this->prefix.$type.$this->suffix;
93
94
        $capitalizeNext = function(&$i) {
95
            $i = ucfirst($i);
96
        };
97
98
        $capitals = $this->capitals;
99 View Code Duplication
        foreach ($capitals as $cap) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
100
            $expl = explode($cap, $type);
101
            array_walk($expl, $capitalizeNext);
102
            $type = implode($cap, $expl);
103
        }
104
105
        $replacements = $this->replacements;
106
        foreach ($replacements as $rep => $target) {
107
            $type = str_replace($rep, $target, $type);
108
        }
109
110
        $class = '\\'.trim($type, '\\');
111
112
        return $class;
113
    }
114
}
115