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
Push — master ( 2dd713...4dc7a4 )
by Dragos
15:08
created

PHPClass2WSDL::setStylesheet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 2
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 24
    /**
65
     * Constructor.
66 24
     *
67 24
     * @param mixed $class The class name from which to generate the WSDL.
68 24
     * @param string $uri The web service URL.
69
     * @throws InvalidArgumentException If the class is not valid or not an object.
70
     */
71
    public function __construct($class, $uri)
72
    {
73
        if (is_string($class) && class_exists($class)) {
74 24
            $this->class = $class;
75 24
        } elseif (is_object($class)) {
76
            $this->class = get_class($class);
77
        } else {
78
            throw new InvalidArgumentException('Invalid class name or object to generate the WSDL for.');
79
        }
80
81
        $this->uri = $uri;
82
    }
83
84
    /**
85
     * Set the stylesheet for the WSDL.
86
     *
87
     * @param string $xslUri The URI to the stylesheet.
88
     * @return PHPClass2WSDL
89
     */
90
    public function setStylesheet($xslUri)
91
    {
92
        $this->xslUri = $xslUri;
93
94
        return $this;
95
    }
96
97 24
    /**
98
     * Set the binding style.
99 24
     *
100
     * @param string $style The style (rpc or document).
101 24
     * @param string $transport The transport.
102
     * @return PHPClass2WSDL
103 24
     */
104 24
    public function setBindingStyle($style, $transport)
105
    {
106 24
        $this->bindingStyle['style'] = $style;
107 24
        $this->bindingStyle['transport'] = $transport;
108 24
109 24
        return $this;
110 24
    }
111 24
112
    /**
113 24
     * Generate the WSDL DOMDocument.
114 24
     *
115 24
     * @param boolean $withAnnotation Flag if only the methods with '@soap' annotation should be added.
116 24
     */
117 24
    public function generateWSDL($withAnnotation = false)
118 24
    {
119 24
        $qNameClassName = WSDL::typeToQName($this->class);
120
121
        $this->wsdl = new WSDL($qNameClassName, $this->uri, $this->xslUri);
122
123
        $port = $this->wsdl->addPortType($qNameClassName . 'Port');
124
        $binding = $this->wsdl->addBinding($qNameClassName . 'Binding', 'tns:' . $qNameClassName . 'Port');
125
126
        $this->wsdl->addSoapBinding($binding, $this->bindingStyle['style'], $this->bindingStyle['transport']);
127
        $this->wsdl->addService(
128 24
            $qNameClassName . 'Service', $qNameClassName . 'Port',
129
            'tns:' . $qNameClassName . 'Binding',
130 24
            $this->uri
131
        );
132 24
133 24
        $ref = new ReflectionClass($this->class);
134 24
        foreach ($ref->getMethods() as $method) {
135 24
            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
                $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 24
            }
138 24
        }
139 24
    }
140 24
141
    /**
142 24
     * 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
    protected function addMethodToWsdl(ReflectionMethod $method, DOMElement $port, DOMElement $binding)
149
    {
150
        $qNameMethodName = WSDL::typeToQName($method->getName());
151
152
        $args = array();
153
        $annotations = array();
154
        $methodAnnotationsCollection = $method->getReflectionDocComment()->getAnnotationsCollection();
155
        if ($methodAnnotationsCollection->hasAnnotationTag('param')) {
156
            /** @var \Wingu\OctopusCore\Reflection\Annotation\Tags\ParamTag $param */
157
            foreach ($methodAnnotationsCollection->getAnnotation('param') as $param) {
158
                $annotations[$param->getParamName()] = $param;
159
            }
160
        }
