Conditions | 12 |
Paths | 80 |
Total Lines | 147 |
Code Lines | 105 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | public function testToXML() |
||
35 | { |
||
36 | $data = array( |
||
37 | 'international' => array( |
||
38 | 'product' => 'bpack World Express Pro', |
||
39 | 'options' => array( |
||
40 | array( |
||
41 | 'common:infoNextDay' => array( |
||
42 | '@attributes' => array( |
||
43 | 'language' => 'NL', |
||
44 | ), |
||
45 | 'common:emailAddress' => '[email protected]', |
||
46 | ), |
||
47 | ), |
||
48 | ), |
||
49 | 'receiver' => array( |
||
50 | 'name' => 'Tijs Verkoyen', |
||
51 | 'company' => 'Sumo Coders', |
||
52 | 'address' => array( |
||
53 | 'streetName' => 'Afrikalaan', |
||
54 | 'number' => '289', |
||
55 | 'box' => '3', |
||
56 | 'postalCode' => '9000', |
||
57 | 'locality' => 'Gent', |
||
58 | 'countryCode' => 'BE', |
||
59 | ), |
||
60 | 'emailAddress' => '[email protected]', |
||
61 | 'phoneNumber' => '+32 9 395 02 51', |
||
62 | ), |
||
63 | 'parcelWeight' => 2000, |
||
64 | 'customsInfo' => array( |
||
65 | 'parcelValue' => 700, |
||
66 | 'contentDescription' => 'BOOK', |
||
67 | 'shipmentType' => 'DOCUMENTS', |
||
68 | 'parcelReturnInstructions' => 'RTS', |
||
69 | 'privateAddress' => false, |
||
70 | ), |
||
71 | ), |
||
72 | ); |
||
73 | |||
74 | $expectedDocument = self::createDomDocument(); |
||
75 | $nationalBox = $expectedDocument->createElement('internationalBox'); |
||
76 | $expectedDocument->appendChild($nationalBox); |
||
77 | $international = $expectedDocument->createElement('international:international'); |
||
78 | $nationalBox->appendChild($international); |
||
79 | $international->appendChild( |
||
80 | $expectedDocument->createElement('international:product', $data['international']['product']) |
||
81 | ); |
||
82 | $options = $expectedDocument->createElement('international:options'); |
||
83 | foreach ($data['international']['options'] as $value) { |
||
84 | foreach ($value as $key2 => $value2) { |
||
85 | $element = $expectedDocument->createElement($key2); |
||
86 | foreach ($value2 as $key3 => $value3) { |
||
87 | if ($key3 == '@attributes') { |
||
88 | foreach ($value3 as $key4 => $value4) { |
||
89 | $element->setAttribute($key4, $value4); |
||
90 | } |
||
91 | } else { |
||
92 | $element->appendChild( |
||
93 | $expectedDocument->createElement($key3, $value3) |
||
94 | ); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | $options->appendChild($element); |
||
99 | } |
||
100 | } |
||
101 | $international->appendChild($options); |
||
102 | $receiver = $expectedDocument->createElement('international:receiver'); |
||
103 | $international->appendChild($receiver); |
||
104 | foreach ($data['international']['receiver'] as $key => $value) { |
||
105 | $key = 'common:' . $key; |
||
106 | if ($key == 'common:address') { |
||
107 | $address = $expectedDocument->createElement($key); |
||
108 | foreach ($value as $key2 => $value2) { |
||
109 | $key2 = 'common:' . $key2; |
||
110 | $address->appendChild( |
||
111 | $expectedDocument->createElement($key2, $value2) |
||
112 | ); |
||
113 | } |
||
114 | $receiver->appendChild($address); |
||
115 | } else { |
||
116 | $receiver->appendChild( |
||
117 | $expectedDocument->createElement($key, $value) |
||
118 | ); |
||
119 | } |
||
120 | } |
||
121 | $international->appendChild( |
||
122 | $expectedDocument->createElement('international:parcelWeight', $data['international']['parcelWeight']) |
||
123 | ); |
||
124 | $customsInfo = $expectedDocument->createElement('international:customsInfo'); |
||
125 | foreach ($data['international']['customsInfo'] as $key => $value) { |
||
126 | if ($key == 'privateAddress') { |
||
127 | $value = ($value) ? 'true' : 'false'; |
||
128 | } |
||
129 | $customsInfo->appendChild( |
||
130 | $expectedDocument->createElement('international:' . $key, $value) |
||
131 | ); |
||
132 | } |
||
133 | $international->appendChild($customsInfo); |
||
134 | |||
135 | $actualDocument = self::createDomDocument(); |
||
136 | $address = new Address( |
||
137 | $data['international']['receiver']['address']['streetName'], |
||
138 | $data['international']['receiver']['address']['number'], |
||
139 | $data['international']['receiver']['address']['box'], |
||
140 | $data['international']['receiver']['address']['postalCode'], |
||
141 | $data['international']['receiver']['address']['locality'], |
||
142 | $data['international']['receiver']['address']['countryCode'] |
||
143 | ); |
||
144 | |||
145 | $receiver = new Receiver(); |
||
146 | $receiver->setName($data['international']['receiver']['name']); |
||
147 | $receiver->setCompany($data['international']['receiver']['company']); |
||
148 | $receiver->setAddress($address); |
||
149 | $receiver->setEmailAddress($data['international']['receiver']['emailAddress']); |
||
150 | $receiver->setPhoneNumber($data['international']['receiver']['phoneNumber']); |
||
151 | |||
152 | $customsInfo = new CustomsInfo(); |
||
153 | $customsInfo->setParcelValue($data['international']['customsInfo']['parcelValue']); |
||
154 | $customsInfo->setContentDescription($data['international']['customsInfo']['contentDescription']); |
||
155 | $customsInfo->setShipmentType($data['international']['customsInfo']['shipmentType']); |
||
156 | $customsInfo->setParcelReturnInstructions($data['international']['customsInfo']['parcelReturnInstructions']); |
||
157 | $customsInfo->setPrivateAddress($data['international']['customsInfo']['privateAddress']); |
||
158 | |||
159 | $messaging = new Messaging( |
||
160 | 'infoNextDay', |
||
161 | $data['international']['options'][0]['common:infoNextDay']['@attributes']['language'], |
||
162 | $data['international']['options'][0]['common:infoNextDay']['common:emailAddress'] |
||
163 | ); |
||
164 | |||
165 | $international = new International(); |
||
166 | $international->setProduct($data['international']['product']); |
||
167 | $international->setReceiver($receiver); |
||
168 | $international->setParcelWeight($data['international']['parcelWeight']); |
||
169 | $international->setCustomsInfo($customsInfo); |
||
170 | |||
171 | $international->addOption($messaging); |
||
172 | |||
173 | // I know, the line below is kinda bogus, but it will make sure all code is tested |
||
174 | $international->setOptions(array($messaging)); |
||
175 | |||
176 | $actualDocument->appendChild( |
||
177 | $international->toXML($actualDocument) |
||
178 | ); |
||
179 | |||
180 | $this->assertSame($expectedDocument->saveXML(), $actualDocument->saveXML()); |
||
181 | } |
||
203 |