EwsSearchDiagnosticsType::setAny()   B
last analyzed

Complexity

Conditions 10
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 4
nc 3
nop 1
dl 0
loc 9
rs 7.6666
c 0
b 0
f 0

How to fix   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
declare(strict_types=1);
4
5
namespace StructType;
6
7
use InvalidArgumentException;
8
use WsdlToPhp\PackageBase\AbstractStructBase;
9
10
/**
11
 * This class stands for SearchDiagnosticsType StructType
12
 * @package Ews
13
 * @subpackage Structs
14
 * @author WsdlToPhp <[email protected]>
15
 */
16
class EwsSearchDiagnosticsType extends AbstractStructBase
17
{
18
    /**
19
     * The any
20
     * @var \DOMDocument|string|null
21
     */
22
    protected $any = null;
23
    /**
24
     * Constructor method for SearchDiagnosticsType
25
     * @uses EwsSearchDiagnosticsType::setAny()
26
     * @param \DOMDocument|string|null $any
27
     */
28
    public function __construct($any = null)
29
    {
30
        $this
31
            ->setAny($any);
32
    }
33
    /**
34
     * Get any value
35
     * @uses \DOMDocument::loadXML()
36
     * @param bool $asString true: returns XML string, false: returns \DOMDocument
37
     * @return \DOMDocument|string|null
38
     */
39
    public function getAny(bool $asDomDocument = false)
40
    {
41
        $domDocument = null;
42
        if (!empty($this->any) && $asDomDocument) {
43
            $domDocument = new \DOMDocument('1.0', 'UTF-8');
44
            $domDocument->loadXML($this->any);
0 ignored issues
show
Bug introduced by
It seems like $this->any can also be of type DOMDocument; however, parameter $source of DOMDocument::loadXML() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

44
            $domDocument->loadXML(/** @scrutinizer ignore-type */ $this->any);
Loading history...
45
        }
46
        return $asDomDocument ? $domDocument : $this->any;
47
    }
48
    /**
49
     * Set any value
50
     * @uses \DOMDocument::hasChildNodes()
51
     * @uses \DOMDocument::saveXML()
52
     * @uses \DOMNode::item()
53
     * @param \DOMDocument|string|null $any
54
     * @return \StructType\EwsSearchDiagnosticsType
55
     */
56
    public function setAny($any = null): self
57
    {
58
        // validation for constraint: xml
59
        if (!is_null($any) && !$any instanceof \DOMDocument && (!is_string($any) || (is_string($any) && (empty($any) || (($anyDoc = new \DOMDocument()) && false === $anyDoc->loadXML($any)))))) {
0 ignored issues
show
introduced by
The condition is_string($any) is always true.
Loading history...
60
            throw new InvalidArgumentException(sprintf('Invalid value %s, please provide a valid XML string', var_export($any, true)), __LINE__);
61
        }
62
        $this->any = ($any instanceof \DOMDocument) ? $any->saveXML($any->hasChildNodes() ? $any->childNodes->item(0) : null) : $any;
63
        
64
        return $this;
65
    }
66
}
67