Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
1 / 1
Method
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
12 / 12
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getCleanName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethodName
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 getParameterType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setParameterType
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getReturnType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setReturnType
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isUnique
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUnique
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getOwner
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getReservedMethodsInstance
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toJsonSerialize
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\Model;
6
7use WsdlToPhp\PackageGenerator\ConfigurationReader\ServiceReservedMethod;
8use WsdlToPhp\PackageGenerator\Generator\Generator;
9
10/**
11 * Class Method stands for an available operation described in the WSDL.
12 */
13class Method extends AbstractModel
14{
15    protected $parameterType;
16
17    protected $returnType;
18
19    protected bool $isUnique = true;
20
21    protected ?string $methodName = null;
22
23    public function __construct(Generator $generator, string $name, $parameterType = null, $returnType = null, ?Service $service = null, bool $isUnique = true)
24    {
25        parent::__construct($generator, $name);
26        $this
27            ->setParameterType($parameterType)
28            ->setReturnType($returnType)
29            ->setUnique($isUnique)
30            ->setOwner($service)
31        ;
32    }
33
34    /**
35     * Method name can't starts with numbers.
36     */
37    public function getCleanName(bool $keepMultipleUnderscores = true): string
38    {
39        return preg_replace('/^(\d+)([a-zA-Z0-9]*)$/', '_$2', parent::getCleanName($keepMultipleUnderscores));
40    }
41
42    /**
43     * Returns the name of the method that is used to call the operation
44     * It takes care of the fact that the method might not be the only one named as it is.
45     */
46    public function getMethodName(): string
47    {
48        if (empty($this->methodName)) {
49            $methodName = $this->getCleanName();
50            if (!$this->isUnique()) {
51                if (is_string($this->getParameterType())) {
52                    $methodName .= ucfirst($this->getParameterType());
53                } else {
54                    $methodName .= '_'.md5(var_export($this->getParameterType(), true));
55                }
56            }
57            $context = $this->getOwner()->getPackagedName();
58            $methodName = $this->replaceReservedMethod($methodName, $context);
59            $methodName = self::replacePhpReservedKeyword($methodName, $context);
60            $this->methodName = self::uniqueName($methodName, $this->getOwner()->getPackagedName());
61        }
62
63        return $this->methodName;
64    }
65
66    public function getParameterType()
67    {
68        return $this->parameterType;
69    }
70
71    public function setParameterType($parameterType): self
72    {
73        $this->parameterType = $parameterType;
74
75        return $this;
76    }
77
78    public function getReturnType()
79    {
80        return $this->returnType;
81    }
82
83    public function setReturnType($returnType): self
84    {
85        $this->returnType = $returnType;
86
87        return $this;
88    }
89
90    public function isUnique(): bool
91    {
92        return $this->isUnique;
93    }
94
95    public function setUnique(bool $isUnique = true): self
96    {
97        $this->isUnique = $isUnique;
98
99        return $this;
100    }
101
102    public function getOwner(): Service
103    {
104        return parent::getOwner();
105    }
106
107    public function getReservedMethodsInstance(?string $filename = null): ServiceReservedMethod
108    {
109        return ServiceReservedMethod::instance($filename);
110    }
111
112    protected function toJsonSerialize(): array
113    {
114        return [
115            'unique' => $this->isUnique,
116            'methodName' => $this->methodName,
117            'parameterType' => $this->parameterType,
118            'returnType' => $this->returnType,
119        ];
120    }
121}