SoapClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Afonso\Soapi;
4
5
use League\Pipeline\Pipeline;
6
use SoapClient as NativeSoapClient;
7
8
/**
9
 * A enhanced, drop-in replacement for PHP's native SoapClient class.
10
 *
11
 * This client includes input and output pipelines to aribtrarily modify
12
 * requests and responses.
13
 */
14
class SoapClient extends NativeSoapClient
15
{
16
    use ProcessesWithPipelines;
17
18
    /**
19
     * Creates and returns a new SoapClient with empty inbound and outbound
20
     * pipelines.
21
     *
22
     * @param   string|null $wsdl
23
     * @param   array       $options
24
     */
25
    public function __construct($wsdl, array $options = array())
26
    {
27
        parent::__construct($wsdl, $options);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SoapClient as the method __construct() does only exist in the following sub-classes of SoapClient: Afonso\Soapi\SoapClient. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
28
29
        // initialize in and outbound pipelines
30
        $this->inboundPipeline = new Pipeline();
31
        $this->outboundPipeline = new Pipeline();
32
    }
33
34
    /**
35
     * Performs a SOAP request.
36
     *
37
     * This function delegates on PHP's native SoapClient __doRequest(), but
38
     * processes the payload through the oubtound and inbound pipelines. This
39
     * allows for arbitrary manipulation of requests and responses, such as
40
     * encryption or logging.
41
     *
42
     * @see     http://php.net/manual/en/soapclient.dorequest.php
43
     * @param   string  $soapRequest
44
     * @param   string  $location
45
     * @param   string  $action
46
     * @param   int     $version
47
     * @param   int     $oneWay
48
     */
49
    public function __doRequest($soapRequest, $location, $action, $version, $oneWay = 0)
50
    {
51
        /*
52
         * Run the request XML through the outbound pipeline.
53
         */
54
        $soapRequest = $this->outboundPipeline->process($soapRequest);
55
56
        /*
57
         * Do the actual request.
58
         */
59
        $soapResponse = parent::__doRequest($soapRequest, $location, $action, $version, $oneWay);
60
61
        /*
62
         * Then run the response through the inbound pipeline.
63
         */
64
        $soapResponse = $this->inboundPipeline->process($soapResponse);
65
        return $soapResponse;
66
    }
67
}
68