Template   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 179
Duplicated Lines 6.15 %

Coupling/Cohesion

Components 3
Dependencies 5

Importance

Changes 0
Metric Value
wmc 18
lcom 3
cbo 5
dl 11
loc 179
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __toString() 0 4 1
A addPage() 0 7 1
A setPages() 0 8 2
A removePage() 0 6 1
A getPages() 0 4 1
A setLayout() 0 6 1
A getLayout() 0 4 1
A setInheritors() 0 6 1
A getInheritors() 0 4 1
A getTemplateInheritors() 11 11 3
A validate() 0 14 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 Victoire\Bundle\TemplateBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Symfony\Component\Validator\Constraints as Assert;
8
use Symfony\Component\Validator\Context\ExecutionContextInterface;
9
use Victoire\Bundle\CoreBundle\Entity\View;
10
use Victoire\Bundle\PageBundle\Entity\BasePage;
11
12
/**
13
 * Template.
14
 *
15
 * @ORM\Entity(repositoryClass="Victoire\Bundle\TemplateBundle\Repository\TemplateRepository")
16
 */
17
class Template extends View
18
{
19
    const TYPE = 'template';
20
21
    /**
22
     * @var string
23
     *
24
     * @ORM\OneToMany(targetEntity="\Victoire\Bundle\CoreBundle\Entity\View", mappedBy="template")
25
     */
26
    protected $inheritors;
27
28
    /**
29
     * @var string
30
     *
31
     * @ORM\Column(name="layout", type="string", length=255)
32
     */
33
    protected $layout;
34
35
    /**
36
     * Construct.
37
     **/
38
    public function __construct()
39
    {
40
        parent::__construct();
41
        $this->widgets = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type string of property $widgets.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $this->inheritors = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type string of property $inheritors.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
        $this->pages = new ArrayCollection();
0 ignored issues
show
Bug introduced by
The property pages does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
    }
45
46
    /**
47
     * to string.
48
     *
49
     * @return string
50
     **/
51
    public function __toString()
52
    {
53
        return 'Modèle > '.$this->getName();
54
    }
55
56
    /**
57
     * add page.
58
     *
59
     * @param BasePage $page
60
     *
61
     * @return Template
62
     **/
63
    public function addPage(BasePage $page)
64
    {
65
        $page->setTemplate($this);
66
        $this->pages[] = $page;
67
68
        return $this;
69
    }
70
71
    /**
72
     * set page.
73
     *
74
     * @param array $pages
75
     *
76
     * @return Template
77
     **/
78
    public function setPages(array $pages)
79
    {
80
        foreach ($pages as $page) {
81
            $this->addPage($page);
82
        }
83
84
        return $this;
85
    }
86
87
    /**
88
     * remove page.
89
     *
90
     * @param BasePage $page
91
     *
92
     * @return Template
93
     **/
94
    public function removePage($page)
95
    {
96
        $this->pages->removeElement($page);
97
98
        return $this;
99
    }
100
101
    /**
102
     * Get pages (all Pages having this object as Template).
103
     *
104
     * @return ArrayCollection
105
     */
106
    public function getPages()
107
    {
108
        return $this->pages;
109
    }
110
111
    /**
112
     * Set layout.
113
     *
114
     * @param string $layout
115
     *
116
     * @return Template
117
     */
118
    public function setLayout($layout)
119
    {
120
        $this->layout = $layout;
121
122
        return $this;
123
    }
124
125
    /**
126
     * Get layout.
127
     *
128
     * @return string
129
     */
130
    public function getLayout()
131
    {
132
        return $this->layout;
133
    }
134
135
    /**
136
     * Set inheritors.
137
     *
138
     * @param string $inheritors
139
     *
140
     * @return Template
141
     */
142
    public function setInheritors($inheritors)
143
    {
144
        $this->inheritors = $inheritors;
145
146
        return $this;
147
    }
148
149
    /**
150
     * Get inheritors (all Views having this object as Template).
151
     *
152
     * @return [View]
0 ignored issues
show
Documentation introduced by
The doc-type [View] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
153
     */
154
    public function getInheritors()
155
    {
156
        return $this->inheritors;
157
    }
158
159
    /**
160
     * Get inheritors (all Templates having this object as Template).
161
     *
162
     * @return [Template]
0 ignored issues
show
Documentation introduced by
The doc-type [Template] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
163
     */
164 View Code Duplication
    public function getTemplateInheritors()
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...
165
    {
166
        $templateInheritors = [];
167
        foreach ($this->inheritors as $inheritor) {
0 ignored issues
show
Bug introduced by
The expression $this->inheritors of type string is not traversable.
Loading history...
168
            if ($inheritor instanceof self) {
169
                $templateInheritors[] = $inheritor;
170
            }
171
        }
172
173
        return $templateInheritors;
174
    }
175
176
    /**
177
     * @Assert\Callback(groups={"victoire"})
178
     *
179
     * @param ExecutionContextInterface $context
180
     */
181
    public function validate(ExecutionContextInterface $context)
182
    {
183
        $template = $this;
184
        while ($template != null) {
185
            if ($template->getLayout() != null) {
186
                return;
187
            }
188
            $template = $template->getTemplate();
189
        }
190
191
        if ($this->getLayout() == null) {
192
            $context->buildViolation('data.template.templateform.view.type.template.layout.validator_message')->addViolation();
193
        }
194
    }
195
}
196