Passed
Push — feature/issue-264 ( 16c0d2...b0dcb4 )
by Mikaël
29:57 queued 27:12
created

Functions::parse()   D

Complexity

Conditions 19
Paths 54

Size

Total Lines 71
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 19.3114

Importance

Changes 0
Metric Value
cc 19
eloc 48
nc 54
nop 0
dl 0
loc 71
ccs 38
cts 42
cp 0.9048
crap 19.3114
rs 4.5166
c 0
b 0
f 0

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
        $methods = $this
12 120
            ->getGenerator()
13 120
            ->getSoapClient()
14 120
            ->getSoapClient()
15 120
            ->getSoapClient()
16 120
            ->__getFunctions()
17
        ;
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
                 * Returns type is not defined in some case.
51
                 */
52 120
                $start = 0;
53 120
                $returnType = false === mb_strpos($infos[0], '(') ? $infos[0] : '';
54 120
                $firstParameterType = '';
55 120
                if (empty($returnType) && false !== mb_strpos($infos[0], '(')) {
56
                    $start = 1;
57
                    [$methodName, $firstParameterType] = explode('(', $infos[0]);
58 120
                } elseif (false !== mb_strpos($infos[1], '(')) {
59 120
                    $start = 2;
60 120
                    [$methodName, $firstParameterType] = explode('(', $infos[1]);
61
                }
62 120
                if (!empty($methodName)) {
63 120
                    $methodParameters = [];
64 120
                    $infosCount = count($infos);
65 120
                    for ($i = $start; $i < $infosCount; $i += 2) {
66 120
                        $info = str_replace([
67 120
                            ', ',
68
                            ',',
69
                            '(',
70
                            ')',
71
                            '$',
72 120
                        ], '', trim($infos[$i]));
73 120
                        if (!empty($info)) {
74 120
                            $methodParameters = array_merge($methodParameters, [
75 120
                                $info => $start === $i ? $firstParameterType : $infos[$i - 1],
76
                            ]);
77
                        }
78
                    }
79 120
                    $services->addService($this->getGenerator()->getServiceName($methodName), $methodName, $methodParameters, empty($returnType) ? 'unknown' : $returnType);
80
                }
81
            }
82
        }
83 120
    }
84
}
85