SoapClient   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 54
ccs 11
cts 14
cp 0.7856
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A OdeslaniTrzby() 0 4 1
A __doRequest() 0 10 2
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatEET;
4
5
use SlevomatEET\Cryptography\CryptographyService;
6
use SlevomatEET\Driver\SoapClientDriver;
7
use const SOAP_1_1;
8
use const WSDL_CACHE_DISK;
9
10
class SoapClient extends \SoapClient
11
{
12
13
	/** @var CryptographyService */
14
	private $cryptoService;
15
16
	/** @var SoapClientDriver */
17
	private $clientDriver;
18
19 1
	public function __construct(string $wsdl, CryptographyService $cryptoService, SoapClientDriver $clientDriver)
20
	{
21
		$options = [
22 1
			'soap_version' => SOAP_1_1,
23
			'encoding' => 'UTF-8',
24
			'trace' => true,
25
			'exceptions' => true,
26
			'cache_wsdl' => WSDL_CACHE_DISK,
27
		];
28 1
		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: SlevomatEET\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...
29 1
		$this->cryptoService = $cryptoService;
30 1
		$this->clientDriver = $clientDriver;
31 1
	}
32
33
	/**
34
	 * @param mixed[] $parameters
35
	 * @return mixed
36
	 */
37 1
	public function OdeslaniTrzby(array $parameters)
38
	{
39 1
		return $this->__soapCall(__FUNCTION__, ['Trzba' => $parameters]);
40
	}
41
42
	/**
43
	 * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
44
	 *
45
	 * @param string $request
46
	 * @param string $location
47
	 * @param string $action
48
	 * @param int $version
49
	 * @param int $oneWay
50
	 * @return string|null
51
	 */
52 1
	public function __doRequest($request, $location, $action, $version, $oneWay = 0): ?string
53
	{
54 1
		$signedRequest = $this->cryptoService->addWSESignature($request);
55 1
		$response = $this->clientDriver->send($signedRequest, $location, $action, $version);
56
57
		if ($oneWay === 1) {
58
			return null;
59
		}
60
		return $response;
61
	}
62
63
}
64