Completed
Push — feature/issue-200 ( 2f17be )
by Mikaël
45:53
created

getOperationMethodParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File;
4
5
use WsdlToPhp\PackageGenerator\Parser\Wsdl\TagHeader;
6
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
7
use WsdlToPhp\PackageGenerator\Model\Method as MethodModel;
8
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
9
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
10
use WsdlToPhp\PackageGenerator\File\Utils as FileUtils;
11
12
class OperationAnnotationBlock extends AbstractOperation
13
{
14
    /**
15
     * @param PhpAnnotationBlock $annotationBlock
16
     * @return OperationAnnotationBlock
17
     */
18
    public function addAnnotationBlockForOperationMethod(PhpAnnotationBlock $annotationBlock)
19
    {
20
        $this->addOperationMethodDeclaration($annotationBlock)
21
            ->addOperationMethodMetaInformation($annotationBlock)
22
            ->addOperationMethodUses($annotationBlock)
23
            ->addOperationMethodParam($annotationBlock)
24
            ->addOperationMethodReturn($annotationBlock);
25
        return $this;
26
    }
27
    /**
28
     * @param PhpAnnotationBlock $annotationBlock
29
     * @return OperationAnnotationBlock
30
     */
31
    protected function addOperationMethodDeclaration(PhpAnnotationBlock $annotationBlock)
32
    {
33
        $annotationBlock->addChild(sprintf('Method to call the operation originally named %s', $this->getMethod()->getName()));
34
        if (!$this->getMethod()->isUnique()) {
35
            $annotationBlock->addChild('This method has been renamed because it is defined several times but with different signature');
36
        }
37
        return $this;
38
    }
39
    /**
40
     * @param PhpAnnotationBlock $annotationBlock
41
     * @return OperationAnnotationBlock
42
     */
43
    protected function addOperationMethodMetaInformation(PhpAnnotationBlock $annotationBlock)
44
    {
45
        $soapHeaderNames = $this->getMethod()->getMetaValue(TagHeader::META_SOAP_HEADER_NAMES, []);
46
        $soapHeaderTypes = $this->getMethod()->getMetaValue(TagHeader::META_SOAP_HEADER_TYPES, []);
47
        $soapHeaderNamespaces = $this->getMethod()->getMetaValue(TagHeader::META_SOAP_HEADER_NAMESPACES, []);
48
        $soapHeaders = $this->getMethod()->getMetaValue(TagHeader::META_SOAP_HEADERS, []);
49
        if (!empty($soapHeaderNames) && !empty($soapHeaderTypes) && !empty($soapHeaderNamespaces)) {
50
            $annotationBlock->addChild('Meta information extracted from the WSDL')
51
                ->addChild(new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('- SOAPHeaderNames: %s', implode(', ', $soapHeaderNames)), AbstractModelFile::ANNOTATION_LONG_LENGTH))
0 ignored issues
show
Bug introduced by
It seems like $soapHeaderNames can also be of type string; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
                ->addChild(new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('- SOAPHeaderNames: %s', implode(', ', /** @scrutinizer ignore-type */ $soapHeaderNames)), AbstractModelFile::ANNOTATION_LONG_LENGTH))
Loading history...
52
                ->addChild(new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('- SOAPHeaderNamespaces: %s', implode(', ', $soapHeaderNamespaces)), AbstractModelFile::ANNOTATION_LONG_LENGTH))
53
                ->addChild(new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('- SOAPHeaderTypes: %s', implode(', ', $this->getSoapHeaderTypesTypes($soapHeaderTypes))), AbstractModelFile::ANNOTATION_LONG_LENGTH))
0 ignored issues
show
Bug introduced by
It seems like $soapHeaderTypes can also be of type string; however, parameter $soapHeaderTypes of WsdlToPhp\PackageGenerat...tSoapHeaderTypesTypes() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
                ->addChild(new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('- SOAPHeaderTypes: %s', implode(', ', $this->getSoapHeaderTypesTypes(/** @scrutinizer ignore-type */ $soapHeaderTypes))), AbstractModelFile::ANNOTATION_LONG_LENGTH))
Loading history...
54
                ->addChild(new PhpAnnotation(PhpAnnotation::NO_NAME, sprintf('- SOAPHeaders: %s', implode(', ', $soapHeaders)), AbstractModelFile::ANNOTATION_LONG_LENGTH));
55
        }
56
        FileUtils::defineModelAnnotationsFromWsdl($annotationBlock, $this->getMethod(), [
57
            TagHeader::META_SOAP_HEADER_NAMES,
58
            TagHeader::META_SOAP_HEADER_NAMESPACES,
59
            TagHeader::META_SOAP_HEADER_TYPES,
60
            TagHeader::META_SOAP_HEADERS,
61
        ]);
62
        return $this;
63
    }
64
    /**
65
     * @param array $soapHeaderTypes
66
     * @return string[]
67
     */
68
    protected function getSoapHeaderTypesTypes(array $soapHeaderTypes)
