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 |
||
| 25 | class Admin implements AdminInterface |
||
| 26 | { |
||
| 27 | use AdminTrait; |
||
| 28 | |||
| 29 | const LOAD_STRATEGY_UNIQUE = 'strategy_unique'; |
||
| 30 | const LOAD_STRATEGY_MULTIPLE = 'strategy_multiple'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Entities collection. |
||
| 34 | * |
||
| 35 | * @var ArrayCollection|ArrayIterator |
||
| 36 | */ |
||
| 37 | protected $entities; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var MessageHandlerInterface |
||
| 41 | */ |
||
| 42 | protected $messageHandler; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var EntityManagerInterface |
||
| 46 | */ |
||
| 47 | protected $entityManager; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var DataProviderInterface |
||
| 51 | */ |
||
| 52 | protected $dataProvider; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Admin configuration object |
||
| 56 | * |
||
| 57 | * @var AdminConfiguration |
||
| 58 | */ |
||
| 59 | protected $configuration; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Admin configured actions |
||
| 63 | * |
||
| 64 | * @var ActionInterface[] |
||
| 65 | */ |
||
| 66 | protected $actions = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Admin current action. It will be set after calling the handleRequest() |
||
| 70 | * |
||
| 71 | * @var ActionInterface |
||
| 72 | */ |
||
| 73 | protected $currentAction; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Admin name |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $name; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Admin constructor. |
||
| 84 | * |
||
| 85 | * @param string $name |
||
| 86 | * @param DataProviderInterface $dataProvider |
||
| 87 | * @param AdminConfiguration $configuration |
||
| 88 | * @param MessageHandlerInterface $messageHandler |
||
| 89 | */ |
||
| 90 | public function __construct( |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Load entities and set current action according to request |
||
| 105 | * |
||
| 106 | * @param Request $request |
||
| 107 | * @param null $user |
||
| 108 | * @return mixed|void |
||
| 109 | * @throws AdminException |
||
| 110 | */ |
||
| 111 | public function handleRequest(Request $request, $user = null) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Check if user is allowed to be here |
||
| 142 | * |
||
| 143 | * @param UserInterface|string $user |
||
| 144 | */ |
||
| 145 | public function checkPermissions($user) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
||
| 166 | * |
||
| 167 | * @return bool true if the entity was saved without errors |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function save() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Remove an entity with data provider |
||
| 196 | * |
||
| 197 | * @return bool true if the entity was saved without errors |
||
| 198 | */ |
||
| 199 | View Code Duplication | public function remove() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 226 | * |
||
| 227 | * @param $actionName |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | * |
||
| 231 | * @throws Exception |
||
| 232 | */ |
||
| 233 | public function generateRouteName($actionName) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Load entities manually according to criteria. |
||
| 250 | * |
||
| 251 | * @param array $criteria |
||
| 252 | * @param array $orderBy |
||
| 253 | * @param int $limit |
||
| 254 | * @param int $offset |
||
| 255 | */ |
||
| 256 | public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Return loaded entities |
||
| 287 | * |
||
| 288 | * @return mixed |
||
| 289 | */ |
||
| 290 | public function getEntities() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 297 | * |
||
| 298 | * @return mixed |
||
| 299 | * |
||
| 300 | * @throws Exception |
||
| 301 | */ |
||
| 302 | public function getUniqueEntity() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Return admin name |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getName() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Return true if current action is granted for user. |
||
| 322 | * |
||
| 323 | * @param string $actionName Le plus grand de tous les héros |
||
| 324 | * @param array $roles |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function isActionGranted($actionName, array $roles) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return ActionInterface[] |
||
| 354 | */ |
||
| 355 | public function getActions() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | public function getActionNames() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param $name |
||
| 370 | * @return ActionInterface |
||
| 371 | * @throws Exception |
||
| 372 | */ |
||
| 373 | public function getAction($name) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Return if an action with specified name exists form this admin. |
||
| 384 | * |
||
| 385 | * @param $name |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function hasAction($name) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param ActionInterface $action |
||
| 395 | * @return void |
||
| 396 | */ |
||
| 397 | public function addAction(ActionInterface $action) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return ActionInterface |
||
| 404 | */ |
||
| 405 | public function getCurrentAction() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Return admin configuration object |
||
| 412 | * |
||
| 413 | * @return AdminConfiguration |
||
| 414 | */ |
||
| 415 | public function getConfiguration() |
||
| 419 | } |
||
| 420 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: