Completed
Pull Request — master (#2)
by Robbert
09:06
created

Parser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 73
Duplicated Lines 12.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 15
c 2
b 0
f 1
lcom 1
cbo 2
dl 9
loc 73
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 3
A parse() 0 15 4
A parsePartial() 0 10 2
B parseFull() 0 28 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace arc\html;
4
5
class Parser 
6
{
7
	public $options = [
8
		'libxml_options' => 0
9
	];
10
11 View Code Duplication
	public function __construct( $options = array() )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
	{
13
		$optionList = [ 'libxml_options' ];
14
		foreach( $options as $option => $optionValue ) {
15
			if ( in_array( $option, $optionList ) ) {
16
				$this->{$option} = $optionValue;
17
			}
18
		}
19
	}
20
21
	public function parse( $html, $encoding = 'UTF-8' )
22
	{
23
		if ( !$html ) {
24
			return \arc\html\Proxy( null );
25
		}
26
		if ( $html instanceof Proxy ) { // already parsed
27
			return $html;
28
		}
29
		$html = (string) $html;
30
		if ( stripos($html, '<html>')!==false ) {
31
			return $this->parseFull( $html, $encoding );
32
		} else {		
33
			return $this->parsePartial( $html, $encoding );
34
		}
35
	}
36
37
	private function parsePartial( $html, $encoding = 'UTF-8' )
38
	{
39
		$result = $this->parseFull( '<div id="ArcPartialHTML">'.$html.'</div>', $encoding );
40
		if ( $result ) {
41
			$result = new \arc\html\Proxy( $result->find('#ArcPartialHTML')[0]->children(), $this );
42
		} else {
43
			throw new \arc\Exception('parse error');
44
		}
45
		return $result;
46
	}
47
48
	private function parseFull( $html, $encoding = 'UTF-8')
49
	{
50
		$dom = new \DomDocument('1.0', $encoding);
51
		$prefix = '';
52
		if ( isset($encoding) ) {
53
			$prefix  = "<head id='ar_html_parser_encoding_header'><meta charset='$encoding'></head>";
54
		}
55
		libxml_disable_entity_loader(); // prevents XXE attacks
56
		$prevErrorSetting = libxml_use_internal_errors(true);
57
		if ( $dom->loadHTML( $prefix . $html, $this->options['libxml_options'] ) ) {
58
			if ( isset($encoding) ) {
59
				$elm  = $dom->getElementById('ar_html_parser_encoding_header');
60
				if ( isset($elm) ) {
61
					$elm->parentNode->removeChild($elm);
62
				}
63
			}
64
			libxml_use_internal_errors( $prevErrorSetting );
65
			return new \arc\html\Proxy( simplexml_import_dom( $dom ), $this );
66
		}
67
		$errors = libxml_get_errors();
68
		libxml_clear_errors();
69
		libxml_use_internal_errors( $prevErrorSetting );
70
		$message = 'Incorrect html passed.';
71
		foreach ( $errors as $error ) {
72
			$message .= "\nline: ".$error->line."; column: ".$error->column."; ".$error->message;
73
		}
74
		throw new \arc\Exception( $message, \arc\exceptions::ILLEGAL_ARGUMENT );
75
	}
76
77
}
78