Conditions | 24 |
Paths | 922 |
Total Lines | 155 |
Code Lines | 77 |
Lines | 4 |
Ratio | 2.58 % |
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 if ( !defined( 'ABSPATH' ) ) exit; |
||
68 | public function parse() |
||
69 | { |
||
70 | $parents = array(); |
||
71 | |||
72 | // flag : are we in a <pre> Tag ? |
||
73 | $tagPreIn = false; |
||
74 | |||
75 | // action to use for each line of the content of a <pre> Tag |
||
76 | $tagPreBr = array( |
||
77 | 'name' => 'br', |
||
78 | 'close' => false, |
||
79 | 'param' => array( |
||
80 | 'style' => array(), |
||
81 | 'num' => 0 |
||
82 | ) |
||
83 | ); |
||
84 | |||
85 | // tag that can be not closed |
||
86 | $tagsNotClosed = array( |
||
87 | 'br', 'hr', 'img', 'col', |
||
88 | 'input', 'link', 'option', |
||
89 | 'circle', 'ellipse', 'path', 'rect', 'line', 'polygon', 'polyline' |
||
90 | ); |
||
91 | |||
92 | // search the HTML tags |
||
93 | $tmp = array(); |
||
94 | $this->_searchCode($tmp); |
||
95 | |||
96 | // all the actions to do |
||
97 | $actions = array(); |
||
98 | |||
99 | // foreach part of the HTML code |
||
100 | foreach ($tmp as $part) { |
||
101 | // if it is a tag code |
||
102 | if ($part[0]=='code') { |
||
103 | // analise the HTML code |
||
104 | $res = $this->_analiseCode($part[1]); |
||
105 | |||
106 | // if it is a real HTML tag |
||
107 | if ($res) { |
||
108 | // save the current posistion in the HTML code |
||
109 | $res['html_pos'] = $part[2]; |
||
110 | |||
111 | // if the tag must be closed |
||
112 | if (!in_array($res['name'], $tagsNotClosed)) { |
||
113 | // if it is a closure tag |
||
114 | if ($res['close']) { |
||
115 | // HTML validation |
||
116 | if (count($parents)<1) |
||
117 | throw new HTML2PDF_exception(3, $res['name'], $this->getHtmlErrorCode($res['html_pos'])); |
||
118 | else if ($parents[count($parents)-1]!=$res['name']) |
||
119 | throw new HTML2PDF_exception(4, $parents, $this->getHtmlErrorCode($res['html_pos'])); |
||
120 | else |
||
121 | unset($parents[count($parents)-1]); |
||
122 | } else { |
||
123 | // if it is a autoclosed tag |
||
124 | if ($res['autoclose']) { |
||
125 | // save the opened tag |
||
126 | $actions[] = $res; |
||
127 | |||
128 | // prepare the closed tag |
||
129 | $res['params'] = array(); |
||
130 | $res['close'] = true; |
||
131 | } |
||
132 | // else :add a child for validation |
||
133 | else |
||
134 | $parents[count($parents)] = $res['name']; |
||
135 | } |
||
136 | |||
137 | // if it is a <pre> tag (or <code> tag) not auclosed => update the flag |
||
138 | if (($res['name']=='pre' || $res['name']=='code') && !$res['autoclose']) { |
||
139 | $tagPreIn = !$res['close']; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | // save the actions to convert |
||
144 | $actions[] = $res; |
||
145 | } else { // else (it is not a real HTML tag => we transform it in Texte |
||
146 | $part[0]='txt'; |
||
147 | } |
||
148 | } |
||
149 | // if it is text |
||
150 | if ($part[0]=='txt') { |
||
151 | // if we are not in a <pre> tag |
||
152 | if (!$tagPreIn) { |
||
153 | // save the action |
||
154 | $actions[] = array( |
||
155 | 'name' => 'write', |
||
156 | 'close' => false, |
||
157 | 'param' => array('txt' => $this->_prepareTxt($part[1])), |
||
158 | ); |
||
159 | } else { // else (if we are in a <pre> tag) |
||
160 | // prepare the text |
||
161 | $part[1] = str_replace("\r", '', $part[1]); |
||
162 | $part[1] = explode("\n", $part[1]); |
||
163 | |||
164 | // foreach line of the text |
||
165 | foreach ($part[1] as $k => $txt) { |
||
166 | // transform the line |
||
167 | $txt = str_replace("\t", self::HTML_TAB, $txt); |
||
168 | $txt = str_replace(' ', ' ', $txt); |
||
169 | |||
170 | // add a break line |
||
171 | if ($k>0) $actions[] = $tagPreBr; |
||
172 | |||
173 | // save the action |
||
174 | $actions[] = array( |
||
175 | 'name' => 'write', |
||
176 | 'close' => false, |
||
177 | 'param' => array('txt' => $this->_prepareTxt($txt, false)), |
||
178 | ); |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | // for each indentified action, we have to clean up the begin and the end of the texte |
||
185 | // based on tags that surround it |
||
186 | |||
187 | // list of the tags to clean |
||
188 | $tagsToClean = array( |
||
189 | 'page', 'page_header', 'page_footer', 'form', |
||
190 | 'table', 'thead', 'tfoot', 'tr', 'td', 'th', 'br', |
||
191 | 'div', 'hr', 'p', 'ul', 'ol', 'li', |
||
192 | 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', |
||
193 | 'bookmark', 'fieldset', 'legend', |
||
194 | 'draw', 'circle', 'ellipse', 'path', 'rect', 'line', 'g', 'polygon', 'polyline', |
||
195 | 'option' |
||
196 | ); |
||
197 | |||
198 | // foreach action |
||
199 | $nb = count($actions); |
||
200 | for ($k=0; $k<$nb; $k++) { |
||
201 | // if it is a Text |
||
202 | if ($actions[$k]['name']=='write') { |
||
203 | // if the tag before the text is a tag to clean => ltrim on the text |
||
204 | View Code Duplication | if ($k>0 && in_array($actions[$k-1]['name'], $tagsToClean)) |
|
205 | $actions[$k]['param']['txt'] = ltrim($actions[$k]['param']['txt']); |
||
206 | |||
207 | // if the tag after the text is a tag to clean => rtrim on the text |
||
208 | View Code Duplication | if ($k<$nb-1 && in_array($actions[$k+1]['name'], $tagsToClean)) |
|
209 | $actions[$k]['param']['txt'] = rtrim($actions[$k]['param']['txt']); |
||
210 | |||
211 | // if the text is empty => remove the action |
||
212 | if (!strlen($actions[$k]['param']['txt'])) |
||
213 | unset($actions[$k]); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | // if we are not on the level 0 => HTML validator ERROR |
||
218 | if (count($parents)) throw new HTML2PDF_exception(5, $parents); |
||
219 | |||
220 | // save the actions to do |
||
221 | $this->code = array_values($actions); |
||
222 | } |
||
223 | |||
520 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..