Issues (343)

src/XML/SerializableElementTrait.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\DOMDocumentFactory;
0 ignored issues
show
The type SimpleSAML\XML\DOMDocumentFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
use function array_pop;
11
use function get_object_vars;
12
13
/**
14
 * Trait grouping common functionality for elements implementing the SerializableElement element.
15
 *
16
 * @package simplesamlphp/xml-common
17
 */
18
trait SerializableElementTrait
19
{
20
    /**
21
     * Whether to format the string output of this element or not.
22
     *
23
     * Defaults to true. Override to disable output formatting.
24
     */
25
    protected bool $formatOutput = true;
26
27
28
    /**
29
     * Output the class as an XML-formatted string
30
     */
31
    public function __toString(): string
32
    {
33
        $xml = $this->toXML();
34
35
        $xmlString = $xml->ownerDocument->saveXML();
0 ignored issues
show
The method saveXML() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        /** @scrutinizer ignore-call */ 
36
        $xmlString = $xml->ownerDocument->saveXML();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
37
        $doc = DOMDocumentFactory::fromString($xmlString);
38
        $doc->formatOutput = $this->formatOutput;
39
40
        if (static::getNormalization() === true) {
41
            $normalized = DOMDocumentFactory::normalizeDocument($doc);
42
            $normalized->normalizeDocument();
43
            return $normalized->saveXML($normalized->firstChild);
44
        }
45
46
        $doc->normalizeDocument();
47
        return $doc->saveXML($doc->firstChild);
48
    }
49
50
51
    /**
52
     * Serialize this XML chunk.
53
     *
54
     * This method will be invoked by any calls to serialize().
55
     *
56
     * @return array{0: string} The serialized representation of this XML object.
57
     */
58
    public function __serialize(): array
59
    {
60
        $xml = $this->toXML();
61
        return [$xml->ownerDocument->saveXML($xml)];
62
    }
63
64
65
    /**
66
     * Unserialize an XML object and load it..
67
     *
68
     * This method will be invoked by any calls to unserialize(), allowing us to restore any data that might not
69
     * be serializable in its original form (e.g.: DOM objects).
70
     *
71
     * @param array{0: string} $serialized The XML object that we want to restore.
72
     */
73
    public function __unserialize(array $serialized): void
74
    {
75
        $xml = static::fromXML(
76
            DOMDocumentFactory::fromString(array_pop($serialized))->documentElement,
77
        );
78
79
        $vars = get_object_vars($xml);
80
        foreach ($vars as $k => $v) {
81
            $this->$k = $v;
82
        }
83
    }
84
85
86
    /**
87
     * Create XML from this class
88
     *
89
     * @param \DOMElement|null $parent
90
     */
91
    abstract public function toXML(?DOMElement $parent = null): DOMElement;
92
}
93