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 |
||
| 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(); |
||
|
|
|||
| 42 | $this->inheritors = new ArrayCollection(); |
||
| 43 | $this->pages = new ArrayCollection(); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * to string. |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | **/ |
||
| 51 | public function __toString() |
||
| 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 [Template] |
||
| 153 | */ |
||
| 154 | public function getInheritors() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get inheritors (all Templates having this object as Template). |
||
| 161 | * |
||
| 162 | * @return [Template] |
||
| 163 | */ |
||
| 164 | View Code Duplication | public function getTemplateInheritors() |
|
| 165 | { |
||
| 166 | $templateInheritors = []; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @Assert\Callback(groups={"victoire"}) |
||
| 178 | * |
||
| 179 | * @param ExecutionContextInterface $context |
||
| 180 | */ |
||
| 181 | public function validate(ExecutionContextInterface $context) |
||
| 195 | } |
||
| 196 |
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..