Conditions | 10 |
Paths | 29 |
Total Lines | 107 |
Code Lines | 53 |
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 |
||
82 | public function process($html) { |
||
83 | // normalize whitespace |
||
84 | $html = str_replace(array("\r\n", "\r"), "\n", $html); |
||
85 | |||
86 | // allows preserving entities untouched |
||
87 | $html = str_replace('&', $this->_unique . 'AMP', $html); |
||
88 | |||
89 | $this->_doc = new DOMDocument(); |
||
90 | |||
91 | // parse to DOM, suppressing loadHTML warnings |
||
92 | // http://www.php.net/manual/en/domdocument.loadhtml.php#95463 |
||
93 | libxml_use_internal_errors(true); |
||
94 | |||
95 | // Do not load entities. May be unnecessary, better safe than sorry |
||
96 | $disable_load_entities = libxml_disable_entity_loader(true); |
||
97 | |||
98 | if (!$this->_doc->loadHTML("<html><meta http-equiv='content-type' " |
||
99 | . "content='text/html; charset={$this->encoding}'><body>{$html}</body>" |
||
100 | . "</html>")) { |
||
101 | |||
102 | libxml_disable_entity_loader($disable_load_entities); |
||
103 | return false; |
||
104 | } |
||
105 | |||
106 | libxml_disable_entity_loader($disable_load_entities); |
||
107 | |||
108 | $this->_xpath = new DOMXPath($this->_doc); |
||
109 | // start processing recursively at the BODY element |
||
110 | $nodeList = $this->_xpath->query('//body[1]'); |
||
111 | $this->addParagraphs($nodeList->item(0)); |
||
112 | |||
113 | // serialize back to HTML |
||
114 | $html = $this->_doc->saveHTML(); |
||
115 | |||
116 | // Note: we create <autop> elements, which will later be converted to paragraphs |
||
117 | |||
118 | // split AUTOPs into multiples at /\n\n+/ |
||
119 | $html = preg_replace('/(' . $this->_unique . 'NL){2,}/', '</autop><autop>', $html); |
||
120 | $html = str_replace(array($this->_unique . 'BR', $this->_unique . 'NL', '<br>'), |
||
121 | '<br />', |
||
122 | $html); |
||
123 | $html = str_replace('<br /></autop>', '</autop>', $html); |
||
124 | |||
125 | // re-parse so we can handle new AUTOP elements |
||
126 | |||
127 | // Do not load entities. May be unnecessary, better safe than sorry |
||
128 | $disable_load_entities = libxml_disable_entity_loader(true); |
||
129 | |||
130 | if (!$this->_doc->loadHTML($html)) { |
||
131 | libxml_disable_entity_loader($disable_load_entities); |
||
132 | return false; |
||
133 | } |
||
134 | |||
135 | libxml_disable_entity_loader($disable_load_entities); |
||
136 | |||
137 | // must re-create XPath object after DOM load |
||
138 | $this->_xpath = new DOMXPath($this->_doc); |
||
139 | |||
140 | // strip AUTOPs that only have comments/whitespace |
||
141 | foreach ($this->_xpath->query('//autop') as $autop) { |
||
142 | /* @var DOMElement $autop */ |
||
143 | $hasContent = false; |
||
144 | if (trim($autop->textContent) !== '') { |
||
145 | $hasContent = true; |
||
146 | } else { |
||
147 | foreach ($autop->childNodes as $node) { |
||
148 | if ($node->nodeType === XML_ELEMENT_NODE) { |
||
149 | $hasContent = true; |
||
150 | break; |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | if (!$hasContent) { |
||
155 | // mark to be later replaced w/ preg_replace (faster than moving nodes out) |
||
156 | $autop->setAttribute("r", "1"); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | // If a DIV contains a single AUTOP, remove it |
||
161 | foreach ($this->_xpath->query('//div') as $el) { |
||
162 | /* @var DOMElement $el */ |
||
163 | $autops = $this->_xpath->query('./autop', $el); |
||
164 | if ($autops->length === 1) { |
||
165 | $firstAutop = $autops->item(0); |
||
166 | /* @var DOMElement $firstAutop */ |
||
167 | $firstAutop->setAttribute("r", "1"); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | $html = $this->_doc->saveHTML(); |
||
172 | |||
173 | // trim to the contents of BODY |
||
174 | $bodyStart = strpos($html, '<body>'); |
||
175 | $bodyEnd = strpos($html, '</body>', $bodyStart + 6); |
||
176 | $html = substr($html, $bodyStart + 6, $bodyEnd - $bodyStart - 6); |
||
177 | |||
178 | // strip AUTOPs that should be removed |
||
179 | $html = preg_replace('@<autop r="1">(.*?)</autop>@', '\\1', $html); |
||
180 | |||
181 | // commit to converting AUTOPs to Ps |
||
182 | $html = str_replace('<autop>', "\n<p>", $html); |
||
183 | $html = str_replace('</autop>', "</p>\n", $html); |
||
184 | |||
185 | $html = str_replace('<br>', '<br />', $html); |
||
186 | $html = str_replace($this->_unique . 'AMP', '&', $html); |
||
187 | return $html; |
||
188 | } |
||
189 | |||
326 |
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..