161
162
        if ($this->bindingStyle['style'] === 'document') {
163 24
            $sequence = array();
164 24
            /** @var \Wingu\OctopusCore\Reflection\ReflectionParameter $param */
165 24
            foreach ($method->getParameters() as $param) {
166 24
                $type = 'anytype';
167 24
                if (isset($annotations['$' . $param->getName()])) {
168
                    $type = $annotations['$' . $param->getName()]->getParamType();
169 24
                }
170 24
171
                $sequenceElement = array('name' => $param->getName(), 'type' => $this->wsdl->getXSDType($type));
172
                if ($param->isOptional()) {
173 24
                    $sequenceElement['nillable'] = 'true';
174
                }
175 24
176 24
                $sequence[] = $sequenceElement;
177 9
            }
178 9
179 9
            $element = array('name' => $qNameMethodName, 'sequence' => $sequence);
180 9
            $args['parameters'] = array('element' => $this->wsdl->addElement($element));
181
        } else {
182 24
            /** @var \Wingu\OctopusCore\Reflection\ReflectionParameter $param */
183
            foreach ($method->getParameters() as $param) {
184 24
                $type = 'anytype';
185 9
                if (isset($annotations['$' . $param->getName()])) {
186 9
                    $type = $annotations['$' . $param->getName()]->getParamType();
187
                }
188
189
                $args[$param->getName()] = array('type' => $this->wsdl->getXSDType($type));
190
            }
191
        }
192
193
        $this->wsdl->addMessage($qNameMethodName . 'In', $args);
194
195
        $returnType = null;
196
        if ($methodAnnotationsCollection->hasAnnotationTag('return') === true) {
197 9
            $annotation = $methodAnnotationsCollection->getAnnotation('return');
198 9
            $annotation = reset($annotation);
199 9
            $returnType = $annotation->getReturnType();
200
        }
201 9
202 9
        $isOneWayMessage = ($returnType === null);
203
204
        if ($isOneWayMessage === false) {
205 24
            $args = array();
206 9
            if ($this->bindingStyle['style'] === 'document') {
207 9
                $sequence = array();
208 9
                if ($returnType !== null) {
209 9
                    $sequence[] = array(
210 9
                        'name' => $qNameMethodName . 'Result',
211 9
                        'type' => $this->wsdl->getXSDType($returnType)
212 9
                    );
213 18
                }
214
215
                $element = array('name' => $qNameMethodName . 'Response', 'sequence' => $sequence);
216
                $args['parameters'] = array('element' => $this->wsdl->addElement($element));
217 24
            } elseif ($returnType !== null) {
218 24
                $args['return'] = array('type' => $this->wsdl->getXSDType($returnType));
219 9
            }
220 9
221
            $this->wsdl->addMessage($qNameMethodName . 'Out', $args);
222
        }
223 24
224 24
        // Add the portType operation.
225 24
        if ($isOneWayMessage === false) {
226
            $portOperation = $this->wsdl->addPortOperation(
227
                $port,
228 24
                $qNameMethodName,
229 24
                'tns:' . $qNameMethodName . 'In',
230 24
                'tns:' . $qNameMethodName . 'Out'
231 24
            );
232 24
        } else {
233 24
            $portOperation = $this->wsdl->addPortOperation($port, $qNameMethodName, 'tns:' . $qNameMethodName . 'In');
234 24
        }
235 24
236
        // Add any documentation for the operation.
237
        $description = $method->getReflectionDocComment()->getFullDescription();
238
        if (strlen($description) > 0) {
239
            $this->wsdl->addDocumentation($portOperation, $description);
240
        }
241
242 24
        // When using the RPC style, make sure the operation style includes a 'namespace' attribute (WS-I Basic Profile 1.1 R2717).
243
        if ($this->bindingStyle['style'] === 'rpc' && isset($this->operationBodyStyle['namespace']) === false) {
244 24
            $this->operationBodyStyle['namespace'] = '' . htmlspecialchars($this->uri);
245
        }
246
247
        // Add the binding operation.
248
        $operation = $this->wsdl->addBindingOperation(
249
            $binding,
250
            $qNameMethodName,
251
            $this->operationBodyStyle,
252
            $this->operationBodyStyle
253
        );
254
        $this->wsdl->addSoapOperation($operation, $this->uri . '#' . $qNameMethodName);
255
    }
256
257
    /**
258
     * Dump the WSDL as XML string.
259
     *
260
     * @return string
261
     */
262
    public function dump()
263
    {
264
        return $this->wsdl->dump();
265
    }
266
}
267