Issues (51)

src/File/Service.php (4 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File;
6
7
use WsdlToPhp\PackageGenerator\ConfigurationReader\GeneratorOptions;
8
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
9
use WsdlToPhp\PackageGenerator\Container\PhpElement\Property as PropertyContainer;
10
use WsdlToPhp\PackageGenerator\File\Element\PhpFunctionParameter;
11
use WsdlToPhp\PackageGenerator\File\Validation\Rules;
12
use WsdlToPhp\PackageGenerator\Generator\Generator;
13
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
14
use WsdlToPhp\PackageGenerator\Model\Method;
15
use WsdlToPhp\PackageGenerator\Model\Method as MethodModel;
16
use WsdlToPhp\PackageGenerator\Model\Service as ServiceModel;
17
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
18
use WsdlToPhp\PackageGenerator\Model\StructAttribute as StructAttributeModel;
19
use WsdlToPhp\PackageGenerator\Parser\Wsdl\TagHeader;
20
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
21
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
22
use WsdlToPhp\PhpGenerator\Element\PhpConstant;
23
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter as PhpFunctionParameterBase;
24
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
25
use WsdlToPhp\PhpGenerator\Element\PhpProperty;
26
27
final class Service extends AbstractModelFile
28
{
29
    public const METHOD_SET_HEADER_PREFIX = 'setSoapHeader';
30
    public const PARAM_SET_HEADER_NAMESPACE = 'namespace';
31
    public const PARAM_SET_HEADER_MUSTUNDERSTAND = 'mustUnderstand';
32
    public const PARAM_SET_HEADER_ACTOR = 'actor';
33
    public const METHOD_GET_RESULT = 'getResult';
34
35
    /**
36
     * Method model can't be found in case the original method's name is unclean:
37
     * - ex: my.operation.name becomes my_operation_name
38
     * thus the Model from Model\Service::getMethod() can't be found
39
     * So we store the generated name associated to the original method object.
40
     */
41
    protected array $methodNames = [];
42
43 48
    public static function getOperationMethodReturnType(MethodModel $method, Generator $generator): string
44
    {
45 48
        $returnType = $method->getReturnType();
46
47 48
        if (is_null($returnType)) {
48 2
            return 'null';
49
        }
50
51 46
        if ((($struct = $generator->getStructByName($returnType)) instanceof StructModel) && !$struct->isRestriction()) {
52 40
            if ($struct->isStruct()) {
53 40
                $returnType = $struct->getPackagedName(true);
54 8
            } elseif ($struct->isArray()) {
55 8
                if (($structInheritance = $struct->getInheritanceStruct()) instanceof StructModel) {
56 8
                    $returnType = sprintf('%s[]', $structInheritance->getPackagedName(true));
57
                } else {
58 4
                    $returnType = $struct->getInheritance();
59
                }
60
            }
61
        }
62
63 46
        return $returnType;
64
    }
65
66 50
    public function setModel(AbstractModel $model): self
67
    {
68 50
        if (!$model instanceof ServiceModel) {
69 2
            throw new \InvalidArgumentException('Model must be an instance of a Service', __LINE__);
70
        }
71
72 48
        return parent::setModel($model);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::setModel($model) returns the type WsdlToPhp\PackageGenerator\File\AbstractModelFile which includes types incompatible with the type-hinted return WsdlToPhp\PackageGenerator\File\Service.
Loading history...
73
    }
74
75 48
    public function getModel(): ?ServiceModel
76
    {
77 48
        return parent::getModel();
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::getModel() could return the type WsdlToPhp\PackageGenerator\Model\AbstractModel which includes types incompatible with the type-hinted return WsdlToPhp\PackageGenerator\Model\Service|null. Consider adding an additional type-check to rule them out.
Loading history...
78
    }
79
80 46
    protected function fillClassConstants(ConstantContainer $constants): void
81
    {
82 46
    }
83
84
    protected function getConstantAnnotationBlock(PhpConstant $constant): ?PhpAnnotationBlock
85
    {
86
        return null;
87
    }
88
89 46
    protected function fillClassProperties(PropertyContainer $properties): void
90
    {
91 46
    }
92
93
    protected function getPropertyAnnotationBlock(PhpProperty $property): ?PhpAnnotationBlock
94
    {
95
        return null;
96
    }
97
98 46
    protected function defineUseStatements(): AbstractModelFile
99
    {
100 46
        $this->getFile()->addUse(\SoapFault::class);
101
102
        /** @var Method $method */
103 46
        foreach ($this->getModel()->getMethods() as $method) {
104 46
            $soapHeaderTypes = $method->getMetaValue(TagHeader::META_SOAP_HEADER_TYPES, []);
105 46
            if (!is_array($soapHeaderTypes)) {
106
                continue;
107
            }
108 46
            foreach ($soapHeaderTypes as $soapHeaderType) {
109 12
                $model = $this->getModelByName($soapHeaderType);
110 12
                if (!$model instanceof StructModel) {
111
                    continue;
112
                }
113 12
                if (!$model->isRestriction()) {
114 12
                    continue;
115
                }
116
117 2
                $this->getFile()->addUse(\InvalidArgumentException::class);
118
119 2
                break 2;
120
            }
121
        }
122
123 46
        return parent::defineUseStatements();
124
    }
125
126 46
    protected function getClassDeclarationLineText(): string
127
    {
128 46
        return GeneratorOptions::VALUE_NONE === $this->getGenerator()->getOptionGatherMethods() ? 'This class stands for all operations' : parent::getClassDeclarationLineText();
129
    }
130
131 46
    protected function fillClassMethods(): void
132
    {
133 46
        $this
134 46
            ->addSoapHeaderMethods()
135 46
            ->addOperationsMethods()
136 46
            ->addGetResultMethod()
137 46
        ;
138
    }
139
140 46
    protected function addSoapHeaderMethods(): self
141
    {
142 46
        foreach ($this->getModel()->getMethods() as $method) {
143 46
            $this->addSoapHeaderFromMethod($method);
144
        }
145
146 46
        return $this;
147
    }
148
149 46
    protected function addSoapHeaderFromMethod(MethodModel $method): self
150
    {
151 46
        $soapHeaderNames = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMES, []);
152 46
        $soapHeaderNamespaces = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMESPACES, []);
153 46
        $soapHeaderTypes = $method->getMetaValue(TagHeader::META_SOAP_HEADER_TYPES, []);
154 46
        if (is_array($soapHeaderNames) && is_array($soapHeaderNamespaces) && is_array($soapHeaderTypes)) {
155 46
            foreach ($soapHeaderNames as $index => $soapHeaderName) {
156 12
                $methodName = $this->getSoapHeaderMethodName($soapHeaderName);
157 12
                if (is_null($this->methods->get($methodName))) {
158 12
                    $soapHeaderNamespace = array_key_exists($index, $soapHeaderNamespaces) ? $soapHeaderNamespaces[$index] : null;
159 12
                    $soapHeaderType = array_key_exists($index, $soapHeaderTypes) ? $soapHeaderTypes[$index] : null;
160 12
                    $this->methods->add($this->getSoapHeaderMethod($methodName, $soapHeaderName, $soapHeaderNamespace, $soapHeaderType));
0 ignored issues
show
It seems like $soapHeaderNamespace can also be of type null; however, parameter $soapHeaderNamespace of WsdlToPhp\PackageGenerat...::getSoapHeaderMethod() does only seem to accept string, 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

160
                    $this->methods->add($this->getSoapHeaderMethod($methodName, $soapHeaderName, /** @scrutinizer ignore-type */ $soapHeaderNamespace, $soapHeaderType));
Loading history...
It seems like $soapHeaderType can also be of type null; however, parameter $soapHeaderType of WsdlToPhp\PackageGenerat...::getSoapHeaderMethod() does only seem to accept string, 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

160
                    $this->methods->add($this->getSoapHeaderMethod($methodName, $soapHeaderName, $soapHeaderNamespace, /** @scrutinizer ignore-type */ $soapHeaderType));
Loading history...
161
                }
162
            }
163
        }
164
165 46
        return $this;
166
    }
