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 |
||
27 | class Configuration implements ConfigurationInterface |
||
28 | { |
||
29 | private $debug; |
||
30 | |||
31 | /** |
||
32 | * @param boolean $debug |
||
33 | */ |
||
34 | 52 | public function __construct($debug = false) |
|
38 | |||
39 | 52 | public function getConfigTreeBuilder() |
|
40 | 1 | { |
|
41 | 52 | $tb = new TreeBuilder(); |
|
42 | |||
43 | $root = $tb |
||
44 | 52 | ->root('jms_serializer', 'array') |
|
45 | 52 | ->children() |
|
46 | 52 | ; |
|
47 | |||
48 | 52 | $this->addHandlersSection($root); |
|
49 | 52 | $this->addSubscribersSection($root); |
|
50 | 52 | $this->addObjectConstructorsSection($root); |
|
51 | 52 | $this->addSerializersSection($root); |
|
52 | 52 | $this->addMetadataSection($root); |
|
53 | 52 | $this->addVisitorsSection($root); |
|
54 | 52 | $this->addContextSection($root); |
|
55 | |||
56 | 52 | return $tb; |
|
57 | } |
||
58 | |||
59 | 52 | private function addHandlersSection(NodeBuilder $builder) |
|
60 | { |
||
61 | $builder |
||
62 | 52 | ->arrayNode('handlers') |
|
63 | 52 | ->addDefaultsIfNotSet() |
|
64 | 52 | ->children() |
|
65 | 52 | ->arrayNode('datetime') |
|
66 | 52 | ->addDefaultsIfNotSet() |
|
67 | 52 | ->children() |
|
68 | 52 | ->scalarNode('default_format')->defaultValue(\DateTime::RFC3339)->end() |
|
69 | 52 | ->scalarNode('default_timezone')->defaultValue(date_default_timezone_get())->end() |
|
70 | 52 | ->scalarNode('cdata')->defaultTrue()->end() |
|
71 | 52 | ->end() |
|
72 | 52 | ->end() |
|
73 | 52 | ->arrayNode('array_collection') |
|
74 | 52 | ->addDefaultsIfNotSet() |
|
75 | 52 | ->children() |
|
76 | 52 | ->booleanNode('initialize_excluded')->defaultFalse()->end() |
|
77 | 52 | ->end() |
|
78 | 52 | ->end() |
|
79 | 52 | ->end() |
|
80 | 52 | ->end() |
|
81 | ; |
||
82 | 52 | } |
|
83 | |||
84 | 52 | private function addSubscribersSection(NodeBuilder $builder) |
|
85 | { |
||
86 | $builder |
||
87 | 52 | ->arrayNode('subscribers') |
|
88 | 52 | ->addDefaultsIfNotSet() |
|
89 | 52 | ->children() |
|
90 | 52 | ->arrayNode('doctrine_proxy') |
|
91 | 52 | ->addDefaultsIfNotSet() |
|
92 | 52 | ->children() |
|
93 | 52 | ->booleanNode('initialize_excluded')->defaultFalse()->end() |
|
94 | 52 | ->booleanNode('initialize_virtual_types')->defaultFalse()->end() |
|
95 | 52 | ->end() |
|
96 | 52 | ->end() |
|
97 | 52 | ->end() |
|
98 | 52 | ->end() |
|
99 | ; |
||
100 | 52 | } |
|
101 | |||
102 | 52 | private function addObjectConstructorsSection(NodeBuilder $builder) |
|
103 | { |
||
104 | $builder |
||
105 | 52 | ->arrayNode('object_constructors') |
|
106 | 52 | ->addDefaultsIfNotSet() |
|
107 | 52 | ->children() |
|
108 | 52 | ->arrayNode('doctrine') |
|
109 | 52 | ->addDefaultsIfNotSet() |
|
110 | 52 | ->children() |
|
111 | 52 | ->enumNode('fallback_strategy') |
|
112 | 52 | ->defaultValue("null") |
|
113 | 52 | ->values(["null", "exception", "fallback"]) |
|
114 | 52 | ->end() |
|
115 | 52 | ->end() |
|
116 | 52 | ->end() |
|
117 | 52 | ->end() |
|
118 | 52 | ->end() |
|
119 | ; |
||
120 | 52 | } |
|
121 | |||
122 | 52 | private function addSerializersSection(NodeBuilder $builder) |
|
123 | { |
||
124 | $builder |
||
125 | 52 | ->arrayNode('property_naming') |
|
126 | 52 | ->addDefaultsIfNotSet() |
|
127 | 52 | ->beforeNormalization() |
|
128 | 52 | ->ifString() |
|
129 | ->then(function ($id) { |
||
130 | 1 | return array('id' => $id); |
|
131 | 52 | }) |
|
132 | 52 | ->end() |
|
133 | 52 | ->children() |
|
134 | 52 | ->scalarNode('id')->cannotBeEmpty()->end() |
|
135 | 52 | ->scalarNode('separator')->defaultValue('_')->end() |
|
136 | 52 | ->booleanNode('lower_case')->defaultTrue()->end() |
|
137 | 52 | ->booleanNode('enable_cache')->defaultTrue()->end() |
|
138 | 52 | ->end() |
|
139 | 52 | ->end() |
|
140 | 52 | ->arrayNode('expression_evaluator') |
|
141 | 52 | ->addDefaultsIfNotSet() |
|
142 | 52 | ->beforeNormalization() |
|
143 | 52 | ->ifString() |
|
144 | ->then(function ($id) { |
||
145 | 1 | return array('id' => $id); |
|
146 | 52 | }) |
|
147 | 52 | ->end() |
|
148 | 52 | ->children() |
|
149 | 52 | ->scalarNode('id') |
|
150 | ->defaultValue(function () { |
||
151 | 48 | if (interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) { |
|
152 | 48 | return 'jms_serializer.expression_evaluator'; |
|
153 | } |
||
154 | return null; |
||
155 | 52 | }) |
|
156 | 52 | ->validate() |
|
157 | ->always(function($v) { |
||
158 | 3 | if (!empty($v) && !interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) { |
|
159 | throw new InvalidArgumentException('You need at least symfony/expression language v2.6 or v3.0 to use the expression evaluator features'); |
||
160 | } |
||
161 | 3 | return $v; |
|
162 | 52 | }) |
|
163 | 52 | ->end() |
|
164 | 52 | ->end() |
|
165 | 52 | ->end() |
|
166 | ; |
||
167 | 52 | } |
|
168 | |||
169 | 52 | private function addMetadataSection(NodeBuilder $builder) |
|
170 | { |
||
171 | $builder |
||
172 | 52 | ->arrayNode('metadata') |
|
173 | 52 | ->addDefaultsIfNotSet() |
|
174 | 52 | ->fixXmlConfig('directory', 'directories') |
|
175 | 52 | ->children() |
|
176 | 52 | ->scalarNode('cache')->defaultValue('file')->end() |
|
177 | 52 | ->booleanNode('debug')->defaultValue($this->debug)->end() |
|
178 | 52 | ->arrayNode('file_cache') |
|
179 | 52 | ->addDefaultsIfNotSet() |
|
180 | 52 | ->children() |
|
181 | 52 | ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/jms_serializer')->end() |
|
182 | 52 | ->end() |
|
183 | 52 | ->end() |
|
184 | 52 | ->booleanNode('auto_detection')->defaultTrue()->end() |
|
185 | 52 | ->booleanNode('infer_types_from_doctrine_metadata') |
|
186 | 52 | ->info('Infers type information from Doctrine metadata if no explicit type has been defined for a property.') |
|
187 | 52 | ->defaultTrue() |
|
188 | 52 | ->end() |
|
189 | 52 | ->arrayNode('directories') |
|
190 | 52 | ->useAttributeAsKey('name') |
|
191 | 52 | ->prototype('array') |
|
192 | 52 | ->children() |
|
193 | 52 | ->scalarNode('path')->isRequired()->end() |
|
194 | 52 | ->scalarNode('namespace_prefix')->defaultValue('')->end() |
|
195 | 52 | ->end() |
|
196 | 52 | ->end() |
|
197 | 52 | ->end() |
|
198 | 52 | ->end() |
|
199 | 52 | ->end() |
|
200 | ; |
||
201 | 52 | } |
|
202 | |||
203 | 52 | private function addVisitorsSection(NodeBuilder $builder) |
|
271 | |||
272 | 52 | private function addContextSection(NodeBuilder $builder) |
|
281 | |||
282 | 52 | private function createContextNode(NodeBuilder $builder, $name) |
|
333 | } |
||
334 |
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.