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 |
||
| 11 | class BlockForm extends BaseForm |
||
| 12 | { |
||
| 13 | |||
| 14 | const TEMPLATE_NAME = 'BlockForm'; |
||
| 15 | |||
| 16 | const MESSAGE_REQUIRED = 'Hodnota musí být vyplněna!'; |
||
| 17 | const MESSAGE_MAX_LENGTH = '%label nesmí mít více jak %d znaků!'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var Closure |
||
| 21 | */ |
||
| 22 | public $onBlockSave; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Closure |
||
| 26 | */ |
||
| 27 | public $onBlockReset; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var BlockRepository |
||
| 31 | */ |
||
| 32 | protected $blockRepository; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var CategoryRepository |
||
| 36 | */ |
||
| 37 | protected $categoryRepository; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $days = [ |
||
| 43 | 'pátek' => 'pátek', |
||
| 44 | 'sobota' => 'sobota', |
||
| 45 | 'neděle' => 'neděle', |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $hours = [ |
||
| 52 | 0 => "00","01","02","03","04","05","06","07","08","09", |
||
| 53 | "10","11","12","13","14","15","16","17","18","19", |
||
| 54 | "20","21","22","23" |
||
| 55 | ]; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $minutes = [ |
||
| 61 | 00 => "00", |
||
| 62 | 05 => "05", |
||
| 63 | 10 => "10", |
||
| 64 | 15 => "15", |
||
| 65 | 20 => "20", |
||
| 66 | 25 => "25", |
||
| 67 | 30 => "30", |
||
| 68 | 35 => "35", |
||
| 69 | 40 => "40", |
||
| 70 | 45 => "45", |
||
| 71 | 50 => "50", |
||
| 72 | 55 => "55", |
||
| 73 | ]; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param BlockRepository $blockRepository |
||
| 77 | * @param CategoryRepository $categoryRepository |
||
| 78 | */ |
||
| 79 | public function __construct( |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public function render() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param array $defaults |
||
| 100 | * @return self |
||
| 101 | */ |
||
| 102 | public function setDefaults(BlockEntity $defaults): self |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return Form |
||
| 113 | */ |
||
| 114 | public function createComponentBlockForm(): Form |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param SubmitButton $button |
||
| 166 | * @return void |
||
| 167 | */ |
||
| 168 | public function processForm(SubmitButton $button) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param SubmitButton $button |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | public function processReset(SubmitButton $button) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | View Code Duplication | protected function buildCategorySelect(): array |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param BlockEntity $defaults |
||
| 204 | * @return BlockEntity |
||
| 205 | */ |
||
| 206 | protected function guardDefaults(BlockEntity $defaults): BlockEntity |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return BlockRepository |
||
| 218 | */ |
||
| 219 | public function getBlockRepository(): BlockRepository |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param BlockRepository $blockRepository |
||
| 226 | * |
||
| 227 | * @return self |
||
| 228 | */ |
||
| 229 | public function setBlockRepository(BlockRepository $repository): self |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return CategoryRepository |
||
| 238 | */ |
||
| 239 | public function getCategoryRepository(): CategoryRepository |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param CategoryRepository $categoryRepository |
||
| 246 | * |
||
| 247 | * @return self |
||
| 248 | */ |
||
| 249 | public function setCategoryRepository(CategoryRepository $repository): self |
||
| 255 | } |
||
| 256 |
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: