Completed
Push — develop ( acf457...a68de9 )
by Mikaël
63:26 queued 35:59
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 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
ccs 2
cts 4
cp 0.5
crap 2.5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File;
4
5
use WsdlToPhp\PackageGenerator\Generator\Generator;
6
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
7
use WsdlToPhp\PackageGenerator\Model\Method as MethodModel;
8
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
9
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
10
11
abstract class AbstractOperation
12
{
13
    /**
14
     * @var string
15
     */
16
    const DEFAULT_TYPE = 'string';
17
    /**
18
     * @var string
19
     */
20
    const ARRAY_TYPE = 'array';
21
    /**
22
     * @var string
23
     */
24
    const SOAP_CALL_NAME = '__call';
25
    /**
26
     * @var MethodModel
27
     */
28
    protected $method;
29
    /**
30
     * @var Generator
31
     */
32
    protected $generator;
33
    /**
34
     * @param MethodModel $method
35
     * @param Generator $generator
36
     */
37 138
    public function __construct(MethodModel $method, Generator $generator)
38
    {
39 138
        $this->setMethod($method)->setGenerator($generator);
40 138
    }
41
    /**
42
     * @return StructModel|null
43
     */
44 138
    protected function getParameterTypeModel()
45
    {
46 138
        return $this->isParameterTypeAString() ? $this->getGenerator()->getStructByName($this->getMethod()->getParameterType()) : null;
47
    }
48
    /**
49
     * @return bool
50
     */
51 12
    protected function isParameterTypeEmpty()
52
    {
53 12
        $parameterType = $this->getMethod()->getParameterType();
54 12
        return empty($parameterType);
55
    }
56
    /**
57
     * @return bool
58
     */
59 138
    protected function isParameterTypeAnArray()
60
    {
61 138
        return is_array($this->getMethod()->getParameterType());
62
    }
63
    /**
64
     * @param bool $methodUsage
65
     * @return string[]
66
     */
67 126
    protected function getParameterTypeArrayTypes($methodUsage = false)
68
    {
69 126
        $types = [];
70 126
        $parameterTypes = $this->getMethod()->getParameterType();
71 126
        if (is_array($parameterTypes)) {
0 ignored issues
show
introduced by
The condition is_array($parameterTypes) is always false.
Loading history...
72 126
            foreach ($parameterTypes as $parameterName => $parameterType) {
73 126
                $type = $methodUsage ? null : self::DEFAULT_TYPE;
74 126
                if (($model = $this->getGenerator()->getStructByName($parameterType)) instanceof StructModel) {
75 120
                    if ($model->isStruct() && !$model->isRestriction()) {
76 114
                        $type = $model->getPackagedName(true);
77 72
                    } elseif (!$model->isStruct() && $model->isArray()) {
78 24
                        if ($methodUsage) {
79 24
                            $type = self::ARRAY_TYPE;
80 12
                        } else {
81 24
                            $type = ($struct = $model->getTopInheritanceStruct()) ? sprintf('%s[]', $struct->getPackagedName(true)) : $model->getTopInheritance();
82
                        }
83 12
                    }
84 60
                }
85 126
                $types[$parameterName] = $type;
86 63
            }
87 63
        }
88 126
        return $types;
89
    }
90
    /**
91
     * @return bool
92
     */
93 138
    protected function isParameterTypeAString()
94
    {
95 138
        return is_string($this->getMethod()->getParameterType());
96
    }
97
    /**
98
     * @return bool
99
     */
100 138
    protected function isParameterTypeAModel()
101
    {
102 138
        return $this->getParameterTypeModel() instanceof StructModel;
103
    }
104
    /**
105
     * @param string $name
106
     * @return string
107
     */
108 138
    protected function getParameterName($name)
109
    {
110 138
        return lcfirst(AbstractModel::cleanString($name));
111
    }
112
    /**
113
     * @param string $name
114
     * @param string $type
115
     * @return PhpFunctionParameter
116
     */
117 138
    protected function getMethodParameter($name, $type = null)
118
    {
119
        try {
120 138
            return new PhpFunctionParameter($name, PhpFunctionParameter::NO_VALUE, $type);
121
        } catch (\InvalidArgumentException $exception) {
122
            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);
123
        }
124
    }
125
    /**
126
     * @param Generator $generator
127
     * @return AbstractOperation
128
     */
129 138
    public function setGenerator(Generator $generator)
130
    {
131 138
        $this->generator = $generator;
132 138
        return $this;
133
    }
134
    /**
135
     * @return Generator
136
     */
137 138
    public function getGenerator()
138
    {
139 138
        return $this->generator;
140
    }
141
    /**
142
     * @param MethodModel $method
143
     * @return AbstractOperation
144
     */
145 138
    public function setMethod(MethodModel $method)
146
    {
147 138
        $this->method = $method;
148 138
        return $this;
149
    }
150
    /**
151
     * @return MethodModel
152
     */
153 138
    public function getMethod()
154
    {
155 138
        return $this->method;
156
    }
157
    /**
158
     * @param string $name
159
     * @return StructModel|null
160
     */
161 36
    protected function getModelByName($name)
162
    {
163 36
        return $this->getGenerator()->getStructByName($name);
164
    }
165
}
166