SoapClientService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 3
1
<?php
2
3
namespace PimpayBundle\Service;
4
5
/**
6
 * Class SoapClientService
7
 * @package PimpayBundle\Service
8
 */
9
class SoapClientService extends \SoapClient
10
{
11
    /**
12
     * @var ApiService
13
     */
14
    protected $apiService;
15
16
    /**
17
     * @var resource
18
     */
19
    protected $context;
20
21
    /**
22
     * SoapClient constructor.
23
     * @param ApiService $apiService
24
     * @param mixed      $wsdl
25
     * @param array|null $options
26
     */
27
    public function __construct(ApiService $apiService, string $wsdl, array $options = null)
28
    {
29
        $this->apiService = $apiService;
30
        $this->context = stream_context_create();
31
        $options = array_merge($options, ['stream_context' => $this->context]);
32
33
        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: PimpayBundle\Service\SoapClientService. 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...
34
35
        $clientHeader = new \SoapHeader('urn:PlatformApiWsdl', 'client', 'phpSdk @ 2017-06-27 12:35:57', false);
36
        $versionHeader = new \SoapHeader('urn:PlatformApiWsdl', 'version', 'v2_6', false);
37
        $signatureHeader = new \SoapHeader('urn:PlatformApiWsdl', 'signature', null, false);
38
39
        $this->__setSoapHeaders([$clientHeader, $versionHeader, $signatureHeader]);
40
    }
41
42
    /**
43
     * @param string $request
44
     * @param string $location
45
     * @param string $action
46
     * @param int    $version
47
     * @param int    $oneWay
48
     * @return string
49
     */
50
    public function __doRequest($request, $location, $action, $version, $oneWay = 0)
51
    {
52
        $request = $this->apiService->getCryptoHandler()->injectSignature($this, $request);
53
54
        return parent::__doRequest($request, $location, $action, $version, $oneWay);
55
    }
56
57
    /**
58
     * @return resource
59
     */
60
    public function getStreamContext()
61
    {
62
        return $this->context;
63
    }
64
}
65