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 |
||
28 | class ContentExtension extends AbstractExtension |
||
29 | { |
||
30 | /** |
||
31 | * @var \eZ\Publish\API\Repository\Repository |
||
32 | */ |
||
33 | protected $repository; |
||
34 | |||
35 | /** |
||
36 | * @var \eZ\Publish\Core\Helper\TranslationHelper |
||
37 | */ |
||
38 | protected $translationHelper; |
||
39 | |||
40 | /** |
||
41 | * @var \eZ\Publish\Core\Helper\FieldHelper |
||
42 | */ |
||
43 | protected $fieldHelper; |
||
44 | |||
45 | /** |
||
46 | * @var LoggerInterface |
||
47 | */ |
||
48 | protected $logger; |
||
49 | |||
50 | public function __construct( |
||
61 | |||
62 | /** |
||
63 | * Returns a list of functions to add to the existing list. |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | public function getFunctions() |
||
100 | |||
101 | /** |
||
102 | * Returns the name of the extension. |
||
103 | * |
||
104 | * @return string The extension name |
||
105 | */ |
||
106 | public function getName() |
||
110 | |||
111 | /** |
||
112 | * @param \eZ\Publish\API\Repository\Values\ValueObject $content Must be a valid Content or ContentInfo object. |
||
113 | * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale) |
||
114 | * |
||
115 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType When $content is not a valid Content or ContentInfo object. |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getTranslatedContentName(ValueObject $content, $forcedLanguage = null) |
||
129 | |||
130 | /** |
||
131 | * Returns the translated field, very similar to getTranslatedFieldValue but this returns the whole field. |
||
132 | * To be used with ez_image_alias for example, which requires the whole field. |
||
133 | * |
||
134 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
135 | * @param string $fieldDefIdentifier Identifier for the field we want to get. |
||
136 | * @param string $forcedLanguage Locale we want the field in (e.g. "cro-HR"). Null by default (takes current locale). |
||
137 | * |
||
138 | * @return \eZ\Publish\API\Repository\Values\Content\Field |
||
139 | */ |
||
140 | public function getTranslatedField(Content $content, $fieldDefIdentifier, $forcedLanguage = null) |
||
144 | |||
145 | /** |
||
146 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
147 | * @param string $fieldDefIdentifier Identifier for the field we want to get the value from. |
||
148 | * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale). |
||
149 | * |
||
150 | * @return mixed A primitive type or a field type Value object depending on the field type. |
||
151 | */ |
||
152 | public function getTranslatedFieldValue(Content $content, $fieldDefIdentifier, $forcedLanguage = null) |
||
156 | |||
157 | /** |
||
158 | * Gets name of a FieldDefinition name by loading ContentType based on Content/ContentInfo object. |
||
159 | * |
||
160 | * @param \eZ\Publish\API\Repository\Values\ValueObject $content Must be Content or ContentInfo object |
||
161 | * @param string $fieldDefIdentifier Identifier for the field we want to get the name from |
||
162 | * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale) |
||
163 | * |
||
164 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType When $content is not a valid Content object. |
||
165 | * |
||
166 | * @return string|null |
||
167 | */ |
||
168 | View Code Duplication | public function getTranslatedFieldDefinitionName(ValueObject $content, $fieldDefIdentifier, $forcedLanguage = null) |
|
181 | |||
182 | /** |
||
183 | * Gets name of a FieldDefinition description by loading ContentType based on Content/ContentInfo object. |
||
184 | * |
||
185 | * @param \eZ\Publish\API\Repository\Values\ValueObject $content Must be Content or ContentInfo object |
||
186 | * @param string $fieldDefIdentifier Identifier for the field we want to get the name from |
||
187 | * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale) |
||
188 | * |
||
189 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType When $content is not a valid Content object. |
||
190 | * |
||
191 | * @return string|null |
||
192 | */ |
||
193 | View Code Duplication | public function getTranslatedFieldDefinitionDescription(ValueObject $content, $fieldDefIdentifier, $forcedLanguage = null) |
|
206 | |||
207 | /** |
||
208 | * Checks if a given field is considered empty. |
||
209 | * This method accepts field as Objects or by identifiers. |
||
210 | * |
||
211 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
212 | * @param \eZ\Publish\API\Repository\Values\Content\Field|string $fieldDefIdentifier Field or Field Identifier to |
||
213 | * get the value from. |
||
214 | * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). |
||
215 | * Null by default (takes current locale). |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function isFieldEmpty(Content $content, $fieldDefIdentifier, $forcedLanguage = null) |
||
227 | |||
228 | /** |
||
229 | * Get ContentType by Content/ContentInfo. |
||
230 | * |
||
231 | * @param \eZ\Publish\API\Repository\Values\Content\Content|\eZ\Publish\API\Repository\Values\Content\ContentInfo $content |
||
232 | * |
||
233 | * @return \eZ\Publish\API\Repository\Values\ContentType\ContentType|null |
||
234 | */ |
||
235 | private function getContentType(ValueObject $content) |
||
245 | |||
246 | public function getFirstFilledImageFieldIdentifier(Content $content) |
||
266 | } |
||
267 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.