167
168 12
    protected function getSoapHeaderMethod(string $methodName, string $soapHeaderName, string $soapHeaderNamespace, string $soapHeaderType): PhpMethod
169
    {
170
        try {
171 12
            $method = new PhpMethod($methodName, [
172 12
                $firstParameter = new PhpFunctionParameter(lcfirst($soapHeaderName), PhpFunctionParameterBase::NO_VALUE, $this->getTypeFromName($soapHeaderType)),
173 12
                new PhpFunctionParameterBase(self::PARAM_SET_HEADER_NAMESPACE, $soapHeaderNamespace, self::TYPE_STRING),
174 12
                new PhpFunctionParameterBase(self::PARAM_SET_HEADER_MUSTUNDERSTAND, false, self::TYPE_BOOL),
175 12
                new PhpFunctionParameterBase(self::PARAM_SET_HEADER_ACTOR, null, '?'.self::TYPE_STRING),
176 12
            ], self::TYPE_SELF);
177
178 12
            $model = $this->getModelByName($soapHeaderType);
179 12
            if ($model instanceof StructModel) {
180 12
                $rules = new Rules($this, $method, new StructAttributeModel($model->getGenerator(), $soapHeaderType, $model->getName(), $model), $this->methods);
181 12
                $rules->applyRules(lcfirst($soapHeaderName));
182 12
                $firstParameter->setModel($model);
183
            }
184 12
            $method->addChild(sprintf('return $this->%s($%s, \'%s\', $%s, $%s, $%s);', self::METHOD_SET_HEADER_PREFIX, self::PARAM_SET_HEADER_NAMESPACE, $soapHeaderName, lcfirst($soapHeaderName), self::PARAM_SET_HEADER_MUSTUNDERSTAND, self::PARAM_SET_HEADER_ACTOR));
185
        } catch (\InvalidArgumentException $exception) {
186
            throw new \InvalidArgumentException(sprintf('Unable to create function parameter for service "%s" with type "%s"', $this->getModel()->getName(), var_export($this->getTypeFromName($soapHeaderName), true)), __LINE__, $exception);
187
        }
188
189 12
        return $method;
190
    }
