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 |
||
| 30 | class ContentExtension extends Twig_Extension |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var \eZ\Publish\API\Repository\Repository |
||
| 34 | */ |
||
| 35 | protected $repository; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \eZ\Publish\Core\Helper\TranslationHelper |
||
| 39 | */ |
||
| 40 | protected $translationHelper; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var \eZ\Publish\Core\Helper\FieldHelper |
||
| 44 | */ |
||
| 45 | protected $fieldHelper; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var LoggerInterface |
||
| 49 | */ |
||
| 50 | protected $logger; |
||
| 51 | |||
| 52 | public function __construct( |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Returns a list of functions to add to the existing list. |
||
| 66 | * |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | public function getFunctions() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Returns the name of the extension. |
||
| 109 | * |
||
| 110 | * @return string The extension name |
||
| 111 | */ |
||
| 112 | public function getName() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param \eZ\Publish\API\Repository\Values\ValueObject $content Must be a valid Content or ContentInfo object. |
||
| 119 | * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale) |
||
| 120 | * |
||
| 121 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType When $content is not a valid Content or ContentInfo object. |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getTranslatedContentName(ValueObject $content, $forcedLanguage = null) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Returns the translated field, very similar to getTranslatedFieldValue but this returns the whole field. |
||
| 138 | * To be used with ez_image_alias for example, which requires the whole field. |
||
| 139 | * |
||
| 140 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 141 | * @param string $fieldDefIdentifier Identifier for the field we want to get. |
||
| 142 | * @param string $forcedLanguage Locale we want the field in (e.g. "cro-HR"). Null by default (takes current locale). |
||
| 143 | * |
||
| 144 | * @return \eZ\Publish\API\Repository\Values\Content\Field |
||
| 145 | */ |
||
| 146 | public function getTranslatedField(Content $content, $fieldDefIdentifier, $forcedLanguage = null) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 153 | * @param string $fieldDefIdentifier Identifier for the field we want to get the value from. |
||
| 154 | * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale). |
||
| 155 | * |
||
| 156 | * @return mixed A primitive type or a field type Value object depending on the field type. |
||
| 157 | */ |
||
| 158 | public function getTranslatedFieldValue(Content $content, $fieldDefIdentifier, $forcedLanguage = null) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Gets name of a FieldDefinition name by loading ContentType based on Content/ContentInfo object. |
||
| 165 | * |
||
| 166 | * @param \eZ\Publish\API\Repository\Values\ValueObject $content Must be Content or ContentInfo object |
||
| 167 | * @param string $fieldDefIdentifier Identifier for the field we want to get the name from |
||
| 168 | * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale) |
||
| 169 | * |
||
| 170 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType When $content is not a valid Content object. |
||
| 171 | * |
||
| 172 | * @return string|null |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function getTranslatedFieldDefinitionName(ValueObject $content, $fieldDefIdentifier, $forcedLanguage = null) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Gets name of a FieldDefinition description by loading ContentType based on Content/ContentInfo object. |
||
| 190 | * |
||
| 191 | * @param \eZ\Publish\API\Repository\Values\ValueObject $content Must be Content or ContentInfo object |
||
| 192 | * @param string $fieldDefIdentifier Identifier for the field we want to get the name from |
||
| 193 | * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale) |
||
| 194 | * |
||
| 195 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType When $content is not a valid Content object. |
||
| 196 | * |
||
| 197 | * @return string|null |
||
| 198 | */ |
||
| 199 | View Code Duplication | public function getTranslatedFieldDefinitionDescription(ValueObject $content, $fieldDefIdentifier, $forcedLanguage = null) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Gets translated property generic helper. |
||
| 215 | * |
||
| 216 | * For generic use, expects property in singular form. For instance if 'name' is provided it will first look for |
||
| 217 | * getName( $lang ) method, then property called ->names[$lang], in either case look for correct translation. |
||
| 218 | * |
||
| 219 | * Languages will consist of either forced language or current SiteAccess languages list, in addition for property |
||
| 220 | * lookup helper will look for mainLanguage property and use it if either alwaysAvailable property is true or non- |
||
| 221 | * existing. |
||
| 222 | * |
||
| 223 | * @param \eZ\Publish\API\Repository\Values\ValueObject $object Can be any kid of Value object which directly holds the translated data |
||
| 224 | * @param string $property Property name, example 'name', 'description' |
||
| 225 | * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale) |
||
| 226 | * |
||
| 227 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue If $property does not exists as plural or as method |
||
| 228 | * |
||
| 229 | * @return string|null |
||
| 230 | */ |
||
| 231 | public function getTranslatedProperty(ValueObject $object, $property, $forcedLanguage = null) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Checks if a given field is considered empty. |
||
| 253 | * This method accepts field as Objects or by identifiers. |
||
| 254 | * |
||
| 255 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 256 | * @param \eZ\Publish\API\Repository\Values\Content\Field|string $fieldDefIdentifier Field or Field Identifier to |
||
| 257 | * get the value from. |
||
| 258 | * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). |
||
| 259 | * Null by default (takes current locale). |
||
| 260 | * |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | public function isFieldEmpty(Content $content, $fieldDefIdentifier, $forcedLanguage = null) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get ContentType by Content/ContentInfo. |
||
| 274 | * |
||
| 275 | * @param \eZ\Publish\API\Repository\Values\Content\Content|\eZ\Publish\API\Repository\Values\Content\ContentInfo $content |
||
| 276 | * |
||
| 277 | * @return \eZ\Publish\API\Repository\Values\ContentType\ContentType|null |
||
| 278 | */ |
||
| 279 | private function getContentType(ValueObject $content) |
||
| 289 | |||
| 290 | public function getFirstFilledImageFieldIdentifier(Content $content) |
||
| 311 | } |
||
| 312 |
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.