69
    {
70
        $soapHeaderTypesTypes = [];
71
        foreach ($soapHeaderTypes as $soapHeaderType) {
72
            $soapHeaderTypesTypes[] = $this->getSoapHeaderTypeType($soapHeaderType, true);
73
        }
74
        return $soapHeaderTypesTypes;
75
    }
76
    /**
77
     * @param string $soapHeaderType
78
     * @param bool $namespaced
79
     * @return string
80
     */
81
    protected function getSoapHeaderTypeType($soapHeaderType, $namespaced = false)
82
    {
83
        $type = $soapHeaderType;
84
        $model = $this->getModelByName($soapHeaderType);
85
        if ($model instanceof AbstractModel) {
86
            $type = $model->getPackagedName($namespaced);
87
        }
88
        return $type;
89
    }
90
    /**
91
     * @param PhpAnnotationBlock $annotationBlock
92
     * @return OperationAnnotationBlock
93
     */
94
    protected function addOperationMethodUses(PhpAnnotationBlock $annotationBlock)
95
    {
96
        $annotationBlock->addChild(new PhpAnnotation(AbstractModelFile::ANNOTATION_USES, sprintf('%s::getSoapClient()', $this->getMethod()->getOwner()->getExtends(true))))
97
            ->addChild(new PhpAnnotation(AbstractModelFile::ANNOTATION_USES, sprintf('%s::setResult()', $this->getMethod()->getOwner()->getExtends(true))))
98
            ->addChild(new PhpAnnotation(AbstractModelFile::ANNOTATION_USES, sprintf('%s::getResult()', $this->getMethod()->getOwner()->getExtends(true))))
99
            ->addChild(new PhpAnnotation(AbstractModelFile::ANNOTATION_USES, sprintf('%s::saveLastError()', $this->getMethod()->getOwner()->getExtends(true))));
100
        return $this;
101
    }
102
    /**
103
     * @param PhpAnnotationBlock $annotationBlock
104
     * @return OperationAnnotationBlock
105
     */
106
    protected function addOperationMethodParam(PhpAnnotationBlock $annotationBlock)
107
    {
108
        $this->addOperationMethodParamFromArray($annotationBlock)->addOperationMethodParamFromModel($annotationBlock)->addOperationMethodParamFromString($annotationBlock);
109
        return $this;
110
    }
111
    /**
112
     * @param PhpAnnotationBlock $annotationBlock
113
     * @return OperationAnnotationBlock
114
     */
115
    protected function addOperationMethodParamFromArray(PhpAnnotationBlock $annotationBlock)
116
    {
117
        if ($this->isParameterTypeAnArray()) {
118
            foreach ($this->getParameterTypeArrayTypes() as $parameterName => $parameterType) {
119
                $annotationBlock->addChild($this->getOperationMethodParam($parameterType, $this->getParameterName($parameterName)));
120
            }
121
        }
122
        return $this;
123
    }
124
    /**
125
     * @param PhpAnnotationBlock $annotationBlock
126
     * @return OperationAnnotationBlock
127
     */
128
    protected function addOperationMethodParamFromModel(PhpAnnotationBlock $annotationBlock)
129
    {
130
        if ($this->isParameterTypeAModel()) {
131
            $annotationBlock->addChild($this->getOperationMethodParam($this->getParameterTypeModel()->getPackagedName(true), $this->getParameterName($this->getParameterTypeModel()->getPackagedName())));
132
        }
133
        return $this;
134
    }
135
    /**
136
     * @param string $type
137
     * @param string $name
138
     * @return PhpAnnotation
139
     */
140
    protected function getOperationMethodParam($type, $name)
141
    {
142
        return new PhpAnnotation(AbstractModelFile::ANNOTATION_PARAM, sprintf('%s $%s', $type, $name));
143
    }
144
    /**
145
     * @param PhpAnnotationBlock $annotationBlock
146
     * @return OperationAnnotationBlock
147
     */
148
    protected function addOperationMethodParamFromString(PhpAnnotationBlock $annotationBlock)
149
    {
150
        if ($this->isParameterTypeAString() && !$this->isParameterTypeAModel()) {
151
            $annotationBlock->addChild($this->getOperationMethodParam($this->getMethod()->getParameterType(), lcfirst($this->getMethod()->getParameterType())));
152
        }
153
        return $this;
154
    }
155
    /**
156
     * @param PhpAnnotationBlock $annotationBlock
157
     * @return OperationAnnotationBlock
158
     */
159
    protected function addOperationMethodReturn(PhpAnnotationBlock $annotationBlock)
160
    {
161
        $annotationBlock->addChild(new PhpAnnotation(AbstractModelFile::ANNOTATION_RETURN, sprintf('%s|bool', $this->getOperationMethodReturnType($this->getMethod()))));
162
        return $this;
163
    }
164
    /**
165
     * @param MethodModel $method
166
     * @return string
167
     */
168
    protected function getOperationMethodReturnType(MethodModel $method)
169
    {
170
        return Service::getOperationMethodReturnType($method, $this->getGenerator());
171
    }
172
}
173