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 FieldRenderingExtension extends Twig_Extension |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var FieldBlockRendererInterface|\eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer |
||
| 32 | */ |
||
| 33 | private $fieldBlockRenderer; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var ContentTypeService |
||
| 37 | */ |
||
| 38 | private $contentTypeService; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var ParameterProviderRegistryInterface |
||
| 42 | */ |
||
| 43 | private $parameterProviderRegistry; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var TranslationHelper |
||
| 47 | */ |
||
| 48 | private $translationHelper; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Hash of field type identifiers (i.e. "ezstring"), indexed by field definition identifier. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $fieldTypeIdentifiers = []; |
||
| 56 | |||
| 57 | public function __construct( |
||
| 58 | FieldBlockRendererInterface $fieldBlockRenderer, |
||
| 59 | ContentTypeService $contentTypeService, |
||
| 60 | ParameterProviderRegistryInterface $parameterProviderRegistry, |
||
| 61 | TranslationHelper $translationHelper |
||
| 62 | ) { |
||
| 63 | $this->fieldBlockRenderer = $fieldBlockRenderer; |
||
|
|
|||
| 64 | $this->contentTypeService = $contentTypeService; |
||
| 65 | $this->parameterProviderRegistry = $parameterProviderRegistry; |
||
| 66 | $this->translationHelper = $translationHelper; |
||
| 67 | } |
||
| 68 | |||
| 69 | public function getName() |
||
| 73 | |||
| 74 | public function getFunctions() |
||
| 75 | { |
||
| 76 | return array( |
||
| 77 | new Twig_SimpleFunction( |
||
| 78 | 'ez_render_field', |
||
| 79 | function (Twig_Environment $environment, Content $content, $fieldIdentifier, array $params = []) { |
||
| 80 | $this->fieldBlockRenderer->setTwig($environment); |
||
| 81 | |||
| 82 | return $this->renderField($content, $fieldIdentifier, $params); |
||
| 83 | }, |
||
| 84 | ['is_safe' => ['html'], 'needs_environment' => true] |
||
| 85 | ), |
||
| 86 | new Twig_SimpleFunction( |
||
| 87 | 'ez_render_fielddefinition_settings', |
||
| 88 | function (Twig_Environment $environment, FieldDefinition $fieldDefinition, array $params = []) { |
||
| 89 | $this->fieldBlockRenderer->setTwig($environment); |
||
| 90 | |||
| 91 | return $this->renderFieldDefinitionSettings($fieldDefinition, $params); |
||
| 92 | }, |
||
| 93 | ['is_safe' => ['html'], 'needs_environment' => true] |
||
| 94 | ), |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Renders the HTML for the settings for the given field definition |
||
| 100 | * $definition. |
||
| 101 | * |
||
| 102 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition |
||
| 103 | * |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function renderFieldDefinitionSettings(FieldDefinition $fieldDefinition, array $params = []) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Renders the HTML for a given field. |
||
| 113 | * |
||
| 114 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 115 | * @param string $fieldIdentifier Identifier for the field we want to render |
||
| 116 | * @param array $params An array of parameters to pass to the field view |
||
| 117 | * |
||
| 118 | * @return string The HTML markup |
||
| 119 | * |
||
| 120 | * @throws InvalidArgumentException |
||
| 121 | */ |
||
| 122 | public function renderField(Content $content, $fieldIdentifier, array $params = []) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Generates the array of parameter to pass to the field template. |
||
| 140 | * |
||
| 141 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 142 | * @param \eZ\Publish\API\Repository\Values\Content\Field $field the Field to display |
||
| 143 | * @param array $params An array of parameters to pass to the field view |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | private function getRenderFieldBlockParameters(Content $content, Field $field, array $params = []) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the field type identifier for $field. |
||
| 189 | * |
||
| 190 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 191 | * @param \eZ\Publish\API\Repository\Values\Content\Field $field |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | private function getFieldTypeIdentifier(Content $content, Field $field) |
||
| 209 | } |
||
| 210 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.