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 |
||
| 23 | abstract class AbstractTemplate implements |
||
| 24 | LoggerAwareInterface, |
||
| 25 | TemplateInterface, |
||
| 26 | ViewableInterface |
||
| 27 | { |
||
| 28 | |||
| 29 | use ViewableTrait; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var LoggerInterface $logger |
||
| 33 | */ |
||
| 34 | private $logger; |
||
| 35 | |||
| 36 | public function __construct(array $data = null) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param SlimApp $app |
||
| 47 | * @return App Chainable |
||
| 48 | */ |
||
| 49 | public function set_app(SlimApp $app) |
||
| 54 | |||
| 55 | public function app($app) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * > LoggerAwareInterface > setLogger() |
||
| 62 | * |
||
| 63 | * Fulfills the PSR-1 style LoggerAwareInterface |
||
| 64 | * |
||
| 65 | * @param LoggerInterface $logger |
||
| 66 | * @return AbstractEngine Chainable |
||
| 67 | */ |
||
| 68 | public function setLogger(LoggerInterface $logger) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param LoggerInterface $logger |
||
| 75 | * @return AbstractEngine Chainable |
||
| 76 | */ |
||
| 77 | public function set_logger(LoggerInterface $logger = null) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @erturn LoggerInterface |
||
| 85 | */ |
||
| 86 | public function logger() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The default Template View is a simple GenericView. |
||
| 93 | * |
||
| 94 | * @return \Charcoal\View\ViewInterface |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function create_view(array $data = null) |
|
| 106 | } |
||
| 107 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: