Completed
Push — master ( f27e2e...5dd696 )
by Asmir
10s
created

ClientStubGenerator::getOperationParams()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 29
Code Lines 18

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.439
c 0
b 0
f 0
cc 6
eloc 18
nc 4
nop 1
crap 6.1215
1
<?php
2
namespace GoetasWebservices\SoapServices\SoapClient\StubGeneration;
3
4
use Doctrine\Common\Inflector\Inflector;
5
use GoetasWebservices\SoapServices\SoapClient\StubGeneration\Tag\MethodTag;
6
use GoetasWebservices\SoapServices\SoapClient\StubGeneration\Tag\ParamTag;
7
use GoetasWebservices\XML\WSDLReader\Wsdl\Message\Part;
8
use GoetasWebservices\XML\WSDLReader\Wsdl\PortType;
9
use GoetasWebservices\Xsd\XsdToPhp\Naming\NamingStrategy;
10
use GoetasWebservices\Xsd\XsdToPhp\Php\PhpConverter;
11
use Zend\Code\Generator\ClassGenerator;
12
use Zend\Code\Generator\DocBlockGenerator;
13
14
class ClientStubGenerator
15
{
16
    protected $reservedWords = [
17
        'int',
18
        'float',
19
        'bool',
20
        'string',
21
        'true',
22
        'false',
23
        'null',
24
        'resource',
25
        'object',
26
        'mixed',
27
        'numeric',
28
    ];
29
30
    private $baseNs = [
31
        'headers' => '\\SoapEnvelope\\Headers',
32
        'parts' => '\\SoapEnvelope\\Parts',
33
        'messages' => '\\SoapEnvelope\\Messages',
34
    ];
35
    /**
36
     * @var NamingStrategy
37
     */
38
    private $namingStrategy;
39
    /**
40
     * @var PhpConverter
41
     */
42
    private $phpConverter;
43
    /**
44
     * @var bool
45
     */
46
    private $unwrapReturn = false;
47
48 1
    public function __construct(PhpConverter $phpConverter, NamingStrategy $namingStrategy, $unwrapReturn = false, array $baseNs = array())
49
    {
50 1
        foreach ($baseNs as $k => $ns) {
51
            if (isset($this->baseNs[$k])) {
52
                $this->baseNs[$k] = $ns;
53
            }
54 1
        }
55 1
        $this->baseNs = $baseNs;
56 1
        $this->namingStrategy = $namingStrategy;
57 1
        $this->phpConverter = $phpConverter;
58 1
        $this->unwrapReturn = $unwrapReturn;
59 1
    }
60
61 1
    public function setUnwrap($mode = true)
62
    {
63 1
        $this->unwrapReturn = (bool)$mode;
64 1
    }
65
66
    /**
67
     * @param PortType[] $ports
68
     * @return ClassGenerator
69
     */
70 1
    public function generate(array $ports)
71
    {
72 1
        $classes = array();
73 1
        foreach ($ports as $port) {
74 1
            $class = new ClassGenerator();
75 1
            if ($this->visitPortType($class, $port) !== false) {
76 1
                $classes[] = $class;
77 1
            }
78 1
        }
79 1
        return $classes;
80
    }
81
82 1
    private function visitPortType(ClassGenerator $class, PortType $portType)
83
    {
84 1
        if (!count($portType->getOperations())) {
85
            return false;
86
        }
87 1
        $docBlock = new DocBlockGenerator("Class representing " . $portType->getName());
88 1
        $docBlock->setWordWrap(false);
89 1
        if ($portType->getDocumentation()) {
90
            $docBlock->setLongDescription($portType->getDocumentation());
91
        }
92
93 1
        $namespaces = $this->phpConverter->getNamespaces();
94 1
        $class->setNamespaceName($namespaces[$portType->getDefinition()->getTargetNamespace()] . "\\SoapStubs");
95 1
        $class->setName(Inflector::classify($portType->getName()));
96 1
        $class->setDocblock($docBlock);
97
98 1
        foreach ($portType->getOperations() as $operation) {
99 1
            $operationTag = $this->visitOperation($operation);
100 1
            $docBlock->setTag($operationTag);
101 1
        }
102 1
    }
103
104 1
    private function visitOperation(PortType\Operation $operation)
105
    {
106 1
        $types = $this->getOperationReturnTypes($operation);
107 1
        $operationTag = new MethodTag(
108 1
            Inflector::camelize($operation->getName()),
109 1
            $types,
110 1
            preg_replace("/[\n\r]+/", " ", $operation->getDocumentation())
111 1
        );
112 1
        $params = $this->getOperationParams($operation);
113 1
        $operationTag->setParams($params);
114 1
        return $operationTag;
115
    }
116
117 1
    private function getOperationParams(PortType\Operation $operation)
118
    {
119 1
        if (!$operation->getInput()) {
120
            return [];
121
        }
122
123 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...
124 1
        if (!$parts) {
125 1
            return [];
126
        }
127
128 1
        if (count($parts) > 1) {
129 1
            $params = [];
130 1
            foreach ($parts as $part) {
131 1
                $partName = $this->namingStrategy->getPropertyName($part);
132 1
                $class = $this->getClassFromPart($part);
133
134 1
                $typeName = $class->getPhpType();
135 1
                if ($t = $class->isSimpleType()) {
136
                    $typeName = $t->getType()->getPhpType();
137
                }
138 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...
139 1
            }
140 1
        } else {
141 1
            $params = $this->extractSinglePartParameters(reset($parts));
142
        }
143
144 1
        return $params;
145
    }
146
147 1
    private function getOperationReturnTypes(PortType\Operation $operation)
148
    {
149 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...
150 1
            return ['void'];
151
        }
152 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...
153 1
        if (count($parts) > 1) {
154 1
            return ['array'];
155
        }
156
        /**
157
         * @var $part \GoetasWebservices\XML\WSDLReader\Wsdl\Message\Part
158
         */
159 1
        $part = reset($parts);
160
161 1
        $class = $this->getClassFromPart($part);
162 1
        if ($this->unwrapReturn) {
163
            foreach ($class->getProperties() as $property) {
164
                $propertyClass = $property->getType();
165
                if ($t = $propertyClass->isSimpleType()) {
166
                    return [$t->getType()->getPhpType()];
167
                }
168
                return [$propertyClass->getPhpType()];
169
            }
170
        }
171
172 1
        if ($t = $class->isSimpleType()) {
173
            return [$t->getType()->getPhpType()];
174
        }
175 1
        return [$class->getPhpType()];
176
    }
177
178 1
    private function getClassFromPart(Part $part)
179
    {
180 1
        if ($part->getType()) {
181 1
            return $this->phpConverter->visitType($part->getType());
182
        } else {
183 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...
184
        }
185
    }
186
187
    /**
188
     * @param $part
189
     * @return array
190
     */
191 1
    private function extractSinglePartParameters(Part $part)
192
    {
193 1
        $params = [];
194 1
        $class = $this->getClassFromPart($part);
195
196 1
        foreach ($class->getPropertiesInHierarchy() as $property) {
197 1
            $t = $property->getType()->getPhpType();
198 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...
199 1
        }
200 1
        return $params;
201
    }
202
}
203