GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#12)
by
unknown
05:29
created

PHPClass2WSDL::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 12
ccs 7
cts 9
cp 0.7778
rs 9.2
cc 4
eloc 8
nc 3
nop 2
crap 4.1755
1
<?php
2
3
namespace PHP2WSDL;
4
5
use DOMElement;
6
use InvalidArgumentException;
7
use Wingu\OctopusCore\Reflection\ReflectionClass;
8
use Wingu\OctopusCore\Reflection\ReflectionMethod;
9
10
/**
11
 * Generate a WSDL from a PHP class.
12
 */
13
class PHPClass2WSDL
14
{
15
16
    /**
17
     * The class to transform.
18
     *
19
     * @var string
20
     */
21
    protected $class;
22
23
    /**
24
     * The URL of the web service.
25
     *
26
     * @var string
27
     */
28
    protected $uri;
29
30
    /**
31
     * The URI to the stylesheet file.
32
     *
33
     * @var string
34
     */
35
    protected $xslUri;
36
37
    /**
38
     * The WSDL document.
39
     *
40
     * @var \PHP2WSDL\WSDL
41
     */
42
    protected $wsdl;
43
44
    /**
45
     * The soap:operation style.
46
     *
47
     * @var array
48
     */
49
    protected $bindingStyle = array(
50
        'style' => 'rpc',
51
        'transport' => 'http://schemas.xmlsoap.org/soap/http'
52
    );
53
54
    /**
55
     * The soap:body operation style options.
56
     *
57
     * @var array
58
     */
59
    protected $operationBodyStyle = array(
60
        'use' => 'encoded',
61
        'encodingStyle' => 'http://schemas.xmlsoap.org/soap/encoding/'
62
    );
63
64
    /**
65
     * Constructor.
66
     *
67
     * @param mixed $class The class name from which to generate the WSDL.
68
     * @param string $uri The web service URL.
69
     * @throws InvalidArgumentException If the class is not valid or not an object.
70
     */
71 30
    public function __construct($class, $uri)
72
    {
73 30
        if (is_string($class) && class_exists($class)) {
74 30
            $this->class = $class;
75 30
        } elseif (is_object($class)) {
76
            $this->class = get_class($class);
77 3
        } else {
78
            throw new InvalidArgumentException('Invalid class name or object to generate the WSDL for.');
79
        }
80
81 30
        $this->uri = $uri;
82 30
    }
83
84
    /**
85
     * Set the stylesheet for the WSDL.
86
     *
87
     * @param string $xslUri The URI to the stylesheet.
88
     * @return PHPClass2WSDL
89
     */
90 3
    public function setStylesheet($xslUri)
91
    {
92 3
        $this->xslUri = $xslUri;
93
94 3
        return $this;
95
    }
96
97
    /**
98
     * Set the binding style.
99
     *
100
     * @param string $style The style (rpc or document).
101
     * @param string $transport The transport.
102
     * @return PHPClass2WSDL
103
     */
104 3
    public function setBindingStyle($style, $transport)
105
    {
106
        $this->bindingStyle['style'] = $style;
107
        $this->bindingStyle['transport'] = $transport;
108
109 3
        return $this;
110
    }
111
112
    /**
113
     * Generate the WSDL DOMDocument.
114
     *
115
     * @param boolean $withAnnotation Flag if only the methods with '@soap' annotation should be added.
116
     */
117 30
    public function generateWSDL($withAnnotation = false)
118
    {
119 30
        $qNameClassName = WSDL::typeToQName($this->class);
120
121 30
        $this->wsdl = new WSDL($qNameClassName, $this->uri, $this->xslUri);
122
123 30
        $port = $this->wsdl->addPortType($qNameClassName . 'Port');
124 30
        $binding = $this->wsdl->addBinding($qNameClassName . 'Binding', 'tns:' . $qNameClassName . 'Port');
125
126 30
        $this->wsdl->addSoapBinding($binding, $this->bindingStyle['style'], $this->bindingStyle['transport']);
127 30
        $this->wsdl->addService(
128 30
            $qNameClassName . 'Service', $qNameClassName . 'Port',
129 30
            'tns:' . $qNameClassName . 'Binding',
130 30
            $this->uri
131 30
        );
132
133 30
        $ref = new ReflectionClass($this->class);
134 30
        foreach ($ref->getMethods() as $method) {
135 27
            if ($withAnnotation === false || $method->getReflectionDocComment()->getAnnotationsCollection()->hasAnnotationTag('soap')) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class ReflectionMethod as the method getReflectionDocComment() does only exist in the following sub-classes of ReflectionMethod: Wingu\OctopusCore\Reflection\ReflectionMethod. 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...
136 27
                $this->addMethodToWsdl($method, $port, $binding);
0 ignored issues
show
Compatibility introduced by
$method of type object<ReflectionMethod> is not a sub-type of object<Wingu\OctopusCore...ction\ReflectionMethod>. It seems like you assume a child class of the class ReflectionMethod to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
137 27
            }
138 30
        }
139 30
    }
140
141
    /**
142
     * Add a method to the WSDL.
143
     *
144
     * @param ReflectionMethod $method The reflection of the method to add.
145
     * @param DOMElement $port The portType element.
146
     * @param DOMElement $binding The binding element.
147
     */
