Passed
Push — master ( 463105...84cd59 )
by Roberto
04:47
created

SoapNative   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 116
ccs 0
cts 81
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B send() 0 32 4
B prepare() 0 28 4
A setNativeProxy() 0 17 3
1
<?php
2
3
namespace NFePHP\Common\Soap;
4
5
/**
6
 * SoapClient based in native PHP SoapClient class
7
 *
8
 * @category  NFePHP
9
 * @package   NFePHP\Common\Soap\SoapNative
10
 * @copyright NFePHP Copyright (c) 2016
11
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
12
 * @license   https://opensource.org/licenses/MIT MIT
13
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
14
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
15
 * @link      http://github.com/nfephp-org/sped-common for the canonical source repository
16
 */
17
18
use NFePHP\Common\Soap\SoapClientExtended;
19
use NFePHP\Common\Soap\SoapBase;
20
use NFePHP\Common\Soap\SoapInterface;
21
use NFePHP\Common\Exception\SoapException;
22
use NFePHP\Common\Certificate;
23
use Psr\Log\LoggerInterface;
24
25
class SoapNative extends SoapBase implements SoapInterface
26
{
27
    /**
28
     * Constructor
29
     * @param Certificate $certificate
30
     * @param LoggerInterface $logger
31
     */
32
    public function __construct(Certificate $certificate = null, LoggerInterface $logger = null)
33
    {
34
        parent::__construct($certificate, $logger);
35
    }
36
37
    /**
38
     * Send soap message to url
39
     * @param string $url
40
     * @param string $operation
41
     * @param string $action
42
     * @param int $soapver
43
     * @param array $parameters
44
     * @param array $namespaces
45
     * @param string $request
46
     * @param \SOAPHeader $soapheader
47
     * @return string
48
     * @throws \NFePHP\Common\Exception\SoapException
49
     */
50
    public function send(
51
        $url,
52
        $operation = '',
53
        $action = '',
54
        $soapver = SOAP_1_2,
55
        $parameters = [],
56
        $namespaces = [],
57
        $request = '',
58
        $soapheader = null
59
    ) {
60
        $this->prepare($url, $soapver);
61
        try {
62
            if (!empty($soapheader)) {
63
                $this->connection->__setSoapHeaders(array($soapheader));
64
            }
65
            $response = $this->connection->$operation($parameters);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
            $this->requestHead = $this->connection->__getLastRequestHeaders();
67
            $this->requestBody = $this->connection->__getLastRequest();
68
            $this->responseHead = $this->connection->__getLastResponseHeaders();
69
            $this->responseBody = $this->connection->__getLastResponse();
70
            $this->saveDebugFiles(
71
                $operation,
72
                $this->requestHead . "\n" . $this->requestBody,
73
                $this->responseHead . "\n" . $this->responseBody
74
            );
75
        } catch (SoapFault $e) {
0 ignored issues
show
Bug introduced by
The class NFePHP\Common\Soap\SoapFault does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
76
            throw SoapException::soapFault($e->getMessage());
77
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class NFePHP\Common\Soap\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
78
            throw SoapException::soapFault($e->getMessage());
79
        }
80
        return $this->responseBody;
81
    }
82
    
83
    /**
84
     * Prepare connection
85
     * @param string $url
86
     * @param int $soapver
87
     * @throws RuntimeException
88
     * @throws \NFePHP\Common\Exception\SoapException
89
     */
90
    protected function prepare($url, $soapver = SOAP_1_2)
91
    {
92
        $wsdl = "$url?WSDL";
93
        $verifypeer = true;
94
        if ($this->disablesec) {
95
            $verifypeer = false;
96
            $verifyhost = false;
97
        }
98
        $params = [
99
            'local_cert' => $this->tempdir . $this->certfile,
100
            'passphrase' => '',
101
            'connection_timeout' => $this->soaptimeout,
102
            'encoding' => 'UTF-8',
103
            'verifypeer' => $verifypeer,
104
            'verifyhost' => $verifyhost,
0 ignored issues
show
Bug introduced by
The variable $verifyhost does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
105
            'soap_version' => $soapver,
106
            'trace' => true,
107
            'cache_wsdl' => WSDL_CACHE_NONE
108
        ];
109
        $this->setNativeProxy($params);
110
        try {
111
            $this->connection = new SoapClientExtended($wsdl, $params);
112
        } catch (SoapFault $e) {
0 ignored issues
show
Bug introduced by
The class NFePHP\Common\Soap\SoapFault does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
113
            throw SoapException::soapFault($e->getMessage());
114
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class NFePHP\Common\Soap\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
115
            throw SoapException::soapFault($e->getMessage());
116
        }
117
    }
118
    
119
    /**
120
     * Set parameters for proxy
121
     * @param array $params
122
     */
123
    private function setNativeProxy(&$params)
124
    {
125
        if ($this->proxyIP != '') {
126
            $pproxy1 = [
127
                'proxy_host' => $this->proxyIP,
128
                'proxy_port' => $this->proxyPort
129
            ];
130
            array_push($params, $pproxy1);
131
        }
132
        if ($this->proxyUser != '') {
133
            $pproxy2 = [
134
                'proxy_login' => $this->proxyUser,
135
                'proxy_password' => $this->proxyPass
136
            ];
137
            array_push($params, $pproxy2);
138
        }
139
    }
140
}
141