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 |
||
| 19 | class FieldFactory |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Application configuration |
||
| 23 | * |
||
| 24 | * @var ApplicationConfiguration |
||
| 25 | */ |
||
| 26 | protected $configuration; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Field class mapping array, indexed by field type. |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $fieldsMapping = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Translator for field values. |
||
| 37 | * |
||
| 38 | * @var TranslatorInterface |
||
| 39 | */ |
||
| 40 | protected $translator; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Twig engine. |
||
| 44 | * |
||
| 45 | * @var Twig_Environment |
||
| 46 | */ |
||
| 47 | protected $twig; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * FieldFactory constructor. |
||
| 51 | * |
||
| 52 | * @param ConfigurationFactory $configurationFactory |
||
| 53 | * @param TranslatorInterface $translator |
||
| 54 | * @param Twig_Environment $twig |
||
| 55 | */ |
||
| 56 | View Code Duplication | public function __construct( |
|
|
1 ignored issue
–
show
|
|||
| 57 | ConfigurationFactory $configurationFactory, |
||
| 58 | TranslatorInterface $translator, |
||
| 59 | Twig_Environment $twig |
||
| 60 | ) { |
||
| 61 | $this->configuration = $configurationFactory->getApplicationConfiguration(); |
||
| 62 | $this->fieldsMapping = $this |
||
| 63 | ->configuration |
||
| 64 | ->getParameter('fields_mapping'); // shortcut to field mapping array |
||
| 65 | $this->translator = $translator; |
||
| 66 | $this->twig = $twig; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Create a new field with its renderer. |
||
| 71 | * |
||
| 72 | * @param $fieldName |
||
| 73 | * @param array $configuration |
||
| 74 | * |
||
| 75 | * @return Field |
||
| 76 | * |
||
| 77 | * @throws Exception |
||
| 78 | */ |
||
| 79 | public function create($fieldName, array $configuration = []) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Return field class according to the field type. If the type is not present in the field mapping array, an |
||
| 149 | * exception will be thrown. |
||
| 150 | * |
||
| 151 | * @param $type |
||
| 152 | * @return string |
||
| 153 | * @throws Exception |
||
| 154 | */ |
||
| 155 | public function getFieldMapping($type) |
||
| 163 | } |
||
| 164 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.