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 bool |
||
| 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 bool $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 | 4 | public function adminCreate(AdminCreateEvent $event) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Add default linked actions and default menu actions. |
||
| 98 | * |
||
| 99 | * @param BeforeConfigurationEvent $event |
||
| 100 | */ |
||
| 101 | 2 | public function beforeActionConfiguration(BeforeConfigurationEvent $event) |
|
| 102 | { |
||
| 103 | // add configuration only if extra configuration is enabled |
||
| 104 | 2 | if (!$this->extraConfigurationEnabled) { |
|
| 105 | return; |
||
| 106 | } |
||
| 107 | // action configuration array |
||
| 108 | 2 | $configuration = $event->getActionConfiguration(); |
|
| 109 | // current action admin |
||
| 110 | 2 | $admin = $event->getAdmin(); |
|
| 111 | // allowed actions according to the admin |
||
| 112 | $keys = $admin |
||
| 113 | 2 | ->getConfiguration() |
|
| 114 | 2 | ->getParameter('actions'); |
|
| 115 | 2 | $allowedActions = array_keys($keys); |
|
| 116 | |||
| 117 | // add default menu configuration |
||
| 118 | 2 | $configuration = $this->addDefaultMenuConfiguration( |
|
| 119 | 2 | $admin->getName(), |
|
| 120 | 2 | $event->getActionName(), |
|
| 121 | $configuration, |
||
| 122 | 2 | $allowedActions |
|
| 123 | ); |
||
| 124 | |||
| 125 | // guess field configuration if required |
||
| 126 | 2 | $this->guessFieldConfiguration( |
|
| 127 | $configuration, |
||
| 128 | 2 | $event->getActionName(), |
|
| 129 | 2 | $admin->getConfiguration()->getParameter('entity') |
|
| 130 | ); |
||
| 131 | |||
| 132 | // guess linked actions for list actions |
||
| 133 | 2 | $this->guessLinkedActionsConfiguration( |
|
| 134 | $configuration, |
||
| 135 | $allowedActions, |
||
| 136 | 2 | $event |
|
| 137 | ); |
||
| 138 | |||
| 139 | // reset action configuration |
||
| 140 | 2 | $event->setActionConfiguration($configuration); |
|
| 141 | 2 | } |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Add default menu configuration for an action. |
||
| 145 | * |
||
| 146 | * @param string $adminName |
||
| 147 | * @param string $actionName |
||
| 148 | * @param array $actionConfiguration |
||
| 149 | * @param array $allowedActions |
||
| 150 | * @return array The modified configuration |
||
| 151 | */ |
||
| 152 | 2 | protected function addDefaultMenuConfiguration( |
|
| 189 | |||
| 190 | /** |
||
| 191 | * If no field was provided, try to guess field |
||
| 192 | * |
||
| 193 | * @param array $configuration |
||
| 194 | * @param $actionName |
||
| 195 | * @param $entityClass |
||
| 196 | */ |
||
| 197 | 2 | protected function guessFieldConfiguration(array &$configuration, $actionName, $entityClass) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Add fake link fields to edit and delete action for the list view. |
||
| 232 | * |
||
| 233 | * @param array $configuration |
||
| 234 | * @param array $allowedActions |
||
| 235 | * @param BeforeConfigurationEvent $event |
||
| 236 | */ |
||
| 237 | 2 | protected function guessLinkedActionsConfiguration( |
|
| 292 | } |
||
| 293 |