Conditions | 24 |
Paths | 36 |
Total Lines | 112 |
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 |
||
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++) { |
||
|
|||
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) { |
||
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 | |||
254 | } |
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: