Response::serverTransactionId()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * This file is part of the php-epp2 library.
5
 *
6
 * (c) Gunter Grodotzki <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace AfriCC\EPP\Frame;
13
14
use AfriCC\EPP\AbstractFrame;
15
use AfriCC\EPP\DOM\DOMTools;
16
use AfriCC\EPP\Frame\Response\Result;
17
use DOMNodeList;
18
19
/**
20
 * @see http://tools.ietf.org/html/rfc5730#section-2.6
21
 */
22
class Response extends AbstractFrame
23
{
24
    /**
25
     * Get Results from response frame
26
     *
27
     * @return Result[]
28
     */
29
    public function results()
30
    {
31
        $results = [];
32
        $nodes = $this->get('//epp:epp/epp:response/epp:result');
33
        foreach ($nodes as $node) {
0 ignored issues
show
Bug introduced by
The expression $nodes of type false|string|object<DOMNodeList> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
34
            $results[] = new Result($node);
35
        }
36
37
        return $results;
38
    }
39
40
    /**
41
     * Whether response is successful
42
     *
43
     * @todo On response with multiple codes this fails miserably
44
     *
45
     * @return bool true on succes, false otherwise.
46
     */
47
    public function success()
48
    {
49
        $code = $this->code();
50
        if ($code >= 1000 && $code < 2000) {
51
            return true;
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * Get Response code
59
     *
60
     * @todo on response with multiple results this fails miserably
61
     *
62
     * @return int response code
63
     */
64
    public function code()
65
    {
66
        return (int) $this->get('//epp:epp/epp:response/epp:result/@code');
67
    }
68
69
    /**
70
     * Get Response message
71
     *
72
     * @todo check message against multiple responses
73
     *
74
     * @return string message
75
     */
76
    public function message()
77
    {
78
        return (string) $this->get('//epp:epp/epp:response/epp:result/epp:msg/text()');
79
    }
80
81
    public function clientTransactionId()
82
    {
83
        $value = $this->get('//epp:epp/epp:response/epp:trID/epp:clTRID/text()');
84
        if ($value === false) {
85
            return;
86
        }
87
88
        return (string) $value;
89
    }
90
91
    public function serverTransactionId()
92
    {
93
        $value = $this->get('//epp:epp/epp:response/epp:trID/epp:svTRID/text()');
94
        if ($value === false) {
95
            return;
96
        }
97
98
        return (string) $value;
99
    }
100
101
    public function data()
102
    {
103
        $nodes = $this->get('//epp:epp/epp:response/epp:resData');
104
        if ($nodes === false || !($nodes instanceof DOMNodeList) || $nodes->length === 0 || !$nodes->item(0)->hasChildNodes()) {
105
            $data = [];
106
        } else {
107
            $data = DOMTools::nodeToArray($nodes->item(0));
0 ignored issues
show
Compatibility introduced by
$nodes->item(0) of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
108
        }
109
110
        // check for extension data
111
        $nodes = $this->get('//epp:epp/epp:response/epp:extension');
112
        if ($nodes !== false && $nodes instanceof DOMNodeList && $nodes->length > 0 && $nodes->item(0)->hasChildNodes()) {
113
            $data = array_merge_recursive($data, DOMTools::nodeToArray($nodes->item(0)));
0 ignored issues
show
Compatibility introduced by
$nodes->item(0) of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
114
        }
115
116
        return $data;
117
    }
118
}
119