Issues (1)

src/FileSystem/Client/CabalMsg.php (1 issue)

1
<?php
2
3
namespace cyberinferno\Cabal\Helpers\FileSystem\Client;
4
5
use cyberinferno\Cabal\Helpers\Exceptions\FileBadFormatException;
6
use cyberinferno\Cabal\Helpers\FileSystem\File;
7
8
/**
9
 * Class CabalMsg
10
 *
11
 * Helper class to load contents from cabal_msg.dec file
12
 *
13
 * @package cyberinferno\Cabal\Helpers\FileSystem\Client
14
 * @author Karthik P
15
 */
16
class CabalMsg extends File
17
{
18
    /**
19
     * @var \SimpleXMLElement
20
     */
21
    protected $_xmlObject;
22
23
    /**
24
     * Returns the XML object of the specified cabal_msg.dec file
25
     *
26
     * @return \SimpleXMLElement
27
     */
28 6
    public function getXmlObject()
29
    {
30 6
        if (isset($this->_xmlObject) === false) {
31 6
            libxml_use_internal_errors(true);
32 6
            $domDocument = new \DOMDocument();
33 6
            $domDocument->recover = true;
34 6
            $domDocument->loadXML($this->getFileContents());
35 6
            $this->_xmlObject = simplexml_load_string($domDocument->saveXML());
0 ignored issues
show
Documentation Bug introduced by
It seems like simplexml_load_string($domDocument->saveXML()) can also be of type false. However, the property $_xmlObject is declared as type SimpleXMLElement. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
36
        }
37 6
        return $this->_xmlObject;
38
    }
39
40
    /**
41
     * Returns array representation of attributes of the XML tree node attribute
42
     * If $topLevelKey is sent only that node's attributes are returned
43
     *
44
     * WARNING: This method might take lot of time to complete when XML tree is large
45
     *
46
     * @param string $topLevelKey
47
     * @return array
48
     */
49 3
    public function getArray($topLevelKey = '')
50
    {
51 3
        $xmlObject = $this->getXmlObject();
52 3
        if (!empty($topLevelKey)) {
53 3
            return get_object_vars($xmlObject->$topLevelKey);
54
        }
55 3
        return get_object_vars($xmlObject);
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 12
    protected function validateFormat()
62
    {
63 12
        parent::validateFormat();
64 12
        $fileContents = $this->getFileContents();
65 12
        if (substr($fileContents, 0, strlen('<cabal_message>')) !== '<cabal_message>') {
66 3
            throw new FileBadFormatException();
67
        }
68
    }
69
}