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 |
||
17 | class Configuration implements ConfigurationInterface |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | 6 | public function getConfigTreeBuilder() |
|
38 | |||
39 | /** |
||
40 | * Configure underscored bundle prefix naming strategy |
||
41 | * |
||
42 | * @return ArrayNodeDefinition |
||
43 | */ |
||
44 | 6 | View Code Duplication | protected function getUnderscoredBundlePrefixDefinition() |
|
|||
45 | { |
||
46 | 6 | $node = new ArrayNodeDefinition('underscored_bundle_prefix'); |
|
47 | |||
48 | $node |
||
49 | 6 | ->addDefaultsIfNotSet() |
|
50 | 6 | ->children() |
|
51 | 6 | ->enumNode('case') |
|
52 | 6 | ->info('Which case to use, lowercase or uppercase. Default is lowercase.') |
|
53 | 6 | ->values(array('lowercase', 'uppercase')) |
|
54 | 6 | ->defaultValue('lowercase') |
|
55 | 6 | ->end() |
|
56 | 6 | ->booleanNode('join_table_field_suffix') |
|
57 | 6 | ->defaultTrue() |
|
58 | 6 | ->info('Join table will get field name suffix enabling you to have multiple many-to-many relations without stating explicitly table names.') |
|
59 | 6 | ->end() |
|
60 | 6 | ->arrayNode('map') |
|
61 | 6 | ->info('Map of short bundle names and prefixes, if you do not want to use full bundle name in prefix. Useful when bundle name is too long, considering that, per example, MySQL has 60 chars table name limit.') |
|
62 | 6 | ->useAttributeAsKey('bundle') |
|
63 | 6 | ->prototype('scalar')->end() |
|
64 | 6 | ->end() |
|
65 | 6 | ->arrayNode('whitelist') |
|
66 | 6 | ->beforeNormalization() |
|
67 | 6 | ->ifString() |
|
68 | 6 | ->then(function ($value) { |
|
69 | return [$value]; |
||
70 | 6 | }) |
|
71 | 6 | ->end() |
|
72 | 6 | ->info('Define for which bundles to apply prefixes.') |
|
73 | 6 | ->prototype('scalar')->end() |
|
74 | 6 | ->end() |
|
75 | 6 | ->arrayNode('blacklist') |
|
76 | 6 | ->beforeNormalization() |
|
77 | 6 | ->ifString() |
|
78 | 6 | ->then(function ($value) { |
|
79 | 1 | return [$value]; |
|
80 | 6 | }) |
|
81 | 6 | ->end() |
|
82 | 6 | ->info('Define for which bundles not to apply prefixes.') |
|
83 | 6 | ->prototype('scalar')->end() |
|
84 | 6 | ->end() |
|
85 | 6 | ->end() |
|
86 | 6 | ->end(); |
|
87 | |||
88 | 6 | return $node; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Configure underscored class namespace prefix naming strategy |
||
93 | * |
||
94 | * @return ArrayNodeDefinition |
||
95 | */ |
||
96 | 6 | View Code Duplication | protected function getUnderscoredClassNamespacePrefixDefinition() |
97 | { |
||
98 | 6 | $node = new ArrayNodeDefinition('underscored_class_namespace_prefix'); |
|
99 | |||
100 | $node |
||
101 | 6 | ->addDefaultsIfNotSet() |
|
102 | 6 | ->children() |
|
103 | 6 | ->enumNode('case') |
|
104 | 6 | ->info('Which case to use, lowercase or uppercase. Default is lowercase.') |
|
105 | 6 | ->values(array('lowercase', 'uppercase')) |
|
106 | 6 | ->defaultValue('lowercase') |
|
107 | 6 | ->end() |
|
108 | 6 | ->booleanNode('join_table_field_suffix') |
|
109 | 6 | ->defaultTrue() |
|
110 | 6 | ->info('Join table will get field name suffix enabling you to have multiple many-to-many relations without stating explicitly table names.') |
|
111 | 6 | ->end() |
|
112 | 6 | ->arrayNode('map') |
|
113 | 6 | ->requiresAtLeastOneElement() |
|
114 | 6 | ->info('Map of FQCNs prefixes and table prefixes to use for naming.') |
|
115 | 6 | ->useAttributeAsKey('namespace') |
|
116 | 6 | ->prototype('scalar')->end() |
|
117 | 6 | ->end() |
|
118 | 6 | ->arrayNode('whitelist') |
|
119 | 6 | ->beforeNormalization() |
|
120 | 6 | ->ifString() |
|
121 | 6 | ->then(function ($value) { |
|
122 | return [$value]; |
||
123 | 6 | }) |
|
124 | 6 | ->end() |
|
125 | 6 | ->info('Define for which FQCNs prefixes table prefixes should be applied.') |
|
126 | 6 | ->prototype('scalar')->end() |
|
127 | 6 | ->end() |
|
128 | 6 | ->arrayNode('blacklist') |
|
129 | 6 | ->beforeNormalization() |
|
130 | 6 | ->ifString() |
|
131 | 6 | ->then(function ($value) { |
|
132 | return [$value]; |
||
133 | 6 | }) |
|
134 | 6 | ->end() |
|
135 | 6 | ->info('Define for which FQCNs prefixes not to apply table prefixes.') |
|
136 | 6 | ->prototype('scalar')->end() |
|
137 | 6 | ->end() |
|
138 | 6 | ->end() |
|
139 | 6 | ->end(); |
|
140 | |||
141 | 6 | return $node; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Configure namer collection |
||
146 | * |
||
147 | * @return ArrayNodeDefinition |
||
148 | */ |
||
149 | 6 | protected function getNamerCollectionDefinition() |
|
186 | |||
187 | } |
||
188 |
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.