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 |
||
24 | class CardAdmin extends Admin |
||
25 | { |
||
26 | public $supportsPreviewMode = true; |
||
27 | |||
28 | /** |
||
29 | * Get template name based on the name of the page. |
||
30 | * |
||
31 | * @param string $name |
||
32 | * @return string |
||
33 | */ |
||
34 | public function getTemplate($name) |
||
45 | |||
46 | /** |
||
47 | * Configure the fields to be shown in create/edit forms |
||
48 | * |
||
49 | * @param \Sonata\AdminBundle\Form\FormMapper $formMapper |
||
50 | * @return void |
||
51 | */ |
||
52 | protected function configureFormFields(FormMapper $formMapper) |
||
77 | |||
78 | /** |
||
79 | * Configure the fields to be shown in filter form. |
||
80 | * |
||
81 | * @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper |
||
82 | * @return void |
||
83 | */ |
||
84 | View Code Duplication | protected function configureDatagridFilters(DatagridMapper $datagridMapper) |
|
95 | |||
96 | /** |
||
97 | * Configure the columns in list page. |
||
98 | * |
||
99 | * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper |
||
100 | * @return void |
||
101 | */ |
||
102 | protected function configureListFields(ListMapper $listMapper) |
||
120 | |||
121 | /** |
||
122 | * Callback method to the filter form to search by the card title. |
||
123 | * |
||
124 | * @param type $queryBuilder |
||
125 | * @param string $alias |
||
126 | * @param string $field |
||
127 | * @param string $value |
||
128 | * @return boolean |
||
129 | */ |
||
130 | View Code Duplication | public function getSearchFilter($queryBuilder, $alias, $field, $value) |
|
141 | |||
142 | public function preUpdate($card) |
||
146 | |||
147 | public function prePersist($card) |
||
151 | |||
152 | } |
||
153 |
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.