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 |
||
| 20 | class BlockPresenter extends BasePresenter |
||
| 21 | { |
||
| 22 | |||
| 23 | const REDIRECT_DEFAULT = 'Block:listing'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var integer |
||
| 27 | */ |
||
| 28 | private $blockId = NULL; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Emailer |
||
| 32 | */ |
||
| 33 | private $emailer; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var BlockRepository |
||
| 37 | */ |
||
| 38 | private $blockRepository; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var IBlockFormFactory |
||
| 42 | */ |
||
| 43 | private $blockFormFactory; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param BlockRepository $blockRepository |
||
| 47 | * @param Emailer $emailer |
||
| 48 | */ |
||
| 49 | public function __construct(BlockRepository $blockRepository, Emailer $emailer) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param IBlockFormFactory $factory |
||
| 57 | */ |
||
| 58 | public function injectBlockFormFactory(IBlockFormFactory $factory) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | public function startup() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return void |
||
| 76 | */ |
||
| 77 | public function actionCreate(ArrayHash $block) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param integer $id |
||
| 97 | * @param ArrayHash $block |
||
| 98 | * @return boolean |
||
| 99 | */ |
||
| 100 | public function actionUpdate($id, ArrayHash $block) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param int $id |
||
| 124 | * @return void |
||
| 125 | * @throws \Nette\Application\AbortException |
||
| 126 | */ |
||
| 127 | public function actionDelete($id) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Send mail to tutor |
||
| 143 | * |
||
| 144 | * @param $id |
||
| 145 | * @return void |
||
| 146 | * @throws \Nette\Application\AbortException |
||
| 147 | */ |
||
| 148 | public function actionMail($id) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | public function renderNew() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Prepare data for editing |
||
| 177 | * |
||
| 178 | * @param int $id of Block |
||
| 179 | * @return void |
||
| 180 | */ |
||
| 181 | public function renderEdit($id) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function renderListing() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return BlockForm |
||
| 213 | */ |
||
| 214 | View Code Duplication | protected function createComponentBlockForm(): BlockForm |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @return Emailer |
||
| 244 | */ |
||
| 245 | protected function getEmailer(): Emailer |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param Emailer $emailer |
||
| 252 | * @return self |
||
| 253 | */ |
||
| 254 | protected function setEmailer(Emailer $emailer): self |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return BlockRepository |
||
| 262 | */ |
||
| 263 | private function getBlockRepository(): BlockRepository |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param BlockRepository $blockRepository |
||
| 270 | * @return self |
||
| 271 | */ |
||
| 272 | private function setBlockRepository(BlockRepository $blockRepository): self |
||
| 278 | |||
| 279 | |||
| 280 | |||
| 281 | } |
||
| 282 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: