Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
33 / 36
85.71% covered (warning)
85.71%
12 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractOperation
91.67% covered (success)
91.67%
33 / 36
85.71% covered (warning)
85.71%
12 / 14
26.39
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 setGenerator
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getGenerator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMethod
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getMethod
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParameterTypeModel
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isParameterTypeEmpty
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isParameterTypeAnArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParameterTypeArrayTypes
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
11.04
 isParameterTypeAString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isParameterTypeAModel
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParameterName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethodParameter
33.33% covered (danger)
33.33%
1 / 3
0.00% covered (danger)
0.00%
0 / 1
3.19
 getModelByName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\File;
6
7use WsdlToPhp\PackageGenerator\Generator\Generator;
8use WsdlToPhp\PackageGenerator\Model\AbstractModel;
9use WsdlToPhp\PackageGenerator\Model\Method as MethodModel;
10use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
11use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
12
13abstract class AbstractOperation
14{
15    public const SOAP_CALL_NAME = '__soapCall';
16
17    protected MethodModel $method;
18
19    protected Generator $generator;
20
21    public function __construct(MethodModel $method, Generator $generator)
22    {
23        $this
24            ->setMethod($method)
25            ->setGenerator($generator)
26        ;
27    }
28
29    public function setGenerator(Generator $generator): self
30    {
31        $this->generator = $generator;
32
33        return $this;
34    }
35
36    public function getGenerator(): Generator
37    {
38        return $this->generator;
39    }
40
41    public function setMethod(MethodModel $method): self
42    {
43        $this->method = $method;
44
45        return $this;
46    }
47
48    public function getMethod(): MethodModel
49    {
50        return $this->method;
51    }
52
53    protected function getParameterTypeModel(): ?StructModel
54    {
55        return $this->isParameterTypeAString() ? $this->getGenerator()->getStructByName($this->getMethod()->getParameterType()) : null;
56    }
57
58    protected function isParameterTypeEmpty(): bool
59    {
60        $parameterType = $this->getMethod()->getParameterType();
61
62        return empty($parameterType);
63    }
64
65    protected function isParameterTypeAnArray(): bool
66    {
67        return is_array($this->getMethod()->getParameterType());
68    }
69
70    protected function getParameterTypeArrayTypes(bool $methodUsage = false): array
71    {
72        $types = [];
73        $parameterTypes = $this->getMethod()->getParameterType();
74        if (!is_array($parameterTypes)) {
75            return [];
76        }
77
78        foreach ($parameterTypes as $parameterName => $parameterType) {
79            $type = $methodUsage ? null : AbstractModelFile::TYPE_STRING;
80
81            if (($model = $this->getGenerator()->getStructByName($parameterType)) instanceof StructModel) {
82                if ($model->isStruct() && !$model->isRestriction()) {
83                    $type = $model->getPackagedName(true);
84                } elseif (!$model->isStruct() && $model->isArray()) {
85                    if ($methodUsage) {
86                        $type = AbstractModelFile::TYPE_ARRAY;
87                    } else {
88                        $type = ($struct = $model->getTopInheritanceStruct()) ? sprintf('%s[]', $struct->getPackagedName(true)) : $model->getTopInheritance();
89                    }
90                }
91            }
92            $types[$parameterName] = $type;
93        }
94
95        return $types;
96    }
97
98    protected function isParameterTypeAString(): bool
99    {
100        return is_string($this->getMethod()->getParameterType());
101    }
102
103    protected function isParameterTypeAModel(): bool
104    {
105        return $this->getParameterTypeModel() instanceof StructModel;
106    }
107
108    protected function getParameterName(string $name): string
109    {
110        return lcfirst(AbstractModel::cleanString($name));
111    }
112
113    protected function getMethodParameter(string $name, ?string $type = null): PhpFunctionParameter
114    {
115        try {
116            return new PhpFunctionParameter($name, PhpFunctionParameter::NO_VALUE, $type);
117        } catch (\InvalidArgumentException $exception) {
118            throw new \InvalidArgumentException(sprintf('Unable to create function parameter for method "%s" with type "%s" and name "%s"', $this->getMethod()->getName(), var_export($type, true), $name), __LINE__, $exception);
119        }
120    }
121
122    protected function getModelByName(string $name): ?StructModel
123    {
124        return $this->getGenerator()->getStructByName($name);
125    }
126}