Completed
Push — master ( 726362...d621a8 )
by Hong
03:08
created

XmlReader::readFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Reader;
16
17
use Phossa2\Shared\Exception\RuntimeException;
18
19
/**
20
 * Read & parse xml formatted file and return the result
21
 *
22
 * @package Phossa2\Shared
23
 * @author  Hong Zhang <[email protected]>
24
 * @version 2.0.1
25
 * @since   2.0.1 added
26
 */
27
class XmlReader extends ReaderAbstract
28
{
29
    /**
30
     * {@inheritDoc}
31
     */
32
    public static function readFile(/*# string */ $path)
33
    {
34
        // check first
35
        static::checkPath($path);
36
37
        // read it
38
        @$data = simplexml_load_file($path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
39
40
        if (false === $data) {
41
            libxml_use_internal_errors(true);
42
            simplexml_load_file($path, null, \LIBXML_NOERROR);
43
            $errors = libxml_get_errors();
44
            throw new RuntimeException($errors[0]->message);
45
        }
46
47
        return json_decode(json_encode($data), true);
48
    }
49
}
50