Config   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 148
Duplicated Lines 21.62 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 51.61%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 2
cbo 0
dl 32
loc 148
ccs 16
cts 31
cp 0.5161
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getConfig() 0 4 1
A getMultiValued() 0 4 1
A isMultiValued() 0 4 1
A hasFieldConfig() 0 4 1
A getFieldConfig() 0 6 2
A hasFieldConfigKey() 8 8 2
A hasFieldConfigValue() 8 8 2
A getFieldConfigValueByKey() 8 8 3
A getFieldConfigKey() 8 8 2

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 TreeHouse\Model\Config;
4
5
/**
6
 * Class containing configuration for models.
7
 */
8
class Config
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $config;
14
15
    /**
16
     * @var array
17
     */
18
    protected $multiValued;
19
20
    /**
21
     * @param array $config
22
     * @param array $multiValued
23
     */
24 18
    public function __construct(array $config, array $multiValued = [])
25
    {
26 18
        $this->config      = $config;
27 18
        $this->multiValued = $multiValued;
28 18
    }
29
30
    /**
31
     * Returns the complete configuration.
32
     *
33
     * @return array
34
     */
35 18
    public function getConfig()
36
    {
37 18
        return $this->config;
38
    }
39
40
    /**
41
     * Returns the multiValued state of fields.
42
     *
43
     * @return array
44
     */
45
    public function getMultiValued()
46
    {
47
        return $this->multiValued;
48
    }
49
50
    /**
51
     * Checks if a field is multi-valued
52
     *
53
     * @param  string  $name
54
     *
55
     * @return boolean
56
     */
57 14
    public function isMultiValued($name)
58
    {
59 14
        return $this->multiValued[$name];
60
    }
61
62
    /**
63
     * Checks if a configuration exists for a given field
64
     *
65
     * @param  string  $name
66
     *
67
     * @return boolean
68
     */
69 18
    public function hasFieldConfig($name)
70
    {
71 18
        return array_key_exists($name, $this->getConfig());
72
    }
73
74
    /**
75
     * Returns the configuration for a field, if it exists
76
     *
77
     * @param  string     $name
78
     *
79
     * @return array|null
80
     */
81 10
    public function getFieldConfig($name)
82
    {
83 10
        $config = $this->getConfig();
84
85 10
        return array_key_exists($name, $config) ? $config[$name] : null;
86
    }
87
88
    /**
89
     * Checks if an key exists for a given field config
90
     *
91
     * @param  string  $config
92
     * @param  integer $key
93
     *
94
     * @return boolean
95
     */
96 10 View Code Duplication
    public function hasFieldConfigKey($config, $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...
97
    {
98 10
        if (null === $values = $this->getFieldConfig($config)) {
99
            return false;
100
        }
101
102 10
        return array_key_exists($key, $values);
103
    }
104
105
    /**
106
     * Checks if a name-value exists for a given field config
107
     *
108
     * @param  string  $field
109
     * @param  string  $value
110
     *
111
     * @return boolean
112
     */
113 View Code Duplication
    public function hasFieldConfigValue($field, $value)
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...
114
    {
115
        if (null === $values = $this->getFieldConfig($field)) {
116
            return false;
117
        }
118
119
        return false !== array_search($value, $values);
120
    }
121
122
    /**
123
     * Returns the name for a field config value
124
     *
125
     * @param  string      $config
126
     * @param  integer     $key
127
     *
128
     * @return string|null
129
     */
130 View Code Duplication
    public function getFieldConfigValueByKey($config, $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...
131
    {
132
        if (null === $values = $this->getFieldConfig($config)) {
133
            return null;
134
        }
135
136
        return array_key_exists($key, $values) ? $values[$key] : null;
137
    }
138
139
    /**
140
     * Returns the key for a field config name-value
141
     *
142
     * @param  string       $config
143
     * @param  string       $value
144
     *
145
     * @return integer|null
146
     */
147 View Code Duplication
    public function getFieldConfigKey($config, $value)
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...
148
    {
149
        if (null === $values = $this->getFieldConfig($config)) {
150
            return null;
151
        }
152
153
        return array_search($value, $values);
154
    }
155
}
156