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 |
||
| 31 | class Content extends AbstractModel implements |
||
|
|
|||
| 32 | AuthorableInterface, |
||
| 33 | ContentInterface, |
||
| 34 | RevisionableInterface, |
||
| 35 | TimestampableInterface |
||
| 36 | { |
||
| 37 | use AuthorableTrait; |
||
| 38 | use RevisionableTrait; |
||
| 39 | use TranslatorAwareTrait; |
||
| 40 | use TimestampableTrait; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Objects are active by default |
||
| 44 | * @var boolean |
||
| 45 | */ |
||
| 46 | private $active = true; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The position is used for ordering lists |
||
| 50 | * @var integer |
||
| 51 | */ |
||
| 52 | private $position = 0; |
||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * @var FactoryInterface |
||
| 57 | */ |
||
| 58 | private $modelFactory; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string[] |
||
| 62 | */ |
||
| 63 | private $requiredAclPermissions = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Dependencies |
||
| 67 | * @param Container $container DI Container. |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | protected function setDependencies(Container $container) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param FactoryInterface $factory The factory used to create models. |
||
| 80 | * @return Content Chainable |
||
| 81 | */ |
||
| 82 | protected function setModelFactory(FactoryInterface $factory) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return FactoryInterface The model factory. |
||
| 90 | */ |
||
| 91 | protected function modelFactory() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param boolean $active The active flag. |
||
| 98 | * @return Content Chainable |
||
| 99 | */ |
||
| 100 | public function setActive($active) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return boolean |
||
| 108 | */ |
||
| 109 | public function getActive() |
||
| 110 | { |
||
| 111 | return $this->active; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param integer $position The position (for ordering purpose). |
||
| 116 | * @throws InvalidArgumentException If the position is not an integer (or numeric integer string). |
||
| 117 | * @return Content Chainable |
||
| 118 | */ |
||
| 119 | public function setPosition($position) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return integer |
||
| 139 | */ |
||
| 140 | public function getPosition() |
||
| 141 | { |
||
| 142 | return $this->position; |
||
| 143 | } |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * @throws InvalidArgumentException If the ACL permissions are invalid. |
||
| 148 | * @param string|string[] $permissions The required ACL permissions. |
||
| 149 | * @return Content Chainable |
||
| 150 | */ |
||
| 151 | public function setRequiredAclPermissions($permissions) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return string[] |
||
| 172 | */ |
||
| 173 | public function getRequiredAclPermissions() |
||
| 174 | { |
||
| 175 | return $this->requiredAclPermissions; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * StorableTrait > preSave(): Called automatically before saving the object to source. |
||
| 180 | * For content object, set the `created` and `lastModified` properties automatically |
||
| 181 | * @return boolean |
||
| 182 | */ |
||
| 183 | protected function preSave() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * StorableTrait > preUpdate(): Called automatically before updating the object to source. |
||
| 196 | * For content object, set the `lastModified` property automatically. |
||
| 197 | * |
||
| 198 | * @param array $properties The properties (ident) set for update. |
||
| 199 | * @return boolean |
||
| 200 | */ |
||
| 201 | protected function preUpdate(array $properties = null) |
||
| 215 | } |
||
| 216 |