RootElementMatcher   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A havingRootElement() 0 3 1
A __construct() 0 4 1
A describeTo() 0 6 2
A matchesSafelyWithDiagnosticDescription() 0 23 4
1
<?php
2
3
namespace WMDE\HamcrestHtml;
4
5
use Hamcrest\Description;
6
use Hamcrest\Matcher;
7
use Hamcrest\TypeSafeDiagnosingMatcher;
8
9
class RootElementMatcher extends TypeSafeDiagnosingMatcher {
10
11
	/**
12
	 * @var Matcher
13
	 */
14
	private $tagMatcher;
15
16
	/**
17
	 * @param Matcher|null $tagMatcher
18
	 *
19
	 * @return static
20
	 */
21 1
	public static function havingRootElement( Matcher $tagMatcher = null ) {
22 1
		return new static( $tagMatcher );
23
	}
24
25 1
	public function __construct( Matcher $tagMatcher = null ) {
26 1
		parent::__construct( self::TYPE_OBJECT, \DOMDocument::class );
27 1
		$this->tagMatcher = $tagMatcher;
28 1
	}
29
30 4
	public function describeTo( Description $description ) {
31 4
		$description->appendText( 'having root element ' );
32 4
		if ( $this->tagMatcher ) {
33 3
			$description->appendDescriptionOf( $this->tagMatcher );
34 3
		}
35 4
	}
36
37
	/**
38
	 * @param \DOMDocument $item
39
	 * @param Description $mismatchDescription
40
	 *
41
	 * @return bool
42
	 */
43 6
	protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
44 6
		$DOMNodeList = $item->documentElement->childNodes->item( 0 )->childNodes;
45 6
		if ( $DOMNodeList->length > 1 ) {
46
			// TODO Test this description
47 1
			$mismatchDescription->appendText( 'having ' . $DOMNodeList->length . ' root elements ' );
48 1
			return false;
49
		}
50
51 5
		$target = $DOMNodeList->item( 0 );
52 5
		if ( !$target ) {
53
			// TODO Reproduce?
54
			$mismatchDescription->appendText( 'having no root elements ' );
55
			return false;
56
		}
57
58 5
		if ( $this->tagMatcher ) {
59 4
			$mismatchDescription->appendText( 'root element ' );
60 4
			$this->tagMatcher->describeMismatch( $target, $mismatchDescription );
61 4
			return $this->tagMatcher->matches( $target );
62
		}
63
64 1
		return true;
65
	}
66
67
}
68