Completed
Push — master ( fa5ac7...eb2a5f )
by Jan
06:08
created

SoapClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 3
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatEET;
4
5
use SlevomatEET\Cryptography\CryptographyService;
6
use SlevomatEET\Driver\SoapClientDriver;
7
8
class SoapClient extends \SoapClient
9
{
10
11
	/** @var \SlevomatEET\Cryptography\CryptographyService */
12
	private $cryptoService;
13
14
	/** @var \SlevomatEET\Driver\SoapClientDriver */
15
	private $clientDriver;
16
17 1
	public function __construct(string $wsdl, CryptographyService $cryptoService, SoapClientDriver $clientDriver)
18
	{
19
		$options = [
20 1
			'soap_version' => SOAP_1_1,
21 1
			'encoding' => 'UTF-8',
22
			'trace' => true,
23
			'exceptions' => true,
24 1
			'cache_wsdl' => WSDL_CACHE_DISK,
25
		];
26 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...
27 1
		$this->cryptoService = $cryptoService;
28 1
		$this->clientDriver = $clientDriver;
29 1
	}
30
31
	/**
32
	 * @param array $parameters
33
	 * @return mixed
34
	 */
35 1
	public function OdeslaniTrzby(array $parameters)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
36
	{
37 1
		return $this->__soapCall(__FUNCTION__, ['Trzba' => $parameters]);
38
	}
39
40
	/**
41
	 * @param string $request
42
	 * @param string $location
43
	 * @param string $action
44
	 * @param int $version
45
	 * @param int $oneWay
46
	 * @return string|null
47
	 */
48 1
	public function __doRequest($request, $location, $action, $version, $oneWay = 0)
49
	{
50 1
		$signedRequest = $this->cryptoService->addWSESignature($request);
51 1
		$response = $this->clientDriver->send($signedRequest, $location, $action, $version);
52
53
		if ($oneWay === 1) {
54
			return null;
55
		}
56
		return $response;
57
	}
58
59
}
60