Completed
Push — feature/issue-84 ( 44128e...1e0a90 )
by Mikaël
34:57
created

Structs   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 5
dl 0
loc 142
ccs 57
cts 57
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 13 3
B parseType() 0 18 6
A parseComplexStruct() 0 13 3
A parseUnionStruct() 0 12 2
A cleanType() 0 15 1
A isStructDefined() 0 4 1
A structHasBeenDefined() 0 5 1
A typeSignature() 0 4 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\Parser\SoapClient;
4
5
class Structs extends AbstractParser
6
{
7
    /**
8
     * @var string
9
     */
10
    const STRUCT_DECLARATION = 'struct';
11
    /**
12
     * @var string
13
     */
14
    const UNION_DECLARATION = 'union';
15
    /**
16
     * @var string
17
     */
18
    const ANY_XML_DECLARATION = '<anyXML>';
19
    /**
20
     * @var string
21
     */
22
    const ANY_XML_TYPE = '\DOMDocument';
23
    /**
24
     * @var string[]
25
     */
26
    private $definedStructs = array();
27 464
    /**
28
     * Parses the SoapClient types
29 464
     * @see \WsdlToPhp\PackageGenerator\Parser\ParserInterface::parse()
30 464
     */
31 464
    public function parse()
32 464
    {
33 464
        $types = $this->getGenerator()
34 464
            ->getSoapClient()
35 464
            ->getSoapClient()
36 448
            ->getSoapClient()
37 464
            ->__getTypes();
38 464
        if (is_array($types)) {
39 464
            foreach ($types as $type) {
40
                $this->parseType($type);
41
            }
42
        }
43 448
    }
44
    /**
45 448
     * @param string $type
46 448
     */
47 448
    protected function parseType($type)
48 448
    {
49 448
        if (!$this->isStructDefined($type)) {
50 448
            $cleanType = self::cleanType($type);
51 448
            $typeDef = explode(' ', $cleanType);
52 448
            if (array_key_exists(1, $typeDef) && !empty($typeDef)) {
53 444
                $structName = $typeDef[1];
54
                if ($typeDef[0] === self::UNION_DECLARATION) {
55 448
                    $this->parseUnionStruct($typeDef);
56 448
                } elseif ($typeDef[0] === self::STRUCT_DECLARATION) {
57 448
                    $this->parseComplexStruct($typeDef);
58 448
                } else {
59
                    $this->getGenerator()->getStructs()->addVirtualStruct($structName);
60
                }
61
            }
62 444
            $this->structHasBeenDefined($type);
63
        }
64 444
    }
65 444
    /**
66 444
     * @param array $typeDef
67 444
     */
68 444
    protected function parseComplexStruct($typeDef)
69 444
    {
70 444
        $typeDefCount = count($typeDef);
71 444
        if ($typeDefCount > 3) {
72 112
            for ($i = 2; $i < $typeDefCount; $i += 2) {
73
                $structParamType = str_replace(self::ANY_XML_DECLARATION, self::ANY_XML_TYPE, $typeDef[$i]);
74 444
                $structParamName = $typeDef[$i + 1];
75
                $this->getGenerator()->getStructs()->addStructWithAttribute($typeDef[1], $structParamName, $structParamType);
76
            }
77
        } else {
78
            $this->getGenerator()->getStructs()->addStruct($typeDef[1]);
79
        }
80
    }
81
    /**
82
     * union types are passed such as ",dateTime,time" or ",PMS_ResStatusType,TransactionActionType,UpperCaseAlphaLength1to2"
83
     * @param array $typeDef
84 448
     */
85
    protected function parseUnionStruct($typeDef)
86 448
    {
87 448
        $typeDefCount = count($typeDef);
88 448
        if ($typeDefCount === 3) {
89 448
            $unionName = $typeDef[1];
90 448
            $unionTypes = array_filter(explode(',', $typeDef[2]), function ($type) {
91 448
                return !empty($type);
92 448
            });
93 448
            sort($unionTypes);
94 448
            $this->getGenerator()->getStructs()->addUnionStruct($unionName, $unionTypes);
95 448
        }
96 448
    }
97 448
    /**
98
     * Remove useless break line, tabs
99
     * Remove curly braces
100
     * Remove brackets
101
     * Adds space before semicolon to parse it
102
     * Remove duplicated spaces
103 448
     * @param string $type
104
     * @return string
105 448
     */
106
    protected static function cleanType($type)
107
    {
108
        $type = str_replace(array(
109
            "\r",
110
            "\n",
111 448
            "\t",
112
            '{',
113 448
            '}',
114 448
            '[',
115
            ']',
116
            ';',
117
        ), '', $type);
118
        $type = preg_replace('/[\s]+/', ' ', $type);
119
        return trim($type);
120 448
    }
121
    /**
122 448
     * @param string $type
123
     * @return boolean
124
     */
125
    private function isStructDefined($type)
126
    {
127
        return in_array(self::typeSignature($type), $this->definedStructs);
128
    }
129
    /**
130
     * @param string $type
131
     * @return Structs
132
     */
133
    private function structHasBeenDefined($type)
134
    {
135
        $this->definedStructs[] = self::typeSignature($type);
136
        return $this;
137
    }
138
    /**
139
     * @param string $type
140
     * @return string
141
     */
142
    private static function typeSignature($type)
143
    {
144
        return md5($type);
145
    }
146
}
147