PropertyMetadata   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 129
loc 129
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassName() 4 4 1
A getPropertyName() 4 4 1
A getDefaultGroup() 4 4 1
A getGroup() 4 4 3
A addConstraint() 9 9 2
A addConstraints() 6 6 2
A getConstraints() 4 4 1
A getConstraintsByGroup() 4 4 2
A mergeConstraints() 6 6 2
A serialize() 9 9 1
A unserialize() 11 11 1

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 Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Validator\Mapping;
12
13
use Cubiche\Core\Validator\Assert;
14
use Metadata\PropertyMetadata as BasePropertyMetadata;
15
16
/**
17
 * PropertyMetadata class.
18
 *
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21 View Code Duplication
class PropertyMetadata extends BasePropertyMetadata
0 ignored issues
show
Duplication introduced by
This class 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...
22
{
23
    /**
24
     * @var string
25
     */
26
    public $defaultGroup;
27
28
    /**
29
     * @var array
30
     */
31
    public $constraintsByGroup = array();
32
33
    /**
34
     * @return string
35
     */
36
    public function getClassName()
37
    {
38
        return $this->class;
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getPropertyName()
45
    {
46
        return $this->name;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getDefaultGroup()
53
    {
54
        return $this->defaultGroup;
55
    }
56
57
    /**
58
     * @param string $group
59
     *
60
     * @return string
61
     */
62
    protected function getGroup($group = null)
63
    {
64
        return $group === null || empty($group) ? $this->defaultGroup : $group;
65
    }
66
67
    /**
68
     * @param Assert $constraint
69
     * @param string $group
70
     */
71
    public function addConstraint(Assert $constraint, $group = null)
72
    {
73
        $group = $this->getGroup($group);
74
        if (!isset($this->constraintsByGroup[$group])) {
75
            $this->constraintsByGroup[$group] = array();
76
        }
77
78
        $this->constraintsByGroup[$group][] = $constraint;
79
    }
80
81
    /**
82
     * @param array  $constraints
83
     * @param string $group
84
     */
85
    public function addConstraints(array $constraints, $group = null)
86
    {
87
        foreach ($constraints as $constraint) {
88
            $this->addConstraint($constraint, $group);
89
        }
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function getConstraints()
96
    {
97
        return $this->constraintsByGroup;
98
    }
99
100
    /**
101
     * @param $group
102
     *
103
     * @return array
104
     */
105
    public function getConstraintsByGroup($group)
106
    {
107
        return isset($this->constraintsByGroup[$group]) ? $this->constraintsByGroup[$group] : array();
108
    }
109
110
    /**
111
     * Merges the constraints of the given metadata into this object.
112
     *
113
     * @param PropertyMetadata $source
114
     */
115
    public function mergeConstraints(PropertyMetadata $source)
116
    {
117
        foreach ($source->getConstraints() as $group => $constraints) {
118
            $this->addConstraints($constraints, $group);
119
        }
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function serialize()
126
    {
127
        return serialize(array(
128
            $this->class,
129
            $this->name,
130
            $this->defaultGroup,
131
            $this->constraintsByGroup,
132
        ));
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function unserialize($str)
139
    {
140
        list(
141
            $this->class,
142
            $this->name,
143
            $this->defaultGroup,
144
            $this->constraintsByGroup) = unserialize($str);
145
146
        $this->reflection = new \ReflectionProperty($this->class, $this->name);
147
        $this->reflection->setAccessible(true);
148
    }
149
}
150