Total Complexity | 55 |
Total Lines | 297 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Parser 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.
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 Parser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Parser { |
||
21 | /** |
||
22 | * |
||
23 | */ |
||
24 | protected $_file = null; |
||
25 | |||
26 | /** |
||
27 | * |
||
28 | */ |
||
29 | protected $_gedcom = null; |
||
30 | |||
31 | /** |
||
32 | * |
||
33 | */ |
||
34 | protected $_errorLog = array(); |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | */ |
||
39 | protected $_linesParsed = 0; |
||
40 | |||
41 | /** |
||
42 | * |
||
43 | */ |
||
44 | protected $_line = ''; |
||
45 | |||
46 | /** |
||
47 | * |
||
48 | */ |
||
49 | protected $_lineRecord = null; |
||
50 | |||
51 | /** |
||
52 | * |
||
53 | */ |
||
54 | protected $_linePieces = 0; |
||
55 | |||
56 | /** |
||
57 | * |
||
58 | */ |
||
59 | protected $_returnedLine = ''; |
||
60 | |||
61 | /** |
||
62 | * |
||
63 | */ |
||
64 | public function __construct(\PhpGedcom\Gedcom $gedcom = null) { |
||
65 | if (!is_null($gedcom)) { |
||
66 | $this->_gedcom = $gedcom; |
||
67 | } else { |
||
68 | $this->_gedcom = new \PhpGedcom\Gedcom(); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * |
||
74 | */ |
||
75 | public function forward() { |
||
76 | // if there was a returned line by back(), set that as our current |
||
77 | // line and blank out the returnedLine variable, otherwise grab |
||
78 | // the next line from the file |
||
79 | |||
80 | if (!empty($this->_returnedLine)) { |
||
81 | $this->_line = $this->_returnedLine; |
||
82 | $this->_returnedLine = ''; |
||
83 | } else { |
||
84 | $this->_line = fgets($this->_file); |
||
85 | $this->_lineRecord = null; |
||
86 | $this->_linesParsed++; |
||
87 | } |
||
88 | |||
89 | return $this; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * |
||
94 | */ |
||
95 | public function back() { |
||
96 | // our parser object encountered a line it wasn't meant to parse |
||
97 | // store this line for the previous parser to analyze |
||
98 | |||
99 | $this->_returnedLine = $this->_line; |
||
100 | |||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Jump to the next level in the GEDCOM that is <= $level. This will leave the parser at the line above |
||
106 | * this level, such that calling $parser->forward() will result in landing at the correct level. |
||
107 | * |
||
108 | * @param int $level |
||
109 | */ |
||
110 | public function skipToNextLevel($level) { |
||
111 | $currentDepth = 999; |
||
112 | |||
113 | while ($currentDepth > $level) { |
||
114 | $this->forward(); |
||
115 | $record = $this->getCurrentLineRecord(); |
||
116 | $currentDepth = (int) $record[0]; |
||
117 | } |
||
118 | |||
119 | $this->back(); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * |
||
124 | */ |
||
125 | public function getGedcom() { |
||
126 | return $this->_gedcom; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * |
||
131 | */ |
||
132 | public function eof() { |
||
133 | return feof($this->_file); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public function parseMultiLineRecord() { |
||
141 | $record = $this->getCurrentLineRecord(); |
||
142 | |||
143 | $depth = (int) $record[0]; |
||
144 | $data = isset($record[2]) ? trim($record[2]) : ''; |
||
145 | |||
146 | $this->forward(); |
||
147 | |||
148 | while (!$this->eof()) { |
||
149 | $record = $this->getCurrentLineRecord(); |
||
150 | $recordType = strtoupper(trim($record[1])); |
||
151 | $currentDepth = (int) $record[0]; |
||
152 | |||
153 | if ($currentDepth <= $depth) { |
||
154 | $this->back(); |
||
155 | break; |
||
156 | } |
||
157 | |||
158 | switch ($recordType) { |
||
159 | case 'CONT': |
||
160 | $data .= "\n"; |
||
161 | |||
162 | if (isset($record[2])) { |
||
163 | $data .= trim($record[2]); |
||
164 | } |
||
165 | break; |
||
166 | case 'CONC': |
||
167 | if (isset($record[2])) { |
||
168 | $data .= ' ' . trim($record[2]); |
||
169 | } |
||
170 | break; |
||
171 | default: |
||
172 | $this->back(); |
||
173 | break 2; |
||
174 | } |
||
175 | |||
176 | $this->forward(); |
||
177 | } |
||
178 | |||
179 | return $data; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * |
||
184 | * @return string The current line |
||
185 | */ |
||
186 | public function getCurrentLine() { |
||
187 | return $this->_line; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * |
||
192 | */ |
||
193 | public function getCurrentLineRecord($pieces = 3) { |
||
194 | if (!is_null($this->_lineRecord)) { |
||
195 | if ($this->_linePieces == $pieces) { |
||
196 | return $this->_lineRecord; |
||
197 | } |
||
198 | } |
||
199 | |||
200 | if (empty($this->_line)) { |
||
201 | return false; |
||
202 | } |
||
203 | |||
204 | $line = trim($this->_line); |
||
205 | |||
206 | $this->_lineRecord = explode(' ', $line, $pieces); |
||
207 | $this->_linePieces = $pieces; |
||
208 | |||
209 | return $this->_lineRecord; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * |
||
214 | */ |
||
215 | protected function logError($error) { |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * |
||
221 | */ |
||
222 | public function logUnhandledRecord($additionalInfo = '') { |
||
226 | ); |
||
227 | } |
||
228 | |||
229 | public function logSkippedRecord($additionalInfo = '') { |
||
230 | $this->logError( |
||
231 | $this->_linesParsed . ': (Skipping) ' . trim(implode('|', $this->getCurrentLineRecord())) . |
||
232 | (!empty($additionalInfo) ? ' - ' . $additionalInfo : '') |
||
233 | ); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * |
||
238 | */ |
||
239 | public function getErrors() { |
||
240 | return $this->_errorLog; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * |
||
245 | */ |
||
246 | public function normalizeIdentifier($identifier) { |
||
247 | $identifier = trim($identifier); |
||
248 | $identifier = trim($identifier, '@'); |
||
249 | |||
250 | return $identifier; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * |
||
255 | * @param string $fileName |
||
256 | * @return Gedcom |
||
257 | */ |
||
258 | public function parse($fileName) { |
||
317 | } |
||
318 | } |