| Total Complexity | 47 |
| Total Lines | 244 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 17 | class XMLDataFormatter extends DataFormatter |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @config |
||
| 22 | * @todo pass this from the API to the data formatter somehow |
||
| 23 | */ |
||
| 24 | private static $api_base = "api/v1/"; |
||
| 25 | |||
| 26 | protected $outputContentType = 'text/xml'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @return array |
||
| 30 | */ |
||
| 31 | public function supportedExtensions() |
||
| 35 | ); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @return array |
||
| 40 | */ |
||
| 41 | public function supportedMimeTypes() |
||
| 46 | ); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param $array |
||
| 51 | * @return string |
||
| 52 | * @throws \Exception |
||
| 53 | */ |
||
| 54 | public function convertArray($array) |
||
| 55 | { |
||
| 56 | $response = Controller::curr()->getResponse(); |
||
| 57 | if ($response) { |
||
| 58 | $response->addHeader("Content-Type", "text/xml"); |
||
| 59 | } |
||
| 60 | |||
| 61 | return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n |
||
| 62 | <response>{$this->convertArrayWithoutHeader($array)}</response>"; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param $array |
||
| 67 | * @return string |
||
| 68 | * @throws \Exception |
||
| 69 | */ |
||
| 70 | public function convertArrayWithoutHeader($array) |
||
| 71 | { |
||
| 72 | $xml = ''; |
||
| 73 | |||
| 74 | foreach ($array as $fieldName => $fieldValue) { |
||
| 75 | if (is_array($fieldValue)) { |
||
| 76 | if (is_numeric($fieldName)) { |
||
| 77 | $fieldName = 'Item'; |
||
| 78 | } |
||
| 79 | |||
| 80 | $xml .= "<{$fieldName}>\n"; |
||
| 81 | $xml .= $this->convertArrayWithoutHeader($fieldValue); |
||
| 82 | $xml .= "</{$fieldName}>\n"; |
||
| 83 | } else { |
||
| 84 | $xml .= "<$fieldName>$fieldValue</$fieldName>\n"; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | return $xml; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Generate an XML representation of the given {@link DataObject}. |
||
| 93 | * |
||
| 94 | * @param DataObject $obj |
||
| 95 | * @param $includeHeader Include <?xml ...?> header (Default: true) |
||
|
|
|||
| 96 | * @return String XML |
||
| 97 | */ |
||
| 98 | public function convertDataObject(DataObjectInterface $obj, $fields = null) |
||
| 99 | { |
||
| 100 | $response = Controller::curr()->getResponse(); |
||
| 101 | if ($response) { |
||
| 102 | $response->addHeader("Content-Type", "text/xml"); |
||
| 103 | } |
||
| 104 | |||
| 105 | return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . $this->convertDataObjectWithoutHeader($obj, $fields); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param DataObject $obj |
||
| 110 | * @param null $fields |
||
| 111 | * @param null $relations |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null, $relations = null) |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Generate an XML representation of the given {@link SS_List}. |
||
| 234 | * |
||
| 235 | * @param SS_List $set |
||
| 236 | * @return String XML |
||
| 237 | */ |
||
| 238 | public function convertDataObjectSet(SS_List $set, $fields = null) |
||
| 239 | { |
||
| 240 | Controller::curr()->getResponse()->addHeader("Content-Type", "text/xml"); |
||
| 241 | $className = $this->sanitiseClassName(get_class($set)); |
||
| 242 | |||
| 243 | $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
||
| 244 | $xml .= (is_numeric($this->totalSize)) ? "<$className totalSize=\"{$this->totalSize}\">\n" : "<$className>\n"; |
||
| 245 | foreach ($set as $item) { |
||
| 246 | $xml .= $this->convertDataObjectWithoutHeader($item, $fields); |
||
| 247 | } |
||
| 248 | $xml .= "</$className>"; |
||
| 249 | |||
| 250 | return $xml; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $strData |
||
| 255 | * @return array|void |
||
| 256 | * @throws \Exception |
||
| 257 | */ |
||
| 258 | public function convertStringToArray($strData) |
||
| 261 | } |
||
| 262 | } |
||
| 263 |
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