Passed
Push — main ( 99c066...305d80 )
by Dimitri
04:43
created

XmlFormatter::format()   C

Complexity

Conditions 15
Paths 100

Size

Total Lines 54
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 21.8664

Importance

Changes 0
Metric Value
cc 15
eloc 26
c 0
b 0
f 0
nc 100
nop 1
dl 0
loc 54
ccs 11
cts 16
cp 0.6875
crap 21.8664
rs 5.9166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Formatter;
13
14
use SimpleXMLElement;
15
16
/**
17
 * Formateur de données XML
18
 */
19
class XmlFormatter implements FormatterInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $basenode = 'xml';
25
26
    /**
27
     * @var SimpleXMLElement
28
     */
29
    protected $structure;
30
31
    /**
32
     * {@inheritDoc}
33
     *
34
     * @return false|string Représentation XML d'une valeur
35
     *                      false en cas d'erreur de formattage
36
     */
37
    public function format($data)
38
    {
39
        if (empty($this->structure)) {
40 2
            $this->structure = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><{$this->basenode} />");
41
        }
42
43
        // Forcez-le à être quelque chose d'utile
44
        if (! is_array($data) && ! is_object($data)) {
45
            $data = (array) $data;
46
        }
47
48
        foreach ($data as $key => $value) {
49
            // change false/true en 0/1
50
            if (is_bool($value)) {
51
                $value = (int) $value;
52
            }
53
54
            // pas de touches numériques dans notre xml s'il vous plait !
55
            if (is_numeric($key)) {
56 2
                helper('inflector');
57
                // crée une clé de chaîne...
58 2
                $key = singular($this->basenode) !== $this->basenode ? singular($this->basenode) : 'item';
59
            }
60
61
            // remplace tout ce qui n'est pas alphanumérique
62 2
            $key = preg_replace('/[^a-z_\-0-9]/i', '', $key);
63
64
            if ($key === '_attributes' && (is_array($value) || is_object($value))) {
65
                $attributes = $value;
66
                if (is_object($attributes)) {
67
                    $attributes = get_object_vars($attributes);
68
                }
69
70
                foreach ($attributes as $attribute_name => $attribute_value) {
71
                    $this->structure->addAttribute($attribute_name, $attribute_value);
72
                }
73
            }
74
            // s'il y a un autre tableau trouvé appelez récursivement cette fonction
75
            elseif (is_array($value) || is_object($value)) {
76 2
                $node = $this->structure->addChild($key);
77
78
                // appel récursif
79 2
                $this->structure = $node;
80 2
                $this->basenode  = $key;
81 2
                $this->format($value);
82
            } else {
83
                // ajouter un seul noeud
84 2
                $value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
85
86 2
                $this->structure->addChild($key, $value);
87
            }
88
        }
89
90 2
        return $this->structure->asXML();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->structure->asXML() also could return the type true which is incompatible with the documented return type false|string.
Loading history...
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     *
96
     * @param string $data Chaine XML
97
     */
98
    public function parse(string $data): array
99
    {
100
        return $data ? (array) simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA) : [];
101
    }
102
}
103