Passed
Push — master ( 6b38f0...d27048 )
by Pol
01:41
created

MockSoapClient::__soapCall()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 2
b 0
f 0
nc 3
nop 5
dl 0
loc 24
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\MockSoapClient;
6
7
use InvalidArgumentException;
8
use SoapClient;
9
use SoapFault;
10
use SoapHeader;
11
12
use function count;
13
use function func_get_args;
14
use function is_array;
15
use function is_callable;
16
17
/**
18
 * Class MockSoapClient.
19
 */
20
class MockSoapClient extends SoapClient
21
{
22
    /**
23
     * @var int
24
     */
25
    private $currentIndex;
26
27
    /**
28
     * @var array<mixed>|callable
29
     */
30
    private $responses;
31
32
    /**
33
     * MockSoapClient constructor.
34
     *
35
     * @param array<mixed>|callable $responses
36
     */
37 5
    public function __construct($responses = null)
38
    {
39 5
        if (false === is_array($responses) && false === is_callable($responses)) {
40 2
            throw new InvalidArgumentException('The response argument must be an array or a callable.');
41
        }
42
43 3
        $this->responses = $responses;
44 3
        $this->currentIndex = 0;
45 3
    }
46
47
    /**
48
     * @param string $function_name
49
     * @param array<mixed> $arguments
50
     * @param array<mixed>|null $options
51
     * @param array<mixed>|SoapHeader|null $input_headers
52
     * @param array<mixed>|null $output_headers
53
     *
54
     * @throws SoapFault
55
     *
56
     * @return mixed
57
     */
58 3
    public function __soapCall(
59
        $function_name,
60
        $arguments,
61
        $options = null,
62
        $input_headers = null,
63
        &$output_headers = null
64
    ) {
65 3
        $index = $this->currentIndex++;
66
67 3
        $responses = $this->responses;
68
69 3
        if (is_callable($responses)) {
70 1
            return ($responses)(...func_get_args());
71
        }
72
73 2
        $index %= count($responses);
0 ignored issues
show
Bug introduced by
It seems like $responses can also be of type callable; however, parameter $var of count() does only seem to accept Countable|array, 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

73
        $index %= count(/** @scrutinizer ignore-type */ $responses);
Loading history...
74
75 2
        $response = $responses[$index];
76
77 2
        if ($response instanceof SoapFault) {
78 1
            throw $response;
79
        }
80
81 1
        return $response;
82
    }
83
}
84