Completed
Push — master ( 1d61ab...ab9b44 )
by Meng
02:44
created

Soap::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 1
eloc 18
nc 1
nop 2
1
<?php
2
3
namespace Meng\Soap;
4
5
/**
6
 * @internal
7
 */
8
class Soap extends \SoapClient
9
{
10
    private $endpoint;
11
    private $soapRequest;
12
    private $soapResponse;
13
    private $soapAction;
14
    private $soapVersion;
15
16
    public function __construct($wsdl, array $options)
17
    {
18
        unset($options['login']);
19
        unset($options['password']);
20
        unset($options['proxy_host']);
21
        unset($options['proxy_port']);
22
        unset($options['proxy_login']);
23
        unset($options['proxy_password']);
24
        unset($options['local_cert']);
25
        unset($options['passphrase']);
26
        unset($options['authentication']);
27
        unset($options['compression']);
28
        unset($options['trace']);
29
        unset($options['connection_timeout']);
30
        unset($options['user_agent']);
31
        unset($options['stream_context']);
32
        unset($options['keep_alive']);
33
        unset($options['ssl_method']);
34
        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: Meng\Soap\Soap. 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...
35
    }
36
37
    public function __doRequest($request, $location, $action, $version, $one_way = 0)
38
    {
39
        if (null !== $this->soapResponse) {
40
            return $this->soapResponse;
41
        }
42
43
        $this->endpoint = (string)$location;
44
        $this->soapAction = (string)$action;
45
        $this->soapVersion = (string)$version;
46
        $this->soapRequest = (string)$request;
47
        return '';
48
    }
49
50
    public function request($function_name, $arguments, $options, $input_headers)
51
    {
52
        $this->__soapCall($function_name, $arguments, $options, $input_headers);
53
        return new SoapRequest($this->endpoint, $this->soapAction, $this->soapVersion, $this->soapRequest);
54
    }
55
56
    public function response($response, $function_name, &$output_headers)
57
    {
58
        $this->soapResponse = $response;
59
        try {
60
            $response = $this->__soapCall($function_name, [], null, null, $output_headers);
61
        } catch (\SoapFault $fault) {
62
            $this->soapResponse = null;
63
            throw $fault;
64
        }
65
        $this->soapResponse = null;
66
        return $response;
67
    }
68
}
69