191
192 12
    protected function getTypeFromName(string $name): ?string
193
    {
194 12
        return self::getPhpType(
195 12
            $this->getStructAttributeTypeAsPhpType(new StructAttributeModel($this->generator, 'any', $name)),
196 12
            $this->getGenerator()->getOptionXsdTypesPath(),
197 12
            $this->getStructAttributeTypeAsPhpType(new StructAttributeModel($this->generator, 'any', $name))
198 12
        );
199
    }
200
201 12
    protected function getSoapHeaderMethodName(string $soapHeaderName): string
202
    {
203 12
        return sprintf('%s%s', self::METHOD_SET_HEADER_PREFIX, ucfirst($soapHeaderName));
204
    }
205
206 46
    protected function addOperationsMethods(): self
207
    {
208 46
        foreach ($this->getModel()->getMethods() as $method) {
209 46
            $this->addMainMethod($method);
210
        }
211
212 46
        return $this;
213
    }
214
215 46
    protected function addGetResultMethod(): self
216
    {
217 46
        $method = new PhpMethod(self::METHOD_GET_RESULT);
218 46
        $method->addChild('return parent::getResult();');
219 46
        $this->methods->add($method);
220
221 46
        return $this;
222
    }
223
224 46
    protected function addMainMethod(MethodModel $method): self
225
    {
226 46
        $methodFile = new Operation($method, $this->getGenerator());
227 46
        $mainMethod = $methodFile->getMainMethod();
228 46
        $this->methods->add($mainMethod);
229 46
        $this->setModelFromMethod($mainMethod, $method);
230
231 46
        return $this;
232
    }
233
234 46
    protected function getMethodAnnotationBlock(PhpMethod $method): PhpAnnotationBlock
