Passed
Pull Request — develop (#234)
by Mikaël
49:21 queued 46:19
created

AbstractOperation::getMethodParameter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 2
cts 4
cp 0.5
crap 2.5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File;
6
7
use InvalidArgumentException;
8
use WsdlToPhp\PackageGenerator\Generator\Generator;
9
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
10
use WsdlToPhp\PackageGenerator\Model\Method as MethodModel;
11
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
12
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
13
14
abstract class AbstractOperation
15
{
16
    public const SOAP_CALL_NAME = '__soapCall';
17
18
    protected MethodModel $method;
19
20
    protected Generator $generator;
21
22 46
    public function __construct(MethodModel $method, Generator $generator)
23
    {
24
        $this
25 46
            ->setMethod($method)
26 46
            ->setGenerator($generator)
27
        ;
28 46
    }
29
30 46
    public function setGenerator(Generator $generator): self
31
    {
32 46
        $this->generator = $generator;
33
34 46
        return $this;
35
    }
36
37 46
    public function getGenerator(): Generator
38
    {
39 46
        return $this->generator;
40
    }
41
42 46
    public function setMethod(MethodModel $method): self
43
    {
44 46
        $this->method = $method;
45
46 46
        return $this;
47
    }
48
49 46
    public function getMethod(): MethodModel
50
    {
51 46
        return $this->method;
52
    }
53
54 46
    protected function getParameterTypeModel(): ?StructModel
55
    {
56 46
        return $this->isParameterTypeAString() ? $this->getGenerator()->getStructByName($this->getMethod()->getParameterType()) : null;
57
    }
58
59 46
    protected function isParameterTypeEmpty(): bool
60
    {
61 46
        $parameterType = $this->getMethod()->getParameterType();
62
63 46
        return empty($parameterType);
64
    }
65
66 46
    protected function isParameterTypeAnArray(): bool
67
    {
68 46
        return is_array($this->getMethod()->getParameterType());
69
    }
70
71 42
    protected function getParameterTypeArrayTypes(bool $methodUsage = false): array
72
    {
73 42
        $types = [];
74 42
        $parameterTypes = $this->getMethod()->getParameterType();
75 42
        if (!is_array($parameterTypes)) {
76
            return [];
77
        }
78
79 42
        foreach ($parameterTypes as $parameterName => $parameterType) {
80 42
            $type = $methodUsage ? null : AbstractModelFile::TYPE_STRING;
81
82 42
            if (($model = $this->getGenerator()->getStructByName($parameterType)) instanceof StructModel) {
83 40
                if ($model->isStruct() && !$model->isRestriction()) {
84 38
                    $type = $model->getPackagedName(true);
85 8
                } elseif (!$model->isStruct() && $model->isArray()) {
86 8
                    if ($methodUsage) {
87 8
                        $type = AbstractModelFile::TYPE_ARRAY;
88
                    } else {
89 8
                        $type = ($struct = $model->getTopInheritanceStruct()) ? sprintf('%s[]', $struct->getPackagedName(true)) : $model->getTopInheritance();
90
                    }
91
                }
92
            }
93 42
            $types[$parameterName] = $type;
94
        }
95
96 42
        return $types;
97
    }
98
99 46
    protected function isParameterTypeAString(): bool
100
    {
101 46
        return is_string($this->getMethod()->getParameterType());
102
    }
103
104 46
    protected function isParameterTypeAModel(): bool
105
    {
106 46
        return $this->getParameterTypeModel() instanceof StructModel;
107
    }
108
109 46
    protected function getParameterName(string $name): string
110
    {
111 46
        return lcfirst(AbstractModel::cleanString($name));
112
    }
113
114 46
    protected function getMethodParameter(string $name, ?string $type = null): PhpFunctionParameter
115
    {
116
        try {
117 46
            return new PhpFunctionParameter($name, PhpFunctionParameter::NO_VALUE, $type);
118
        } catch (InvalidArgumentException $exception) {
119
            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);
120
        }
121
    }
122
123 12
    protected function getModelByName(string $name): ?StructModel
124
    {
125 12
        return $this->getGenerator()->getStructByName($name);
126
    }
127
}
128