iCalEasyReader   C
last analyzed

Complexity

Total Complexity 55

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 238
rs 6
c 0
b 0
f 0
wmc 55
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
F load() 0 112 24
A addType() 0 11 3
B addItem() 0 41 9
A processMultivalue() 0 13 3
A concatItem() 0 13 3
A transformLine() 0 7 1
B mb_eregi_replace_all() 0 16 7
A ignoreLine() 0 6 3
A isLineContinuation() 0 4 2

How to fix   Complexity   

Complex Class

Complex classes like iCalEasyReader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use iCalEasyReader, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Finder\Readers;
4
5
/**
6
 * iCalEasyReader is an easy to understood class, loads a "ics" format string and returns an array with the traditional iCal fields
7
 *
8
 * @category	Parser
9
 * @author		Matias Perrone <matias.perrone at gmail dot com>
10
 * @author		Timo Henke <[email protected]> (Some ideas taken partially from iCalParse on http://www.phpclasses.org/)
11
 * @license		http://www.opensource.org/licenses/mit-license.php MIT License
12
 * @version		1.4.1
13
 * @param	string	$data	ics file string content
14
 * @param	array|false	$data $makeItEasy	the idea is to convert this "keys" into the "values", converting the DATE and DATE-TIME values to the respective DateTime type of PHP, also all the keys are lowercased
15
 * @return	array|false
16
 */
