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 |
||
| 33 | class AdminFactory |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | private $isInit = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var EventDispatcherInterface |
||
| 42 | */ |
||
| 43 | private $eventDispatcher; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var ConfigurationFactory |
||
| 47 | */ |
||
| 48 | private $configurationFactory; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var AdminConfiguration[] |
||
| 52 | */ |
||
| 53 | private $adminConfigurations; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var ActionFactory |
||
| 57 | */ |
||
| 58 | private $actionFactory; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var MessageHandlerInterface |
||
| 62 | */ |
||
| 63 | private $messageHandler; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var Registry |
||
| 67 | */ |
||
| 68 | private $adminRegistry; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var DataProviderFactory |
||
| 72 | */ |
||
| 73 | private $dataProviderFactory; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var RequestHandler |
||
| 77 | */ |
||
| 78 | private $requestHandler; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var AuthorizationCheckerInterface |
||
| 82 | */ |
||
| 83 | private $authorizationChecker; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var TokenStorageInterface |
||
| 87 | */ |
||
| 88 | private $tokenStorage; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var ActionRegistry |
||
| 92 | */ |
||
| 93 | private $actionRegistry; |
||
| 94 | /** |
||
| 95 | * @var ViewFactory |
||
| 96 | */ |
||
| 97 | private $viewFactory; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * AdminFactory constructor. |
||
| 101 | * |
||
| 102 | * @param array $adminConfigurations |
||
| 103 | * @param EventDispatcherInterface $eventDispatcher |
||
| 104 | * @param MessageHandlerInterface $messageHandler |
||
| 105 | * @param Registry $adminRegistry |
||
| 106 | * @param ActionRegistry $actionRegistry |
||
| 107 | * @param ActionFactory $actionFactory |
||
| 108 | * @param ConfigurationFactory $configurationFactory |
||
| 109 | * @param DataProviderFactory $dataProviderFactory |
||
| 110 | * @param ViewFactory $viewFactory |
||
| 111 | * @param RequestHandler $requestHandler |
||
| 112 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
| 113 | * @param TokenStorageInterface $tokenStorage |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function __construct( |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Create admins from configuration and load them into the pool. Dispatch ADMIN_CREATE event. |
||
| 145 | */ |
||
| 146 | public function init() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Create an Admin from configuration values. It will be added to AdminFactory admin's list. |
||
| 190 | * |
||
| 191 | * @param string $name |
||
| 192 | * @param array $configuration |
||
| 193 | * |
||
| 194 | * @return AdminInterface |
||
| 195 | * |
||
| 196 | * @throws LogicException |
||
| 197 | */ |
||
| 198 | public function create($name, array $configuration) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Inject an Admin into an AdminAware controller. Add this Admin to Twig global parameters. |
||
| 249 | * |
||
| 250 | * @param mixed $controller |
||
| 251 | * @param Request $request |
||
| 252 | */ |
||
| 253 | public function injectAdmin($controller, Request $request) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Return true if the AdminFactory is initialized. |
||
| 277 | * |
||
| 278 | * @return boolean |
||
| 279 | */ |
||
| 280 | public function isInit() |
||
| 284 | } |
||
| 285 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.