GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

XmlFile::readFile()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 10
nop 2
dl 0
loc 23
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Filesystem\Files;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Filesystem\File;
19
use O2System\Filesystem\Files\Abstracts\AbstractFile;
20
use O2System\Spl\DataStructures\SplArrayObject;
21
use O2System\Spl\Iterators\ArrayIterator;
22
23
/**
24
 * Class XmlFile
25
 *
26
 * @package O2System\Filesystem\Factory
27
 */
28
class XmlFile extends AbstractFile
29
{
30
    protected $fileExtension = '.xml';
31
32
    /**
33
     * XmlFile::readFile
34
     *
35
     * @param string $filePath Path to the file.
36
     * @param array  $options  Read file options.
37
     *
38
     * @return mixed
39
     */
40
    public function readFile($filePath = null, array $options = [])
41
    {
42
        $filePath = empty($filePath)
43
            ? $this->filePath
44
            : $filePath;
45
46
        $result = new ArrayIterator();
47
48
        if (false !== ($xml = simplexml_load_file($filePath))) {
49
            $contents = json_decode(json_encode($xml), true); // force to array conversion
50
51
            if (count($contents) == 1) {
52
                $contents = reset($contents);
53
            }
54
55
            if (json_last_error() === JSON_ERROR_NONE) {
56
                foreach ($contents as $content) {
57
                    $result[] = new SplArrayObject($content);
58
                }
59
            }
60
        }
61
62
        return $result;
63
    }
64
65
    // ------------------------------------------------------------------------
66
67
    /**
68
     * XmlFile::writeFile
69
     *
70
     * @param string $filePath Path to the file.
71
     * @param array  $options  Write file options.
72
     *
73
     * @return bool Returns TRUE on success or FALSE on failure.
74
     */
75
    public function writeFile($filePath = null, array $options = [])
76
    {
77
        $filePath = empty($filePath)
78
            ? $this->filePath
79
            : $filePath;
80
81
        if ($this->count()) {
82
            $root = '<' . pathinfo($filePath, PATHINFO_FILENAME) . '/>';
83
84
            $contents = $this->getArrayCopy();
85
            $xml = new \SimpleXMLElement($root);
86
            array_walk_recursive($contents, [&$xml, 'addChild']);
87
88
            return (new File())->write($filePath, $xml->asXML());
89
        }
90
    }
91
}