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 |
||
| 16 | class ExtraConfigurationSubscriber implements EventSubscriberInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var bool |
||
| 20 | */ |
||
| 21 | protected $enableExtraConfiguration; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var EntityManager |
||
| 25 | */ |
||
| 26 | protected $entityManager; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var ApplicationConfiguration |
||
| 30 | */ |
||
| 31 | protected $applicationConfiguration; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @return array |
||
| 35 | */ |
||
| 36 | 1 | public static function getSubscribedEvents() |
|
| 43 | |||
| 44 | /** |
||
| 45 | * ExtraConfigurationSubscriber constructor |
||
| 46 | * |
||
| 47 | * @param bool $enableExtraConfiguration |
||
| 48 | * @param Registry $doctrine |
||
| 49 | * @param ApplicationConfiguration $applicationConfiguration |
||
| 50 | */ |
||
| 51 | 1 | public function __construct( |
|
| 52 | $enableExtraConfiguration = true, |
||
| 53 | Registry $doctrine, |
||
| 54 | ApplicationConfiguration $applicationConfiguration |
||
| 55 | ) { |
||
| 56 | 1 | $this->enableExtraConfiguration = $enableExtraConfiguration; |
|
| 57 | // entity manager can be closed. Scrutinizer recommands to inject registry instead |
||
| 58 | 1 | $this->entityManager = $doctrine->getManager(); |
|
| 59 | 1 | $this->applicationConfiguration = $applicationConfiguration; |
|
| 60 | 1 | } |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Adding default CRUD if none is defined |
||
| 64 | * |
||
| 65 | * @param AdminEvent $event |
||
| 66 | */ |
||
| 67 | 1 | public function adminCreate(AdminEvent $event) |
|
| 68 | { |
||
| 69 | 1 | if (!$this->enableExtraConfiguration) { |
|
| 70 | 1 | return; |
|
| 71 | } |
||
| 72 | 1 | $configuration = $event->getConfiguration(); |
|
| 73 | |||
| 74 | // if no actions are defined, we set default CRUD action |
||
| 75 | 1 | if (!array_key_exists('actions', $configuration) || !count($configuration['actions'])) { |
|
| 76 | 1 | $configuration['actions'] = [ |
|
| 77 | 1 | 'create' => [], |
|
| 78 | 1 | 'list' => [], |
|
| 79 | 1 | 'edit' => [], |
|
| 80 | 1 | 'delete' => [], |
|
| 81 | 1 | 'batch' => [] |
|
| 82 | 1 | ]; |
|
| 83 | 1 | } else { |
|
| 84 | 1 | $actions = $configuration['actions']; |
|
| 85 | |||
| 86 | 1 | foreach ($actions as $name => $action) { |
|
| 87 | 1 | if (!array_key_exists('batch', $action) || $action['batch'] !== false) { |
|
| 88 | 1 | if ($name == 'list') { |
|
| 89 | 1 | $configuration['actions']['batch'] = []; |
|
| 90 | 1 | } |
|
| 91 | 1 | } |
|
| 92 | 1 | } |
|
| 93 | } |
||
| 94 | 1 | $event->setConfiguration($configuration); |
|
| 95 | 1 | } |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Add default linked actions and default menu actions |
||
| 99 | * |
||
| 100 | * @param AdminEvent $event |
||
| 101 | * @throws MappingException |
||
| 102 | */ |
||
| 103 | public function actionCreate(AdminEvent $event) |
||
| 204 | } |
||
| 205 |