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 |
||
| 21 | class ExtraConfigurationSubscriber implements EventSubscriberInterface |
||
| 22 | { |
||
| 23 | use TranslationKeyTrait; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * If is true, the extra configuration will be added. |
||
| 27 | * |
||
| 28 | * @var boolean |
||
| 29 | */ |
||
| 30 | protected $extraConfigurationEnabled = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var EntityManager |
||
| 34 | */ |
||
| 35 | protected $entityManager; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var ApplicationConfiguration |
||
| 39 | */ |
||
| 40 | protected $applicationConfiguration; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return array |
||
| 44 | */ |
||
| 45 | 1 | public static function getSubscribedEvents() |
|
| 46 | { |
||
| 47 | return [ |
||
| 48 | 1 | AdminEvents::ADMIN_CREATE => 'adminCreate', |
|
| 49 | ActionEvents::BEFORE_CONFIGURATION => 'beforeActionConfiguration', |
||
| 50 | ]; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * ExtraConfigurationSubscriber constructor. |
||
| 55 | * |
||
| 56 | * @param boolean $extraConfigurationEnabled |
||
| 57 | * @param Registry $doctrine |
||
| 58 | * @param ConfigurationFactory $configurationFactory |
||
| 59 | */ |
||
| 60 | 6 | public function __construct( |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Adding default CRUD if none is defined. |
||
| 73 | * |
||
| 74 | * @param AdminCreateEvent $event |
||
| 75 | */ |
||
| 76 | 2 | public function adminCreate(AdminCreateEvent $event) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Add default linked actions and default menu actions. |
||
| 97 | * |
||
| 98 | * @param BeforeConfigurationEvent $event |
||
| 99 | */ |
||
| 100 | 4 | public function beforeActionConfiguration(BeforeConfigurationEvent $event) |
|
| 101 | { |
||
| 102 | // add configuration only if extra configuration is enabled |
||
| 103 | 4 | if (!$this->extraConfigurationEnabled) { |
|
| 104 | 1 | return; |
|
| 105 | } |
||
| 106 | // Action configuration array |
||
| 107 | 3 | $configuration = $event->getActionConfiguration(); |
|
| 108 | |||
| 109 | // current Admin |
||
| 110 | 3 | $admin = $event->getAdmin(); |
|
| 111 | |||
| 112 | // allowed Actions according to the admin |
||
| 113 | $keys = $admin |
||
| 114 | 3 | ->getConfiguration() |
|
| 115 | 3 | ->getParameter('actions'); |
|
| 116 | 3 | $allowedActions = array_keys($keys); |
|
| 117 | |||
| 118 | // add default menu configuration |
||
| 119 | 3 | $configuration = $this->addDefaultMenuConfiguration( |
|
| 120 | 3 | $admin->getName(), |
|
| 121 | 3 | $event->getActionName(), |
|
| 122 | $configuration, |
||
| 123 | 3 | $allowedActions |
|
| 124 | ); |
||
| 125 | |||
| 126 | // guess field configuration if required |
||
| 127 | 3 | $this->guessFieldConfiguration( |
|
| 128 | $configuration, |
||
| 129 | 3 | $event->getActionName(), |
|
| 130 | 3 | $admin->getConfiguration()->getParameter('entity') |
|
| 131 | ); |
||
| 132 | |||
| 133 | // guess linked actions for list actions |
||
| 134 | 3 | $this->guessLinkedActionsConfiguration( |
|
| 135 | $configuration, |
||
| 136 | $allowedActions, |
||
| 137 | 3 | $event |
|
| 138 | ); |
||
| 139 | |||
| 140 | // reset action configuration |
||
| 141 | 3 | $event->setActionConfiguration($configuration); |
|
| 142 | 3 | } |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Add default menu configuration for an action. |
||
| 146 | * |
||
| 147 | * @param string $adminName |
||
| 148 | * @param string $actionName |
||
| 149 | * @param array $actionConfiguration |
||
| 150 | * @param array $allowedActions |
||
| 151 | * @return array The modified configuration |
||
| 152 | */ |
||
| 153 | 3 | protected function addDefaultMenuConfiguration( |
|
| 190 | |||
| 191 | /** |
||
| 192 | * If no field was provided, try to guess field |
||
| 193 | * |
||
| 194 | * @param array $configuration |
||
| 195 | * @param $actionName |
||
| 196 | * @param $entityClass |
||
| 197 | */ |
||
| 198 | 3 | protected function guessFieldConfiguration(array &$configuration, $actionName, $entityClass) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Add fake link fields to edit and delete action for the list view. |
||
| 233 | * |
||
| 234 | * @param array $configuration |
||
| 235 | * @param array $allowedActions |
||
| 236 | * @param BeforeConfigurationEvent $event |
||
| 237 | */ |
||
| 238 | 3 | protected function guessLinkedActionsConfiguration( |
|
| 293 | } |
||
| 294 |