17
class iCalEasyReader
18
{
19
	private $ical = null;
20
	private $_lastitem = null;
21
	private $_ignored = false;
22
23
	public function &load($data)
24
	{
25
		$this->ical = false;
26
		$regex_opt = 'mid';
27
28
		// Lines in the string
29
		$lines = mb_split('[\r\n]+', $data);
30
31
		// Delete empty ones
32
		$last = count($lines);
33
		for ($i = 0; $i < $last; $i++) {
34
			if (trim($lines[$i]) == "")
35
				unset($lines[$i]);
36
		}
37
		$lines = array_values($lines);
38
39
		// First and last items
40
		$first = 0;
41
		$last = count($lines) - 1;
42
43
		if (!(mb_ereg_match('^BEGIN:VCALENDAR', $lines[$first], $regex_opt) and mb_ereg_match('^END:VCALENDAR', $lines[$last], $regex_opt))) {
44
			$first = null;
45
			$last = null;
46
			foreach ($lines as $i => &$line) {
47
				if (mb_ereg_match('^BEGIN:VCALENDAR', $line, $regex_opt))
48
					$first = $i;
49
50
				if (mb_ereg_match('^END:VCALENDAR', $line, $regex_opt)) {
51
					$last = $i;
52
					break;
53
				}
54
			}
55
		}
56
57
		// Procesing
58
		if (!is_null($first) and !is_null($last)) {
59
			$lines = array_slice($lines, $first + 1, ($last - $first - 1), true);
60
61
			$group = null;
62
			$parentgroup = null;
63
			$this->ical = [];
64
			$lines = array_values($lines);
65
			for ($ix = 0; $ix < count($lines); $ix++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
66
				$line = $lines[$ix];
67
68
				// There are cases like "ATTENDEE" that may take several lines.
69
				if (!$this->isLineContinuation($line)) {
70
					if ($this->ignoreLine($line)) {
71
						continue;
72
					}
73
					$this->_ignored = false;
74
75
					$pattern = '^(BEGIN|END)\:(.+)$'; // (VALARM|VTODO|VJOURNAL|VEVENT|VFREEBUSY|VCALENDAR|DAYLIGHT|VTIMEZONE|STANDARD)
76
					mb_ereg_search_init($line);
77
					$regs = mb_ereg_search_regs($pattern, $regex_opt);
78
					if ($regs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $regs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
79
						// $regs
80
						// 0 => BEGIN:VEVENT
81
						// 1 => BEGIN
82
						// 2 => VEVENT
83
						switch ($regs[1]) {
84
							case 'BEGIN':
85
								if (!is_null($group))
86
									$parentgroup = $group;
87
88
								$group = trim($regs[2]);
89
90
								// Adding new values to groups
91
								if (is_null($parentgroup)) {
92
									if (!array_key_exists($group, $this->ical))
93
										$this->ical[$group] = [null];
94
									else
95
										$this->ical[$group][] = null;
96
								} else {
97
									if (!array_key_exists($parentgroup, $this->ical))
98
										$this->ical[$parentgroup] = [$group => [null]];
99
100
									if (!array_key_exists($group, $this->ical[$parentgroup]))
101
										$this->ical[$parentgroup][$group] = [null];
102
									else
103
										$this->ical[$parentgroup][$group][] = null;
104
								}
105
106
								break;
107
							case 'END':
108
								if (is_null($group))
109
									$parentgroup = null;
110
111
								$group = null;
112
								break;
113
						}
114
						continue;
115
					}
116
117
					$r = $line;
118
					$concat = $lines[++$ix] ?? false;
119
					while ($this->isLineContinuation($concat)) {
120
						$r .= substr($concat, 1);
121
						$concat = $lines[++$ix] ?? false;
122
					}
123
					$ix--;
124
					if ($r !== $line)
125
						$line = $r;
126
127
					$this->addItem($line, $group, $parentgroup);
128
				} else
129
					$this->concatItem($line);
130
			};
131
		}
132
133
		return $this->ical;
134
	}
135
136
	public function addType(&$value, $item)
137
	{
138
		$type = explode('=', $item);
139
140
		if (count($type) > 1 and $type[0] == 'VALUE')
141
			$value['type'] = $type[1];
142
		else
143
			$value[$type[0]] = $type[1];
144
145
		return $value;
146
	}
147
148
	public function addItem($line, $group, $parentgroup)
149
	{
150
		$line = $this->transformLine($line);
151
		$item = explode(':', $line, 2);
152
153
		if (!array_key_exists(1, $item)) {
154
			trigger_error("Unexpected Line error. Possible Corruption. Line " . strlen($line) . ":" . PHP_EOL . $line . PHP_EOL, E_USER_NOTICE);
155
			return;
156
		}
157
158
		// If $group is null is an independent value
159
		if (is_null($group)) {
160
			$this->ical[$item[0]] = (count($item) > 1 ? $item[1] : null);
161
			$this->_lastitem = &$this->ical[$item[0]];
162
		}
163
		// If $group is set then is an item of a group
164
		else {
165
			$subitem = explode(';', $item[0], 2);
166
			if (count($subitem) == 1)
167
				$value = (count($item) > 1 ? $item[1] : null);
168
			else {
169
				$value = ['value' => $item[1]];
170
				if (strpos($subitem[1], ";") !== false)
171
					$value += $this->processMultivalue($subitem[1]);
172
				else
173
					$this->addType($value, $subitem[1]);
174
			}
175
176
			// Multi value
177
			if (is_string($value))
178
				$this->processMultivalue($value);
179
180
			if (is_null($parentgroup)) {
181
				$this->ical[$group][count($this->ical[$group]) - 1][$subitem[0]] = $value;
182
				$this->_lastitem = &$this->ical[$group][count($this->ical[$group]) - 1][$subitem[0]];
183
			} else {
184
				$this->ical[$parentgroup][$group][count($this->ical[$parentgroup][$group]) - 1][$subitem[0]] = $value;
185
				$this->_lastitem = &$this->ical[$parentgroup][$group][count($this->ical[$parentgroup][$group]) - 1][$subitem[0]];
186
			}
187
		}
188
	}
189
190
	public function processMultivalue(&$value)
191
	{
192
		$z = explode(';', $value);
193
		if (count($z) > 1) {
194
			$value = [];
195
			foreach ($z as &$v) {
196
				$t = explode('=', $v);
197
				$value[$t[0]] = $t[count($t) - 1];
198
			}
199
		}
200
		unset($z);
201
		return $value;
202
	}
203
204
	public function concatItem($line)
205
	{
206
		if (!$this->_ignored) {
207
			$line = mb_substr($line, 1);
208
			if (is_array($this->_lastitem)) {
209
				$line = $this->transformLine($this->_lastitem['value'] . $line);
210
				$this->_lastitem['value'] = $line;
211
			} else {
212
				$line = $this->transformLine($this->_lastitem . $line);
213
				$this->_lastitem = $line;
214
			}
215
		}
216
	}
217
218
	public function transformLine($line)
219
	{
220
		$patterns = ['\\\\[n]', '\\\\[t]', '\\\\,', '\\\\;'];
221
		$replacements = ["\n", "\t", ",", ";"];
222
223
		return $this->mb_eregi_replace_all($patterns, $replacements, $line);
224
	}
225
226
	public function mb_eregi_replace_all($pattern, $replacement, $string)
227
	{
228
		if (is_array($pattern) and is_array($replacement)) {
229
			foreach ($pattern as $i => $pattern) {
230
				if (array_key_exists($i, $replacement))
231
					$substitute = $replacement[$i];
232
				else
233
					$substitute = '';
234
235
				$string = mb_eregi_replace($pattern, $substitute, $string);
236
			}
237
		} elseif (is_string($pattern) and is_string($replacement))
238
			$string = mb_eregi_replace($pattern, $replacement, $string);
239
240
		return $string;
241
	}
242
243
	public function ignoreLine($line)
244
	{
245
		$ignore = substr($line, 0, 2) == 'X-' or trim($line) == '';
246
		$this->_ignored = $this->isLineContinuation($line) ? $this->_ignored : $ignore;
247
		return $ignore;
248
	}
249
250
	public function isLineContinuation($line)
251
	{
252
		return $line !== false && in_array($line[0], [" ", "\t"]);
253
	}
254
}