Conditions | 42 |
Paths | 1873 |
Total Lines | 176 |
Code Lines | 96 |
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 |
||
66 | protected function convertProperty(Component\VCard $input, Component\VCard $output, Property $property, $targetVersion) |
||
67 | { |
||
68 | // Skipping these, those are automatically added. |
||
69 | if (in_array($property->name, ['VERSION', 'PRODID'])) { |
||
70 | return; |
||
71 | } |
||
72 | |||
73 | $parameters = $property->parameters(); |
||
74 | $valueType = null; |
||
75 | if (isset($parameters['VALUE'])) { |
||
76 | $valueType = $parameters['VALUE']->getValue(); |
||
77 | unset($parameters['VALUE']); |
||
78 | } |
||
79 | if (!$valueType) { |
||
80 | $valueType = $property->getValueType(); |
||
81 | } |
||
82 | if (Document::VCARD30 !== $targetVersion && 'PHONE-NUMBER' === $valueType) { |
||
83 | $valueType = null; |
||
84 | } |
||
85 | $newProperty = $output->createProperty( |
||
86 | $property->name, |
||
87 | $property->getParts(), |
||
88 | [], // parameters will get added a bit later. |
||
89 | $valueType |
||
90 | ); |
||
91 | |||
92 | if (Document::VCARD30 === $targetVersion) { |
||
93 | if ($property instanceof Property\Uri && in_array($property->name, ['PHOTO', 'LOGO', 'SOUND'])) { |
||
94 | $newProperty = $this->convertUriToBinary($output, $newProperty); |
||
95 | } elseif ($property instanceof Property\VCard\DateAndOrTime) { |
||
96 | // In vCard 4, the birth year may be optional. This is not the |
||
97 | // case for vCard 3. Apple has a workaround for this that |
||
98 | // allows applications that support Apple's extension still |
||
99 | // omit birthyears in vCard 3, but applications that do not |
||
100 | // support this, will just use a random birthyear. We're |
||
101 | // choosing 1604 for the birthyear, because that's what apple |
||
102 | // uses. |
||
103 | $parts = DateTimeParser::parseVCardDateTime($property->getValue()); |
||
104 | if (is_null($parts['year'])) { |
||
105 | $newValue = '1604-' . $parts['month'] . '-' . $parts['date']; |
||
106 | $newProperty->setValue($newValue); |
||
107 | $newProperty['X-APPLE-OMIT-YEAR'] = '1604'; |
||
108 | } |
||
109 | |||
110 | if ('ANNIVERSARY' == $newProperty->name) { |
||
111 | // Microsoft non-standard anniversary |
||
112 | $newProperty->name = 'X-ANNIVERSARY'; |
||
113 | |||
114 | // We also need to add a new apple property for the same |
||
115 | // purpose. This apple property needs a 'label' in the same |
||
116 | // group, so we first need to find a groupname that doesn't |
||
117 | // exist yet. |
||
118 | $x = 1; |
||
119 | while ($output->select('ITEM' . $x . '.')) { |
||
120 | ++$x; |
||
121 | } |
||
122 | $output->add('ITEM' . $x . '.X-ABDATE', $newProperty->getValue(), ['VALUE' => 'DATE-AND-OR-TIME']); |
||
123 | $output->add('ITEM' . $x . '.X-ABLABEL', '_$!<Anniversary>!$_'); |
||
124 | } |
||
125 | } elseif ('KIND' === $property->name) { |
||
126 | switch (strtolower($property->getValue())) { |
||
127 | case 'org': |
||
128 | // vCard 3.0 does not have an equivalent to KIND:ORG, |
||
129 | // but apple has an extension that means the same |
||
130 | // thing. |
||
131 | $newProperty = $output->createProperty('X-ABSHOWAS', 'COMPANY'); |
||
132 | break; |
||
133 | |||
134 | case 'individual': |
||
135 | // Individual is implicit, so we skip it. |
||
136 | return; |
||
137 | |||
138 | case 'group': |
||
139 | // OS X addressbook property |
||
140 | $newProperty = $output->createProperty('X-ADDRESSBOOKSERVER-KIND', 'GROUP'); |
||
141 | break; |
||
142 | } |
||
143 | } elseif ('MEMBER' === $property->name) { |
||
144 | $newProperty = $output->createProperty('X-ADDRESSBOOKSERVER-MEMBER', $property->getValue()); |
||
145 | } |
||
146 | } elseif (Document::VCARD40 === $targetVersion) { |
||
147 | // These properties were removed in vCard 4.0 |
||
148 | if (in_array($property->name, ['NAME', 'MAILER', 'LABEL', 'CLASS'])) { |
||
149 | return; |
||
150 | } |
||
151 | |||
152 | if ($property instanceof Property\Binary) { |
||
153 | $newProperty = $this->convertBinaryToUri($output, $newProperty, $parameters); |
||
154 | } elseif ($property instanceof Property\VCard\DateAndOrTime && isset($parameters['X-APPLE-OMIT-YEAR'])) { |
||
155 | // If a property such as BDAY contained 'X-APPLE-OMIT-YEAR', |
||
156 | // then we're stripping the year from the vcard 4 value. |
||
157 | $parts = DateTimeParser::parseVCardDateTime($property->getValue()); |
||
158 | if ($parts['year'] === $property['X-APPLE-OMIT-YEAR']->getValue()) { |
||
159 | $newValue = '--' . $parts['month'] . '-' . $parts['date']; |
||
160 | $newProperty->setValue($newValue); |
||
161 | } |
||
162 | |||
163 | // Regardless if the year matched or not, we do need to strip |
||
164 | // X-APPLE-OMIT-YEAR. |
||
165 | unset($parameters['X-APPLE-OMIT-YEAR']); |
||
166 | } |
||
167 | switch ($property->name) { |
||
168 | case 'X-ABSHOWAS': |
||
169 | if ('COMPANY' === strtoupper($property->getValue())) { |
||
170 | $newProperty = $output->createProperty('KIND', 'ORG'); |
||
171 | } |
||
172 | break; |
||
173 | case 'X-ADDRESSBOOKSERVER-KIND': |
||
174 | if ('GROUP' === strtoupper($property->getValue())) { |
||
175 | $newProperty = $output->createProperty('KIND', 'GROUP'); |
||
176 | } |
||
177 | break; |
||
178 | case 'X-ADDRESSBOOKSERVER-MEMBER': |
||
179 | $newProperty = $output->createProperty('MEMBER', $property->getValue()); |
||
180 | break; |
||
181 | case 'X-ANNIVERSARY': |
||
182 | $newProperty->name = 'ANNIVERSARY'; |
||
183 | // If we already have an anniversary property with the same |
||
184 | // value, ignore. |
||
185 | foreach ($output->select('ANNIVERSARY') as $anniversary) { |
||
186 | if ($anniversary->getValue() === $newProperty->getValue()) { |
||
187 | return; |
||
188 | } |
||
189 | } |
||
190 | break; |
||
191 | case 'X-ABDATE': |
||
192 | // Find out what the label was, if it exists. |
||
193 | if (!$property->group) { |
||
194 | break; |
||
195 | } |
||
196 | $label = $input->{$property->group . '.X-ABLABEL'}; |
||
197 | |||
198 | // We only support converting anniversaries. |
||
199 | if (!$label || '_$!<Anniversary>!$_' !== $label->getValue()) { |
||
200 | break; |
||
201 | } |
||
202 | |||
203 | // If we already have an anniversary property with the same |
||
204 | // value, ignore. |
||
205 | foreach ($output->select('ANNIVERSARY') as $anniversary) { |
||
206 | if ($anniversary->getValue() === $newProperty->getValue()) { |
||
207 | return; |
||
208 | } |
||
209 | } |
||
210 | $newProperty->name = 'ANNIVERSARY'; |
||
211 | break; |
||
212 | // Apple's per-property label system. |
||
213 | case 'X-ABLABEL': |
||
214 | if ('_$!<Anniversary>!$_' === $newProperty->getValue()) { |
||
215 | // We can safely remove these, as they are converted to |
||
216 | // ANNIVERSARY properties. |
||
217 | return; |
||
218 | } |
||
219 | break; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | // set property group |
||
224 | $newProperty->group = $property->group; |
||
225 | |||
226 | if (Document::VCARD40 === $targetVersion) { |
||
227 | $this->convertParameters40($newProperty, $parameters); |
||
228 | } else { |
||
229 | $this->convertParameters30($newProperty, $parameters); |
||
230 | } |
||
231 | |||
232 | // Lastly, we need to see if there's a need for a VALUE parameter. |
||
233 | // |
||
234 | // We can do that by instantiating a empty property with that name, and |
||
235 | // seeing if the default valueType is identical to the current one. |
||
236 | $tempProperty = $output->createProperty($newProperty->name); |
||
237 | if ($tempProperty->getValueType() !== $newProperty->getValueType()) { |
||
238 | $newProperty['VALUE'] = $newProperty->getValueType(); |
||
239 | } |
||
240 | |||
241 | $output->add($newProperty); |
||
242 | } |
||
424 |