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 | /** |
||
| 30 | * Do not load entities on handleRequest (for create method for example) |
||
| 31 | */ |
||
| 32 | const LOAD_STRATEGY_NONE = 'strategy_none'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Load one entity on handleRequest (edit method for example) |
||
| 36 | */ |
||
| 37 | const LOAD_STRATEGY_UNIQUE = 'strategy_unique'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Load multiple entities on handleRequest (list method for example) |
||
| 41 | */ |
||
| 42 | const LOAD_STRATEGY_MULTIPLE = 'strategy_multiple'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Entities collection. |
||
| 46 | * |
||
| 47 | * @var ArrayCollection|ArrayIterator |
||
| 48 | */ |
||
| 49 | protected $entities; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var MessageHandlerInterface |
||
| 53 | */ |
||
| 54 | protected $messageHandler; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var EntityManagerInterface |
||
| 58 | */ |
||
| 59 | protected $entityManager; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var DataProviderInterface |
||
| 63 | */ |
||
| 64 | protected $dataProvider; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Admin configuration object |
||
| 68 | * |
||
| 69 | * @var AdminConfiguration |
||
| 70 | */ |
||
| 71 | protected $configuration; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Admin configured actions |
||
| 75 | * |
||
| 76 | * @var ActionInterface[] |
||
| 77 | */ |
||
| 78 | protected $actions = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Admin current action. It will be set after calling the handleRequest() |
||
| 82 | * |
||
| 83 | * @var ActionInterface |
||
| 84 | */ |
||
| 85 | protected $currentAction; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Admin name |
||
| 89 | * |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $name; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Admin constructor. |
||
| 96 | * |
||
| 97 | * @param string $name |
||
| 98 | * @param DataProviderInterface $dataProvider |
||
| 99 | * @param AdminConfiguration $configuration |
||
| 100 | * @param MessageHandlerInterface $messageHandler |
||
| 101 | */ |
||
| 102 | public function __construct( |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Load entities and set current action according to request |
||
| 117 | * |
||
| 118 | * @param Request $request |
||
| 119 | * @param null $user |
||
| 120 | * @return void |
||
| 121 | * @throws AdminException |
||
| 122 | */ |
||
| 123 | public function handleRequest(Request $request, $user = null) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Check if user is allowed to be here |
||
| 159 | * |
||
| 160 | * @param UserInterface|string $user |
||
| 161 | */ |
||
| 162 | public function checkPermissions($user) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Create and return a new entity. |
||
| 183 | * |
||
| 184 | * @return object |
||
| 185 | */ |
||
| 186 | public function create() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Save entity via admin manager. Error are catch, logged and a flash message is added to session |
||
| 203 | * |
||
| 204 | * @return bool true if the entity was saved without errors |
||
| 205 | */ |
||
| 206 | View Code Duplication | public function save() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Remove an entity with data provider |
||
| 233 | * |
||
| 234 | * @return bool true if the entity was saved without errors |
||
| 235 | */ |
||
| 236 | View Code Duplication | public function remove() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Generate a route for admin and action name (like lag.admin.my_admin) |
||
| 263 | * |
||
| 264 | * @param $actionName |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | * |
||
| 268 | * @throws Exception |
||
| 269 | */ |
||
| 270 | public function generateRouteName($actionName) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Load entities manually according to criteria. |
||
| 287 | * |
||
| 288 | * @param array $criteria |
||
| 289 | * @param array $orderBy |
||
| 290 | * @param int $limit |
||
| 291 | * @param int $offset |
||
| 292 | */ |
||
| 293 | public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Return loaded entities |
||
| 324 | * |
||
| 325 | * @return mixed |
||
| 326 | */ |
||
| 327 | public function getEntities() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Return entity for current admin. If entity does not exist, it throws an exception. |
||
| 334 | * |
||
| 335 | * @return mixed |
||
| 336 | * |
||
| 337 | * @throws Exception |
||
| 338 | */ |
||
| 339 | public function getUniqueEntity() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Return admin name |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function getName() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Return true if current action is granted for user. |
||
| 362 | * |
||
| 363 | * @param string $actionName Le plus grand de tous les héros |
||
| 364 | * @param array $roles |
||
| 365 | * |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | public function isActionGranted($actionName, array $roles) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return ActionInterface[] |
||
| 394 | */ |
||
| 395 | public function getActions() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | public function getActionNames() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param $name |
||
| 410 | * @return ActionInterface |
||
| 411 | * @throws Exception |
||
| 412 | */ |
||
| 413 | public function getAction($name) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Return if an action with specified name exists form this admin. |
||
| 424 | * |
||
| 425 | * @param $name |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | public function hasAction($name) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param ActionInterface $action |
||
| 435 | * @return void |
||
| 436 | */ |
||
| 437 | public function addAction(ActionInterface $action) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @return ActionInterface |
||
| 444 | */ |
||
| 445 | public function getCurrentAction() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Return admin configuration object |
||
| 452 | * |
||
| 453 | * @return AdminConfiguration |
||
| 454 | */ |
||
| 455 | public function getConfiguration() |
||
| 459 | } |
||
| 460 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: