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 | public function __construct(Connection $connection, array $locales) |
||
39 | { |
||
40 | parent::__construct($connection); |
||
41 | $this->locales = $locales; |
||
42 | $this->setCountField('CONCAT(b.translation_id)'); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Configure filters |
||
47 | */ |
||
48 | public function buildFilters() |
||
49 | { |
||
50 | $this->addFilter('status', new StringFilterType('status'), 'kuma_translator.adminlist.filter.status'); |
||
51 | $this->addFilter('domain', new StringFilterType('domain'), 'kuma_translator.adminlist.filter.domain'); |
||
52 | $this->addFilter('keyword', new StringFilterType('keyword'), 'kuma_translator.adminlist.filter.keyword'); |
||
53 | $this->addFilter('text', new StringFilterType('text'), 'kuma_translator.adminlist.filter.text'); |
||
54 | $this->addFilter('locale', new EnumerationFilterType('locale'), 'kuma_translator.adminlist.filter.locale', array_combine( |
||
55 | $this->locales, |
||
56 | $this->locales |
||
57 | )); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Configure the visible columns |
||
62 | */ |
||
63 | public function buildFields() |
||
64 | { |
||
65 | $this->addField('domain', 'kuma_translator.adminlist.header.domain', true); |
||
66 | $this->addField('keyword', 'kuma_translator.adminlist.header.keyword', true); |
||
67 | $this->addField('status', 'kuma_translator.adminlist.header.status', true); |
||
68 | } |
||
69 | |||
70 | public function getExportFields() |
||
71 | { |
||
72 | if (empty($this->exportFields)) { |
||
73 | $this->addExportField('domain', 'kuma_translator.adminlist.header.domain'); |
||
74 | $this->addExportField('keyword', 'kuma_translator.adminlist.header.keyword'); |
||
75 | |||
76 | $this->locales = array_unique($this->locales); |
||
77 | // Field building hack... |
||
78 | foreach ($this->locales as $locale) { |
||
79 | $this->addExportField($locale, strtoupper($locale)); |
||
80 | } |
||
81 | |||
82 | $this->addExportField('status', 'kuma_translator.adminlist.header.status'); |
||
83 | } |
||
84 | |||
85 | return $this->exportFields; |
||
86 | } |
||
87 | |||
88 | public function addExportField($name, $header, $template = null, FieldAlias $alias = null) |
||
89 | { |
||
90 | $this->exportFields[] = new Field($name, $header); |
||
91 | |||
92 | return $this; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return bool |
||
97 | */ |
||
98 | public function canAdd() |
||
99 | { |
||
100 | return true; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param object|array $item |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | public function canEdit($item) |
||
109 | { |
||
110 | return false; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param object|array $item |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function canEditInline($item) |
||
119 | { |
||
120 | return true; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return bool |
||
125 | */ |
||
126 | public function canExport() |
||
127 | { |
||
128 | return true; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Override path convention (because settings is a virtual admin subtree) |
||
133 | * |
||
134 | * @param string $suffix |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | View Code Duplication | public function getPathByConvention($suffix = null) |
|
139 | { |
||
140 | if (empty($suffix)) { |
||
141 | return sprintf('%s_settings_%ss', $this->getBundleName(), strtolower($this->getEntityName())); |
||
142 | } |
||
143 | |||
144 | return sprintf('%s_settings_%ss_%s', $this->getBundleName(), strtolower($this->getEntityName()), $suffix); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function getAdminType($item) |
||
151 | { |
||
152 | return null; |
||
153 | } |
||
154 | |||
155 | public function getBundleName() |
||
159 | |||
160 | public function getEntityName() |
||
161 | { |
||
162 | return 'Translation'; |
||
163 | } |
||
164 | |||
165 | public function getControllerPath() |
||
166 | { |
||
167 | return 'KunstmaanTranslatorBundle:Index'; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return QueryBuilder|null |
||
172 | */ |
||
173 | public function getQueryBuilder() |
||
305 | } |
||
306 |