235
    {
236 46
        $annotationBlock = new PhpAnnotationBlock();
237 46
        if (0 === mb_stripos($method->getName(), self::METHOD_SET_HEADER_PREFIX)) {
238 12
            $this->addAnnotationBlockForSoapHeaderMethod($annotationBlock, $method);
239 46
        } elseif (self::METHOD_GET_RESULT === $method->getName()) {
240 46
            $this->addAnnotationBlockForgetResultMethod($annotationBlock);
241
        } else {
242 46
            $this->addAnnotationBlockForOperationMethod($annotationBlock, $method);
243
        }
244
245 46
        return $annotationBlock;
246
    }
247
248 12
    protected function addAnnotationBlockForSoapHeaderMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method): self
249
    {
250 12
        $methodParameters = $method->getParameters();
251 12
        $firstParameter = array_shift($methodParameters);
252 12
        if ($firstParameter instanceof PhpFunctionParameter) {
253 12
            $annotationBlock->addChild(sprintf('Sets the %s SoapHeader param', ucfirst($firstParameter->getName())));
254 12
            $firstParameterType = $firstParameter->getType();
255 12
            if ($firstParameter->getModel() instanceof StructModel) {
256 12
                $firstParameterType = $this->getStructAttributeTypeAsPhpType(new StructAttributeModel($firstParameter->getModel()->getGenerator(), $firstParameter->getName(), $firstParameter->getModel()->getName(), $firstParameter->getModel()));
257 12
                if ($firstParameter->getModel()->isRestriction()) {
258 2
                    $annotationBlock
259 2
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $firstParameter->getModel()->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))
260 2
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $firstParameter->getModel()->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)))
261 2
                        ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, \InvalidArgumentException::class))
262 2
                    ;
263
                }
264
            }
265 12
            $annotationBlock
266 12
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()->getExtends(true), self::METHOD_SET_HEADER_PREFIX)))
267 12
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $firstParameterType, $firstParameter->getName())))
268 12
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', self::TYPE_STRING, self::PARAM_SET_HEADER_NAMESPACE)))
269 12
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', self::TYPE_BOOL, self::PARAM_SET_HEADER_MUSTUNDERSTAND)))
270 12
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s|null $%s', self::TYPE_STRING, self::PARAM_SET_HEADER_ACTOR)))
271 12
                ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)))
272 12
            ;
273
        }
274
275 12
        return $this;
276
    }
277
278 46
    protected function addAnnotationBlockForOperationMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method): self
279
    {
280 46
        if (($model = $this->getModelFromMethod($method)) instanceof MethodModel) {
281 46
            $operationAnnotationBlock = new OperationAnnotationBlock($model, $this->getGenerator());
282 46
            $operationAnnotationBlock->addAnnotationBlockForOperationMethod($annotationBlock);
283
        }
284
285 46
        return $this;
286
    }
287
288 46
    protected function addAnnotationBlockForgetResultMethod(PhpAnnotationBlock $annotationBlock): self
289
    {
290 46
        $annotationBlock
291 46
            ->addChild('Returns the result')->addChild(new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::getResult()', $this->getModel()->getExtends(true))))
292 46
            ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getServiceReturnTypes()))
293 46
        ;
294
295 46
        return $this;
296
    }
297
298 46
    protected function getServiceReturnTypes(): string
299
    {
300 46
        $returnTypes = [];
301 46
        foreach ($this->getModel()->getMethods() as $method) {
302 46
            $returnTypes[] = self::getOperationMethodReturnType($method, $this->getGenerator());
303
        }
304 46
        natcasesort($returnTypes);
305
306 46
        return implode('|', array_unique($returnTypes));
307
    }
308
309 46
    protected function getModelFromMethod(PhpMethod $method): ?MethodModel
310
    {
311 46
        $model = $this->getGenerator()->getServiceMethod($method->getName());
312 46
        if (!$model instanceof MethodModel) {
313 8
            $model = array_key_exists($method->getName(), $this->methodNames) ? $this->methodNames[$method->getName()] : null;
314
        }
315
316 46
        return $model;
317
    }
318
319 46
    protected function setModelFromMethod(PhpMethod $phpMethod, MethodModel $methodModel): self
320
    {
321 46
        $this->methodNames[$phpMethod->getName()] = $methodModel;
322
323 46
        return $this;
324
    }
325
}
326