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:
Complex classes like TranslationAdminListConfigurator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TranslationAdminListConfigurator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class TranslationAdminListConfigurator extends AbstractDoctrineDBALAdminListConfigurator implements ChangeableLimitInterface |
||
| 20 | { |
||
| 21 | use ChangeableLimitTrait; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $locales; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $locale; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var Field[] |
||
| 35 | */ |
||
| 36 | private $exportFields = []; |
||
|
|
|||
| 37 | |||
| 38 | /** |
||
| 39 | * @param Connection $connection |
||
| 40 | * @param array $locales |
||
| 41 | */ |
||
| 42 | public function __construct(Connection $connection, array $locales) |
||
| 43 | { |
||
| 44 | parent::__construct($connection); |
||
| 45 | $this->locales = $locales; |
||
| 46 | $this->setCountField('CONCAT(b.translation_id)'); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Configure filters |
||
| 51 | */ |
||
| 52 | public function buildFilters() |
||
| 53 | { |
||
| 54 | $this->addFilter('status', new StringFilterType('status'), 'kuma_translator.adminlist.filter.status'); |
||
| 55 | $this->addFilter('domain', new StringFilterType('domain'), 'kuma_translator.adminlist.filter.domain'); |
||
| 56 | $this->addFilter('keyword', new StringFilterType('keyword'), 'kuma_translator.adminlist.filter.keyword'); |
||
| 57 | $this->addFilter('text', new StringFilterType('text'), 'kuma_translator.adminlist.filter.text'); |
||
| 58 | $this->addFilter('locale', new EnumerationFilterType('locale'), 'kuma_translator.adminlist.filter.locale', array_combine( |
||
| 59 | $this->locales, |
||
| 60 | $this->locales |
||
| 61 | )); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Configure the visible columns |
||
| 66 | */ |
||
| 67 | public function buildFields() |
||
| 68 | { |
||
| 69 | $this->addField('domain', 'kuma_translator.adminlist.header.domain', true); |
||
| 70 | $this->addField('keyword', 'kuma_translator.adminlist.header.keyword', true); |
||
| 71 | $this->addField('status', 'kuma_translator.adminlist.header.status', true); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getExportFields() |
||
| 75 | { |
||
| 76 | if (empty($this->exportFields)) { |
||
| 77 | $this->addExportField('domain', 'kuma_translator.adminlist.header.domain'); |
||
| 78 | $this->addExportField('keyword', 'kuma_translator.adminlist.header.keyword'); |
||
| 79 | |||
| 80 | $this->locales = array_unique($this->locales); |
||
| 81 | // Field building hack... |
||
| 82 | foreach ($this->locales as $locale) { |
||
| 83 | $this->addExportField($locale, strtoupper($locale)); |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->addExportField('status', 'kuma_translator.adminlist.header.status'); |
||
| 87 | } |
||
| 88 | |||
| 89 | return $this->exportFields; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function addExportField($name, $header, $template = null, FieldAlias $alias = null) |
||
| 93 | { |
||
| 94 | $this->exportFields[] = new Field($name, $header); |
||
| 95 | |||
| 96 | return $this; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | public function canAdd() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param object|array $item |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function canEdit($item) |
||
| 113 | { |
||
| 114 | return false; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param object|array $item |
||
| 119 | * |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | public function canEditInline($item) |
||
| 123 | { |
||
| 124 | return true; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public function canExport() |
||
| 131 | { |
||
| 132 | return true; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Override path convention (because settings is a virtual admin subtree) |
||
| 137 | * |
||
| 138 | * @param string $suffix |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | View Code Duplication | public function getPathByConvention($suffix = null) |
|
| 143 | { |
||
| 144 | if (empty($suffix)) { |
||
| 145 | return sprintf('%s_settings_%ss', $this->getBundleName(), strtolower($this->getEntityName())); |
||
| 146 | } |
||
| 147 | |||
| 148 | return sprintf('%s_settings_%ss_%s', $this->getBundleName(), strtolower($this->getEntityName()), $suffix); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritdoc} |
||
| 153 | */ |
||
| 154 | public function getAdminType($item) |
||
| 155 | { |
||
| 156 | return null; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function getBundleName() |
||
| 163 | |||
| 164 | public function getEntityName() |
||
| 168 | |||
| 169 | public function getControllerPath() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return QueryBuilder|null |
||
| 176 | */ |
||
| 177 | public function getQueryBuilder() |
||
| 178 | { |
||
| 179 | if (\is_null($this->queryBuilder)) { |
||
| 180 | $this->queryBuilder = new QueryBuilder($this->connection); |
||
| 181 | $this->queryBuilder |
||
| 182 | ->select('DISTINCT b.translation_id AS id, b.keyword, b.domain, b.status') |
||
| 183 | ->from('kuma_translation', 'b') |
||
| 309 | } |
||
| 310 |