Total Complexity | 41 |
Total Lines | 185 |
Duplicated Lines | 26.49 % |
Changes | 0 |
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:
Complex classes like XMLDataFormatter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XMLDataFormatter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class XMLDataFormatter extends DataFormatter |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @config |
||
21 | * @todo pass this from the API to the data formatter somehow |
||
22 | */ |
||
23 | private static $api_base = "api/v1/"; |
||
24 | |||
25 | protected $outputContentType = 'text/xml'; |
||
26 | |||
27 | public function supportedExtensions() |
||
28 | { |
||
29 | return array( |
||
30 | 'xml' |
||
31 | ); |
||
32 | } |
||
33 | |||
34 | public function supportedMimeTypes() |
||
35 | { |
||
36 | return array( |
||
37 | 'text/xml', |
||
38 | 'application/xml', |
||
39 | ); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Generate an XML representation of the given {@link DataObject}. |
||
44 | * |
||
45 | * @param DataObject $obj |
||
46 | * @param $includeHeader Include <?xml ...?> header (Default: true) |
||
|
|||
47 | * @return String XML |
||
48 | */ |
||
49 | public function convertDataObject(DataObjectInterface $obj, $fields = null) |
||
50 | { |
||
51 | $response = Controller::curr()->getResponse(); |
||
52 | if ($response) { |
||
53 | $response->addHeader("Content-Type", "text/xml"); |
||
54 | } |
||
55 | |||
56 | return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . $this->convertDataObjectWithoutHeader($obj, $fields); |
||
57 | } |
||
58 | |||
59 | public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null, $relations = null) |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Generate an XML representation of the given {@link SS_List}. |
||
179 | * |
||
180 | * @param SS_List $set |
||
181 | * @return String XML |
||
182 | */ |
||
183 | public function convertDataObjectSet(SS_List $set, $fields = null) |
||
184 | { |
||
185 | Controller::curr()->getResponse()->addHeader("Content-Type", "text/xml"); |
||
186 | $className = $this->sanitiseClassName(get_class($set)); |
||
187 | |||
188 | $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
||
189 | $xml .= (is_numeric($this->totalSize)) ? "<$className totalSize=\"{$this->totalSize}\">\n" : "<$className>\n"; |
||
190 | foreach ($set as $item) { |
||
191 | $xml .= $this->convertDataObjectWithoutHeader($item, $fields); |
||
192 | } |
||
193 | $xml .= "</$className>"; |
||
194 | |||
195 | return $xml; |
||
196 | } |
||
197 | |||
198 | public function convertStringToArray($strData) |
||
201 | } |
||
202 | } |
||
203 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths