Test Failed
Pull Request — master (#344)
by Andrew
09:56
created

SoapClient::transformIncomingRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3.0924

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 18
cts 23
cp 0.7826
rs 9.312
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0924
1
<?php
2
/**
3
 * amadeus-ws-client
4
 *
5
 * Copyright 2015 Amadeus Benelux NV
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 * http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 *
19
 * @package Amadeus
20
 * @license https://opensource.org/licenses/Apache-2.0 Apache 2.0
21
 */
22
23
namespace Amadeus\Client;
24
25
use Psr\Log;
26
27
/**
28
 * Amadeus Web Services SoapClient class
29
 *
30
 * This SoapClient will perform an XSLT transformation on the request to be sent to the amadeus server:
31
 * it will remove any empty XML tags except for some specific tags that may be required to be present and empty.
32
 *
33
 * If you do not remove empty XML tags, you will get SoapFaults back from the server when unexpected empty elements
34
 * are present in your request.
35
 *
36
 * @package Amadeus\Client
37
 * @author Dieter Devlieghere <[email protected]>
38
 */
39
class SoapClient extends \SoapClient implements Log\LoggerAwareInterface
40
{
41
    use Log\LoggerAwareTrait;
42
43
    const REMOVE_EMPTY_XSLT_LOCATION = 'SoapClient/removeempty.xslt';
44
45
    /**
46
     * Construct a new SoapClient
47
     *
48
     * @param string $wsdl Location of WSDL file
49
     * @param array $options initialisation options
50
     * @param Log\LoggerInterface|null $logger Error logging object
51
     */
52 15
    public function __construct($wsdl, $options, Log\LoggerInterface $logger = null)
53
    {
54 15
        if (!($logger instanceof Log\LoggerInterface)) {
55 9
            $logger = new Log\NullLogger();
56
        }
57 15
        $this->setLogger($logger);
58
59 15
        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: Amadeus\Client\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...
60 15
    }
61
62
    /**
63
     * __doRequest override of SoapClient
64
     *
65
     * @param string $request The XML SOAP request.
66
     * @param string $location The URL to request.
67
     * @param string $action The SOAP action.
68
     * @param int $version The SOAP version.
69
     * @param int|null $oneWay
70
     * @uses parent::__doRequest
71
     * @return string The XML SOAP response.
72
     * @throws Exception When PHP XSL extension is not enabled or WSDL file isn't readable.
73
     */
74
    public function __doRequest($request, $location, $action, $version, $oneWay = null)
75
    {
76
        if (!extension_loaded('xsl')) {
77
            throw new Exception('PHP XSL extension is not enabled.');
78
        }
79
80
        $newRequest = $this->transformIncomingRequest($request);
81
82
        return parent::__doRequest($newRequest, $location, $action, $version, $oneWay);
83
    }
84
85
    /**
86
     * @param string $request
87
     * @return string
88
     * @throws Exception when XSLT file isn't readable
89
     */
90 9
    protected function transformIncomingRequest($request)
91
    {
92 9
        $newRequest = null;
0 ignored issues
show
Unused Code introduced by
$newRequest 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...
93
94 9
        $xsltFile = dirname(__FILE__).DIRECTORY_SEPARATOR.self::REMOVE_EMPTY_XSLT_LOCATION;
95 9
        if (!is_readable($xsltFile)) {
96
            throw new Exception('XSLT file "'.$xsltFile.'" is not readable!');
97
        }
98
99 9
        $dom = new \DOMDocument('1.0', 'UTF-8');
100 9
        $dom->loadXML($request);
101 9
        $xslt = new \DOMDocument('1.0', 'UTF-8');
102
103 9
        $xslt->load($xsltFile);
104
105 9
        $processor = new \XSLTProcessor();
106 9
        $processor->importStylesheet($xslt);
107 9
        $transform = $processor->transformToXml($dom);
108 9
        if ($transform === false) {
109
            //On transform error: usually when modifying the XSLT transformation incorrectly...
110
            $this->logger->log(
111
                Log\LogLevel::ERROR,
112
                __METHOD__."__doRequest(): XSLTProcessor::transformToXml "
113
                . "returned FALSE: could not perform transformation!!"
114
            );
115
            $newRequest = $request;
116
        } else {
117 9
            $newDom = new \DOMDocument('1.0', 'UTF-8');
118 9
            $newDom->preserveWhiteSpace = false;
119 9
            $newDom->loadXML($transform);
120
121 9
            $newRequest = $newDom->saveXML();
122
        }
123
124 9
        unset($processor, $xslt, $dom, $transform);
125
126 9
        return $newRequest;
127
    }
128
}
129