ClientStubGenerator::getOperationParams()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.1215

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 17
cts 20
cp 0.85
rs 8.8337
c 0
b 0
f 0
cc 6
nc 4
nop 1
crap 6.1215
1
<?php
2
3
namespace GoetasWebservices\SoapServices\SoapClient\StubGeneration;
4
5
use Doctrine\Common\Inflector\Inflector;
6
use GoetasWebservices\SoapServices\SoapClient\StubGeneration\Tag\MethodTag;
7
use GoetasWebservices\SoapServices\SoapClient\StubGeneration\Tag\ParamTag;
8
use GoetasWebservices\XML\WSDLReader\Wsdl\Message\Part;
9
use GoetasWebservices\XML\WSDLReader\Wsdl\PortType;
10
use GoetasWebservices\Xsd\XsdToPhp\Naming\NamingStrategy;
11
use GoetasWebservices\Xsd\XsdToPhp\Php\PhpConverter;
12
use Zend\Code\Generator\ClassGenerator;
13
use Zend\Code\Generator\DocBlockGenerator;
14
15
class ClientStubGenerator
16
{
17
    protected $reservedWords = [
18
        'int',
19
        'float',
20
        'bool',
21
        'string',
22
        'true',
23
        'false',
24
        'null',
25
        'resource',
26
        'object',
27
        'mixed',
28
        'numeric',
29
    ];
30
31
    private $baseNs = [
32
        'headers' => '\\SoapEnvelope\\Headers',
33
        'parts' => '\\SoapEnvelope\\Parts',
34
        'messages' => '\\SoapEnvelope\\Messages',
35
    ];
36
    /**
37
     * @var NamingStrategy
38
     */
39
    private $namingStrategy;
40
    /**
41
     * @var PhpConverter
42
     */
43
    private $phpConverter;
44
    /**
45
     * @var bool
46
     */
47
    private $unwrapReturn = false;
48
49 1
    public function __construct(PhpConverter $phpConverter, NamingStrategy $namingStrategy, $unwrapReturn = false, array $baseNs = array())
50
    {
51 1
        foreach ($baseNs as $k => $ns) {
52
            if (isset($this->baseNs[$k])) {
53
                $this->baseNs[$k] = $ns;
54
            }
55 1
        }
56 1
        $this->baseNs = $baseNs;
57 1
        $this->namingStrategy = $namingStrategy;
58 1
        $this->phpConverter = $phpConverter;
59 1
        $this->unwrapReturn = $unwrapReturn;
60 1
    }
61
62 1
    public function setUnwrap($mode = true)
63
    {
64 1
        $this->unwrapReturn = (bool)$mode;
65 1
    }
66
67
    /**
68
     * @param PortType[] $ports
69
     * @return ClassGenerator
70
     */
71 1
    public function generate(array $ports)
72
    {
73 1
        $classes = array();
74 1
        foreach ($ports as $port) {
75 1
            $class = new ClassGenerator();
76 1
            if ($this->visitPortType($class, $port) !== false) {
77 1
                $classes[] = $class;
78 1
            }
79 1
        }
80 1
        return $classes;
81
    }
82
83 1
    private function visitPortType(ClassGenerator $class, PortType $portType)
84
    {
85 1
        if (!count($portType->getOperations())) {
86
            return false;
87
        }
88 1
        $docBlock = new DocBlockGenerator("Class representing " . $portType->getName());
89 1
        $docBlock->setWordWrap(false);
90 1
        if ($portType->getDocumentation()) {
91
            $docBlock->setLongDescription($portType->getDocumentation());
92
        }
93
94 1
        $namespaces = $this->phpConverter->getNamespaces();
95 1
        $class->setNamespaceName($namespaces[$portType->getDefinition()->getTargetNamespace()] . "\\SoapStubs");
96 1
        $class->setName(Inflector::classify($portType->getName()));
97 1
        $class->setDocblock($docBlock);
98
99 1
        foreach ($portType->getOperations() as $operation) {
100 1
            $operationTag = $this->visitOperation($operation);
101 1
            $docBlock->setTag($operationTag);
102 1
        }
103 1
    }
104
105 1
    private function visitOperation(PortType\Operation $operation)
106
    {
107 1
        $types = $this->getOperationReturnTypes($operation);
108 1
        $operationTag = new MethodTag(
109 1
            Inflector::camelize($operation->getName()),
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Inflector\Inflector::camelize() has been deprecated.

This method has been deprecated.

Loading history...
110 1
            $types,
111 1
            preg_replace("/[\n\r]+/", " ", $operation->getDocumentation())
112 1
        );
113 1
        $params = $this->getOperationParams($operation);
114 1
        $operationTag->setParams($params);
115 1
        return $operationTag;
116
    }
117
118 1
    private function getOperationParams(PortType\Operation $operation)
119
    {
120 1
        if (!$operation->getInput()) {
121
            return [];
122
        }
123
124 1
        $parts = $operation->getInput()->getMessage()->getParts();
0 ignored issues
show
Bug introduced by
The method getParts cannot be called on $operation->getInput()->getMessage() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
125 1
        if (!$parts) {
126 1
            return [];
127
        }
128
129 1
        if (count($parts) > 1) {
130 1
            $params = [];
131 1
            foreach ($parts as $part) {
132 1
                $partName = $this->namingStrategy->getPropertyName($part);
133 1
                $class = $this->getClassFromPart($part);
134
135 1
                $typeName = $class->getPhpType();
136 1
                if ($t = $class->isSimpleType()) {
137
                    $typeName = $t->getType()->getPhpType();
138
                }
139 1
                $params[] = $param = new ParamTag($partName, [$typeName]);
0 ignored issues
show
Unused Code introduced by
$param 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...
140 1
            }
141 1
        } else {
142 1
            $params = $this->extractSinglePartParameters(reset($parts));
143
        }
144
145 1
        return $params;
146
    }
147
148 1
    private function getOperationReturnTypes(PortType\Operation $operation)
149
    {
150 1
        if (!$operation->getOutput() || !$operation->getOutput()->getMessage()->getParts()) {
0 ignored issues
show
Bug introduced by
The method getParts cannot be called on $operation->getOutput()->getMessage() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
151 1
            return ['void'];
152
        }
153 1
        $parts = $operation->getOutput()->getMessage()->getParts();
0 ignored issues
show
Bug introduced by
The method getParts cannot be called on $operation->getOutput()->getMessage() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
154 1
        if (count($parts) > 1) {
155 1
            return ['array'];
156
        }
157
        /**
158
         * @var $part \GoetasWebservices\XML\WSDLReader\Wsdl\Message\Part
159
         */
160 1
        $part = reset($parts);
161
162 1
        $class = $this->getClassFromPart($part);
163 1
        if ($this->unwrapReturn) {
164
            foreach ($class->getProperties() as $property) {
165
                $propertyClass = $property->getType();
166
                if ($t = $propertyClass->isSimpleType()) {
167
                    return [$t->getType()->getPhpType()];
168
                }
169
                return [$propertyClass->getPhpType()];
170
            }
171
        }
172
173 1
        if ($t = $class->isSimpleType()) {
174
            return [$t->getType()->getPhpType()];
175
        }
176 1
        return [$class->getPhpType()];
177
    }
178
179 1
    private function getClassFromPart(Part $part)
180
    {
181 1
        if ($part->getType()) {
182 1
            return $this->phpConverter->visitType($part->getType());
183
        } else {
184 1
            return $this->phpConverter->visitElementDef($part->getElement());
0 ignored issues
show
Compatibility introduced by
$part->getElement() of type object<GoetasWebservices...ma\Element\ElementItem> is not a sub-type of object<GoetasWebservices...ema\Element\ElementDef>. It seems like you assume a concrete implementation of the interface GoetasWebservices\XML\XS...ema\Element\ElementItem 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...
185
        }
186
    }
187
188
    /**
189
     * @param $part
190
     * @return array
191
     */
192 1
    private function extractSinglePartParameters(Part $part)
193
    {
194 1
        $params = [];
195 1
        $class = $this->getClassFromPart($part);
196
197 1
        foreach ($class->getPropertiesInHierarchy() as $property) {
198 1
            $t = $property->getType()->getPhpType();
199 1
            $params[] = $param = new ParamTag($property->getName(), [$t]);
0 ignored issues
show
Unused Code introduced by
$param 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...
200 1
        }
201 1
        return $params;
202
    }
203
}
204