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 |
||
| 26 | class HTMLTemplateMessage extends TranslatorAwareMessage |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var ServiceLocatorInterface |
||
| 30 | */ |
||
| 31 | protected $serviceManager; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * View variables |
||
| 35 | * @var array|ArrayAccess&Traversable |
||
| 36 | */ |
||
| 37 | protected $variables = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param ServiceLocatorInterface $serviceManager |
||
| 41 | * @param array $options |
||
| 42 | */ |
||
| 43 | public function __construct(ServiceLocatorInterface $serviceManager, array $options = array()) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Property overloading: set variable value |
||
| 55 | * |
||
| 56 | * @param string $name |
||
| 57 | * @param mixed $value |
||
| 58 | * @return void |
||
| 59 | */ |
||
| 60 | public function __set($name, $value) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Property overloading: get variable value |
||
| 67 | * |
||
| 68 | * @param string $name |
||
| 69 | * @return mixed |
||
| 70 | */ |
||
| 71 | public function __get($name) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Property overloading: do we have the requested variable value? |
||
| 83 | * |
||
| 84 | * @param string $name |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | public function __isset($name) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Property overloading: unset the requested variable |
||
| 95 | * |
||
| 96 | * @param string $name |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | public function __unset($name) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get a single view variable |
||
| 110 | * |
||
| 111 | * @param string $name |
||
| 112 | * @param mixed|null $default (optional) default value if the variable is not present. |
||
| 113 | * @return mixed |
||
| 114 | */ |
||
| 115 | public function getVariable($name, $default = null) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Set view variable |
||
| 127 | * |
||
| 128 | * @param string $name |
||
| 129 | * @param mixed $value |
||
| 130 | * @return ViewModel |
||
| 131 | */ |
||
| 132 | public function setVariable($name, $value) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Set view variables en masse |
||
| 140 | * |
||
| 141 | * Can be an array or a Traversable + ArrayAccess object. |
||
| 142 | * |
||
| 143 | * @param array|ArrayAccess|Traversable $variables |
||
| 144 | * @param bool $overwrite Whether or not to overwrite the internal container with $variables |
||
| 145 | * @throws \InvalidArgumentException |
||
| 146 | * @return self |
||
| 147 | */ |
||
| 148 | public function setVariables($variables, $overwrite = false) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get view variables |
||
| 178 | * |
||
| 179 | * @return array|ArrayAccess|Traversable |
||
| 180 | */ |
||
| 181 | public function getVariables() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Clear all variables |
||
| 188 | * |
||
| 189 | * Resets the internal variable container to an empty container. |
||
| 190 | * |
||
| 191 | * @return ViewModel |
||
| 192 | */ |
||
| 193 | public function clearVariables() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * |
||
| 201 | * |
||
| 202 | * @param $template |
||
| 203 | * |
||
| 204 | * @return self |
||
| 205 | */ |
||
| 206 | public function setTemplate($template) |
||
| 211 | |||
| 212 | public function getTemplate() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @example /module/Jobs/src/Jobs/Listener/PortalListener.php |
||
| 219 | * @return string |
||
| 220 | * @throws \InvalidArgumentException the mail body must completely be provided by the template, any other attempt is a misconception that may leave the coder in an quagmire |
||
| 221 | */ |
||
| 222 | public function getBodyText() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param MailService $mailService |
||
| 258 | * @return \Core\Mail\HTMLTemplateMessage |
||
| 259 | */ |
||
| 260 | public static function factory(MailService $mailService) |
||
| 264 | } |
||
| 265 |
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..