Total Complexity | 55 |
Total Lines | 256 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 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 |
||
17 | class Parser |
||
18 | { |
||
19 | protected $_file; |
||
20 | |||
21 | protected $_gedcom; |
||
22 | |||
23 | protected $_errorLog = []; |
||
24 | |||
25 | protected $_linesParsed = 0; |
||
26 | |||
27 | protected $_line = ''; |
||
28 | |||
29 | protected $_lineRecord; |
||
30 | |||
31 | protected $_linePieces = 0; |
||
32 | |||
33 | protected $_returnedLine = ''; |
||
34 | |||
35 | public function __construct(Gedcom $gedcom = null) |
||
36 | { |
||
37 | if (!is_null($gedcom)) { |
||
38 | $this->_gedcom = $gedcom; |
||
39 | } else { |
||
40 | $this->_gedcom = new Gedcom(); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | public function forward() |
||
45 | { |
||
46 | // if there was a returned line by back(), set that as our current |
||
47 | // line and blank out the returnedLine variable, otherwise grab |
||
48 | // the next line from the file |
||
49 | |||
50 | if (!empty($this->_returnedLine)) { |
||
51 | $this->_line = $this->_returnedLine; |
||
52 | $this->_returnedLine = ''; |
||
53 | } else { |
||
54 | $this->_line = fgets($this->_file); |
||
55 | $this->_lineRecord = null; |
||
56 | $this->_linesParsed++; |
||
57 | } |
||
58 | |||
59 | return $this; |
||
60 | } |
||
61 | |||
62 | public function back() |
||
63 | { |
||
64 | // our parser object encountered a line it wasn't meant to parse |
||
65 | // store this line for the previous parser to analyze |
||
66 | |||
67 | $this->_returnedLine = $this->_line; |
||
68 | |||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Jump to the next level in the GEDCOM that is <= $level. This will leave the parser at the line above |
||
74 | * this level, such that calling $parser->forward() will result in landing at the correct level. |
||
75 | * |
||
76 | * @param int $level |
||
77 | */ |
||
78 | public function skipToNextLevel($level) |
||
79 | { |
||
80 | $currentDepth = 999; |
||
81 | |||
82 | while ($currentDepth > $level) { |
||
83 | $this->forward(); |
||
84 | $record = $this->getCurrentLineRecord(); |
||
85 | $currentDepth = (int) $record[0]; |
||
86 | } |
||
87 | |||
88 | $this->back(); |
||
89 | } |
||
90 | |||
91 | public function getGedcom() |
||
92 | { |
||
93 | return $this->_gedcom; |
||
94 | } |
||
95 | |||
96 | public function eof() |
||
97 | { |
||
98 | return feof($this->_file); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | public function parseMultiLineRecord() |
||
105 | { |
||
106 | $record = $this->getCurrentLineRecord(); |
||
107 | |||
108 | $depth = (int) $record[0]; |
||
109 | $data = isset($record[2]) ? trim($record[2]) : ''; |
||
110 | |||
111 | $this->forward(); |
||
112 | |||
113 | while (!$this->eof()) { |
||
114 | $record = $this->getCurrentLineRecord(); |
||
115 | $recordType = strtoupper(trim($record[1])); |
||
116 | $currentDepth = (int) $record[0]; |
||
117 | |||
118 | if ($currentDepth <= $depth) { |
||
119 | $this->back(); |
||
120 | break; |
||
121 | } |
||
122 | |||
123 | switch ($recordType) { |
||
124 | case 'CONT': |
||
125 | $data .= "\n"; |
||
126 | |||
127 | if (isset($record[2])) { |
||
128 | $data .= trim($record[2]); |
||
129 | } |
||
130 | break; |
||
131 | case 'CONC': |
||
132 | if (isset($record[2])) { |
||
133 | $data .= ' '.trim($record[2]); |
||
134 | } |
||
135 | break; |
||
136 | default: |
||
137 | $this->back(); |
||
138 | break 2; |
||
139 | } |
||
140 | |||
141 | $this->forward(); |
||
142 | } |
||
143 | |||
144 | return $data; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return string The current line |
||
149 | */ |
||
150 | public function getCurrentLine() |
||
151 | { |
||
152 | return $this->_line; |
||
153 | } |
||
154 | |||
155 | public function getCurrentLineRecord($pieces = 3) |
||
173 | } |
||
174 | |||
175 | protected function logError($error) |
||
176 | { |
||
177 | $this->_errorLog[] = $error; |
||
178 | } |
||
179 | |||
180 | public function logUnhandledRecord($additionalInfo = '') |
||
181 | { |
||
182 | $this->logError( |
||
183 | $this->_linesParsed.': (Unhandled) '.trim(implode('|', $this->getCurrentLineRecord())). |
||
|
|||
184 | (!empty($additionalInfo) ? ' - '.$additionalInfo : '') |
||
185 | ); |
||
186 | } |
||
187 | |||
188 | public function logSkippedRecord($additionalInfo = '') |
||
189 | { |
||
190 | $this->logError( |
||
191 | $this->_linesParsed.': (Skipping) '.trim(implode('|', $this->getCurrentLineRecord())). |
||
192 | (!empty($additionalInfo) ? ' - '.$additionalInfo : '') |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | public function getErrors() |
||
199 | } |
||
200 | |||
201 | public function normalizeIdentifier($identifier) |
||
202 | { |
||
203 | $identifier = trim($identifier); |
||
204 | |||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param string $fileName |
||
210 | * |
||
211 | * @return Gedcom |
||
212 | */ |
||
213 | public function parse($fileName) |
||
273 | } |
||
274 | } |
||
275 |