Functions::parse()   D
last analyzed

Complexity

Conditions 19
Paths 54

Size

Total Lines 72
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 19.1961

Importance

Changes 0
Metric Value
cc 19
eloc 48
c 0
b 0
f 0
nc 54
nop 0
dl 0
loc 72
ccs 45
cts 49
cp 0.9184
crap 19.1961
rs 4.5166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\Parser\SoapClient;
6
7
final class Functions extends AbstractParser
8
{
9 120
    public function parse(): void
10
    {
11 120
        $methods = $this
12 120
            ->getGenerator()
13 120
            ->getSoapClient()
14 120
            ->getSoapClient()
15 120
            ->getSoapClient()
16 120
            ->__getFunctions()
17 120
        ;
18
19 120
        $services = $this->getGenerator()->getServices();
20
21 120
        if (!is_array($methods) || 0 === count($methods)) {
0 ignored issues
show
introduced by
The condition is_array($methods) is always true.
Loading history...
22
            return;
23
        }
24
25 120
        foreach ($methods as $method) {
26 120
            $infos = explode(' ', $method);
27
            // "Regular" SOAP Style
28 120
            if (count($infos) < 3) {
29 12
                $returnType = $infos[0];
30 12
                if (false !== mb_strpos($infos[1], '()') && array_key_exists(1, $infos)) {
31 12
                    $methodName = trim(str_replace('()', '', $infos[1]));
32 12
                    $parameterType = null;
33
                } else {
34
                    [$methodName, $parameterType] = explode('(', $infos[1]);
35
                }
36 12
                if (!empty($returnType) && !empty($methodName)) {
37 12
                    $services->addService($this->getGenerator()->getServiceName($methodName), $methodName, $parameterType, $returnType);
38
                }
39
            } else {
40
                /*
41
                 * RPC SOAP Style
42
                 * Some RPC WS defines the return type as a list of values
43
                 * So we define the return type as an array and reset the information to use to extract method name and parameters
44
                 */
45 120
                if (0 === mb_stripos($infos[0], 'list(')) {
46 2
                    $infos = explode(' ', preg_replace('/(list\(.*\)\s)/i', '', $method));
47 2
                    array_unshift($infos, 'array');
48
                }
49
50
                /**
51
                 * Returns type is not defined in some case.
52
                 */
53 120
                $start = 0;
54 120
                $returnType = false === mb_strpos($infos[0], '(') ? $infos[0] : '';
55 120
                $firstParameterType = '';
56 120
                if (empty($returnType) && false !== mb_strpos($infos[0], '(')) {
57
                    $start = 1;
58
                    [$methodName, $firstParameterType] = explode('(', $infos[0]);
59 120
                } elseif (false !== mb_strpos($infos[1], '(')) {
60 120
                    $start = 2;
61 120
                    [$methodName, $firstParameterType] = explode('(', $infos[1]);
62
                }
63 120
                if (!empty($methodName)) {
64 120
                    $methodParameters = [];
65 120
                    $infosCount = count($infos);
66 120
                    for ($i = $start; $i < $infosCount; $i += 2) {
67 120
                        $info = str_replace([
68 120
                            ', ',
69 120
                            ',',
70 120
                            '(',
71 120
                            ')',
72 120
                            '$',
73 120
                        ], '', trim($infos[$i]));
74 120
                        if (!empty($info)) {
75 120
                            $methodParameters = array_merge($methodParameters, [
76 120
                                $info => $start === $i ? $firstParameterType : $infos[$i - 1],
77 120
                            ]);
78
                        }
79
                    }
80 120
                    $services->addService($this->getGenerator()->getServiceName($methodName), $methodName, $methodParameters, empty($returnType) ? 'unknown' : $returnType);
81
                }
82
            }
83
        }
84
    }
85
}
86