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 | protected $renderedBody; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param ServiceLocatorInterface $serviceManager |
||
| 43 | * @param array $options |
||
| 44 | */ |
||
| 45 | public function __construct(ServiceLocatorInterface $serviceManager, array $options = array()) |
||
| 46 | { |
||
| 47 | // @TODO make this multipart |
||
| 48 | parent::__construct($options); |
||
| 49 | $this->serviceManager = $serviceManager; |
||
| 50 | $this->getHeaders()->addHeader(Header\ContentType::fromString('Content-Type: text/html; charset=UTF-8')); |
||
| 51 | $this->setEncoding('UTF-8'); |
||
| 52 | $this->variables = new ViewVariables(); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Property overloading: set variable value |
||
| 57 | * |
||
| 58 | * @param string $name |
||
| 59 | * @param mixed $value |
||
| 60 | * @return void |
||
| 61 | */ |
||
| 62 | public function __set($name, $value) |
||
| 63 | { |
||
| 64 | $this->setVariable($name, $value); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Property overloading: get variable value |
||
| 69 | * |
||
| 70 | * @param string $name |
||
| 71 | * @return mixed |
||
| 72 | */ |
||
| 73 | public function __get($name) |
||
| 74 | { |
||
| 75 | if (!$this->__isset($name)) { |
||
| 76 | return null; |
||
| 77 | } |
||
| 78 | |||
| 79 | $variables = $this->getVariables(); |
||
| 80 | return $variables[$name]; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Property overloading: do we have the requested variable value? |
||
| 85 | * |
||
| 86 | * @param string $name |
||
| 87 | * @return bool |
||
| 88 | */ |
||
| 89 | public function __isset($name) |
||
|
|
|||
| 90 | { |
||
| 91 | $variables = $this->getVariables(); |
||
| 92 | return isset($variables[$name]); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Property overloading: unset the requested variable |
||
| 97 | * |
||
| 98 | * @param string $name |
||
| 99 | * @return void |
||
| 100 | */ |
||
| 101 | public function __unset($name) |
||
| 102 | { |
||
| 103 | if ($this->__isset($name)) { |
||
| 104 | unset($this->variables[$name]); |
||
| 105 | } |
||
| 106 | } |
||
| 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) |
||
| 149 | { |
||
| 150 | View Code Duplication | if (!is_array($variables) && !$variables instanceof \Traversable) { |
|
| 151 | throw new \InvalidArgumentException( |
||
| 152 | sprintf( |
||
| 153 | '%s: expects an array, or Traversable argument; received "%s"', |
||
| 154 | __METHOD__, |
||
| 155 | (is_object($variables) ? get_class($variables) : gettype($variables)) |
||
| 156 | ) |
||
| 157 | ); |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($overwrite) { |
||
| 161 | if (is_object($variables) && !$variables instanceof \ArrayAccess) { |
||
| 162 | $variables = ArrayUtils::iteratorToArray($variables); |
||
| 163 | } |
||
| 164 | |||
| 165 | $this->variables = $variables; |
||
| 166 | return $this; |
||
| 167 | } |
||
| 168 | |||
| 169 | foreach ($variables as $key => $value) { |
||
| 170 | $this->setVariable($key, $value); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $this; |
||
| 174 | } |
||
| 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 | * @param $template |
||
| 202 | * |
||
| 203 | * @return self |
||
| 204 | */ |
||
| 205 | public function setTemplate($template) |
||
| 210 | |||
| 211 | public function getTemplate() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @example /module/Jobs/src/Jobs/Listener/PortalListener.php |
||
| 218 | * @return string |
||
| 219 | * @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 |
||
| 220 | */ |
||
| 221 | public function renderBodyText($force = true) |
||
| 257 | |||
| 258 | public function getBodyText() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param MailService $mailService |
||
| 266 | * @return \Core\Mail\HTMLTemplateMessage |
||
| 267 | */ |
||
| 268 | public static function factory(MailService $mailService) |
||
| 272 | } |
||
| 273 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.