Completed
Push — feature/issue-54 ( 548adc...74298b )
by Mikaël
35:52 queued 06:48
created

Method::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1
Metric Value
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.6666
cc 1
eloc 7
nc 1
nop 6
crap 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\Model;
4
5
use WsdlToPhp\PackageGenerator\Generator\Generator;
6
7
/**
8
 * Class Method stands for an available operation described in the WSDL
9
 */
10
class Method extends AbstractModel
11
{
12
    /**
13
     * Type of the parameter for the operation
14
     * @var string
15
     */
16
    private $parameterType = '';
17
    /**
18
     * Type of the return value for the operation
19
     * @var string
20
     */
21
    private $returnType = '';
22
    /**
23
     * Indicates function is not alone with this name, then its name is contextualized based on its parameter(s)
24
     * @var bool
25
     */
26
    private $isUnique = true;
27
    /**
28
     * Generated method name stored as soon as it has been defined once
29
     * @var string
30
     */
31
    private $methodName = null;
32
    /**
33
     * Main constructor
34
     * @see AbstractModel::__construct()
35
     * @uses Method::setParameterType()
36
     * @uses Method::setReturnType()
37
     * @uses AbstractModel::setOwner()
38
     * @param Generator $generator
39
     * @param string $name the function name
40
     * @param string|array $parameterType the type/name of the parameter
41
     * @param string|array $returnType the type/name of the return value
42
     * @param Service $service defines the struct which owns this value
43
     * @param bool $isUnique defines if the method is unique or not
44
     */
45 496
    public function __construct(Generator $generator, $name, $parameterType, $returnType, Service $service, $isUnique = true)
46
    {
47 496
        parent::__construct($generator, $name);
48 372
        $this
49 496
            ->setParameterType($parameterType)
50 496
            ->setReturnType($returnType)
51 496
            ->setIsUnique($isUnique)
52 496
            ->setOwner($service);
53 496
    }
54
    /**
55
     * Method name can't starts with numbers
56
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::getCleanName()
57
     * @return string
58
     */
59 496
    public function getCleanName($keepMultipleUnderscores = true)
60
    {
61 496
        return preg_replace('/^(\d+)([a-zA-Z0-9]*)$/', '_$2', parent::getCleanName($keepMultipleUnderscores));
62
    }
63
    /**
64
     * Returns the name of the method that is used to call the operation
65
     * It takes care of the fact that the method might not be the only one named as it is.
66
     * @uses Method::getCleanName()
67
     * @uses AbstractModel::replaceReservedPhpKeyword()
68
     * @uses AbstractModel::getOwner()
69
     * @uses AbstractModel::getPackagedName()
70
     * @uses AbstractModel::uniqueName()
71
     * @uses Method::getOwner()
72
     * @uses Method::getParameterType()
73
     * @uses Method::getIsUnique()
74
     * @return string
75
     */
76 496
    public function getMethodName()
77
    {
78 496
        if (empty($this->methodName)) {
79 496
            $methodName = $this->getCleanName();
80 496
            if (!$this->getIsUnique()) {
81 12
                if (is_string($this->getParameterType())) {
82 12
                    $methodName .= ucfirst($this->getParameterType());
83 9
                } else {
84
                    $methodName .= '_' . md5(var_export($this->getParameterType(), true));
85
                }
86 9
            }
87 496
            $this->methodName = self::uniqueName(self::replaceReservedPhpKeyword($methodName, $this->getOwner()->getPackagedName()), $this->getOwner()->getPackagedName());
88 372
        }
89 496
        return $this->methodName;
90
    }
91
    /**
92
     * Returns the parameter type
93
     * @return string|string[]
94
     */
95 348
    public function getParameterType()
96
    {
97 348
        return $this->parameterType;
98
    }
99
    /**
100
     * Set the parameter type
101
     * @param string|string[]
102
     * @return Method
103
     */
104 496
    public function setParameterType($parameterType)
105
    {
106 496
        $this->parameterType = $parameterType;
107 496
        return $this;
108
    }
109
    /**
110
     * Returns the retrun type
111
     * @return string
112
     */
113 284
    public function getReturnType()
114
    {
115 284
        return $this->returnType;
116
    }
117
    /**
118
     * Set the retrun type
119
     * @param string|string[]
120
     * @return Method
121
     */
122 496
    public function setReturnType($returnType)
123
    {
124 496
        $this->returnType = $returnType;
125 496
        return $this;
126
    }
127
    /**
128
     * Returns the isUnique property
129
     * @return bool
130
     */
131 496
    public function getIsUnique()
132
    {
133 496
        return $this->isUnique;
134
    }
135
    /**
136
     * Set the isUnique property
137
     * @param bool
138
     * @return Method
139
     */
140 496
    public function setIsUnique($isUnique)
141
    {
142 496
        $this->isUnique = $isUnique;
143 496
        return $this;
144
    }
145
    /**
146
     * Returns the owner model object, meaning a Service object
147
     * @see AbstractModel::getOwner()
148
     * @uses AbstractModel::getOwner()
149
     * @return Service
150
     */
151 496
    public function getOwner()
152
    {
153 496
        return parent::getOwner();
154
    }
155
}
156