Completed
Pull Request — master (#2)
by Robbert
07:15
created

Parser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 77
Duplicated Lines 11.69 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 72%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 15
c 3
b 0
f 1
lcom 1
cbo 2
dl 9
loc 77
ccs 36
cts 50
cp 0.72
rs 10

4 Methods

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