148 27
    protected function addMethodToWsdl(ReflectionMethod $method, DOMElement $port, DOMElement $binding)
149
    {
150 27
        $qNameMethodName = WSDL::typeToQName($method->getName());
151
152 27
        $args = array();
153 27
        $annotations = array();
154 27
        $methodAnnotationsCollection = $method->getReflectionDocComment()->getAnnotationsCollection();
155 27
        if ($methodAnnotationsCollection->hasAnnotationTag('param')) {
156
            /** @var \Wingu\OctopusCore\Reflection\Annotation\Tags\ParamTag $param */
157 27
            foreach ($methodAnnotationsCollection->getAnnotation('param') as $param) {
158 27
                $annotations[$param->getParamName()] = $param;
159 27
            }
160 27
        }
161
162 27
        if ($this->bindingStyle['style'] === 'document') {
163
            $sequence = array();
164
            /** @var \Wingu\OctopusCore\Reflection\ReflectionParameter $param */
165
            foreach ($method->getParameters() as $param) {
166
                $type = 'anytype';
167
                if (isset($annotations['$' . $param->getName()])) {
168
                    $type = $annotations['$' . $param->getName()]->getParamType();
169
                }
170
171
                $sequenceElement = array('name' => $param->getName(), 'type' => $this->wsdl->getXSDType($type));
172
                if ($param->isOptional()) {
173
                    $sequenceElement['nillable'] = 'true';
174
                }
175
176
                $sequence[] = $sequenceElement;
177
            }
178
179
            $element = array('name' => $qNameMethodName, 'sequence' => $sequence);
180
            $args['parameters'] = array('element' => $this->wsdl->addElement($element));
181
        } else {
182
            /** @var \Wingu\OctopusCore\Reflection\ReflectionParameter $param */
183 27
            foreach ($method->getParameters() as $param) {
184 27
                $type = 'anytype';
185 27
                if (isset($annotations['$' . $param->getName()])) {
186 27
                    $type = $annotations['$' . $param->getName()]->getParamType();
187 27
                }
188
189 27
                $args[$param->getName()] = array('type' => $this->wsdl->getXSDType($type));
190 27
            }
191
        }
192
193 27
        $this->wsdl->addMessage($qNameMethodName . 'In', $args);
194
195 27
        $returnType = null;
196 27
        if ($methodAnnotationsCollection->hasAnnotationTag('return') === true) {
197 9
            $annotation = $methodAnnotationsCollection->getAnnotation('return');
198 9
            $annotation = reset($annotation);
199 9
            $returnType = $annotation->getReturnType();
200 9
        }
201
202 27
        $isOneWayMessage = ($returnType === null);
203
204 27
        if ($isOneWayMessage === false) {
205 9
            $args = array();
206 9
            if ($this->bindingStyle['style'] === 'document') {
207
                $sequence = array();
208
                if ($returnType !== null) {
209
                    $sequence[] = array(
210
                        'name' => $qNameMethodName . 'Result',
211
                        'type' => $this->wsdl->getXSDType($returnType)
212
                    );
213
                }
214
215
                $element = array('name' => $qNameMethodName . 'Response', 'sequence' => $sequence);
216
                $args['parameters'] = array('element' => $this->wsdl->addElement($element));
217 9
            } elseif ($returnType !== null) {
218 9
                $args['return'] = array('type' => $this->wsdl->getXSDType($returnType));
219 9
            }
220
221 9
            $this->wsdl->addMessage($qNameMethodName . 'Out', $args);
222 9
        }
223
224
        // Add the portType operation.
225 27
        if ($isOneWayMessage === false) {
226 9
            $portOperation = $this->wsdl->addPortOperation(
227 9
                $port,
228 9
                $qNameMethodName,
229 9
                'tns:' . $qNameMethodName . 'In',
230 9
                'tns:' . $qNameMethodName . 'Out'
231 9
            );
232 9
        } else {
233 21
            $portOperation = $this->wsdl->addPortOperation($port, $qNameMethodName, 'tns:' . $qNameMethodName . 'In');
234
        }
235
236
        // Add any documentation for the operation.
237 27
        $description = $method->getReflectionDocComment()->getFullDescription();
238 27
        if (strlen($description) > 0) {
239 9
            $this->wsdl->addDocumentation($portOperation, $description);
240 9
        }
241
242
        // When using the RPC style, make sure the operation style includes a 'namespace' attribute (WS-I Basic Profile 1.1 R2717).
243 27
        if ($this->bindingStyle['style'] === 'rpc' && isset($this->operationBodyStyle['namespace']) === false) {
244 27
            $this->operationBodyStyle['namespace'] = '' . htmlspecialchars($this->uri);
245 27
        }
246
247
        // Add the binding operation.
248 27
        $operation = $this->wsdl->addBindingOperation(
249 27
            $binding,
250 27
            $qNameMethodName,
251 27
            $this->operationBodyStyle,
252 27
            $this->operationBodyStyle
253 27
        );
254 27
        $this->wsdl->addSoapOperation($operation, $this->uri . '#' . $qNameMethodName);
255 27
    }
256
257
    /**
258
     * Dump the WSDL as XML string.
259
     *
260
     * @return string
261
     */
262 30
    public function dump()
263
    {
264 30
        return $this->wsdl->dump();
265
    }
266
}
267