Passed
Push — master ( f2650d...9a436a )
by Radosław
01:54
created

DebugSoapClient::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Radowoj\Yaah;
4
5
use SoapClient;
6
7
/**
8
 * When things go south on WebAPI Sandbox, it is good to be able to send XML of request/response to Allegro support.
9
 * This class helps with that. Just provide some path or php://stdout as $options['log_file'] and you're good to go.
10
 */
11
class DebugSoapClient extends SoapClient
12
{
13
    /**
14
     * Log file (or stream) to use for logging SOAP requests/responses
15
     * @var resource
16
     */
17
    protected $logFile = null;
18
19
    public function __construct($wsdl, array $options = [])
20
    {
21
        if (!array_key_exists('log_file', $options)) {
22
            throw new Exception("Debug soap client requires a log file path provided as \$options['log_file'] to work");
23
        }
24
25
        $this->logFile = fopen($options['log_file'], "a");
26
27
        $options['trace'] = 1;
28
29
        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: Radowoj\Yaah\DebugSoapClient. 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...
30
    }
31
32
33
    public function __destruct()
34
    {
35
        fclose($this->logFile);
36
    }
37
38
39
    public function __call($functionName, $arguments)
40
    {
41
        $response = parent::__call($functionName, $arguments);
42
43
        $this->log("Date: " . date('Y-m-d H:i:s'));
44
        $this->log("Request {$functionName}: \n{$this->__getLastRequest()}\n");
45
        $this->log("Response {$functionName}: \n{$this->__getLastResponse()}\n");
46
47
        return $response;
48
    }
49
50
51
    protected function log($message)
52
    {
53
        fwrite($this->logFile, $message);
54
    }
55
}
56