DirectChildElementMatcher   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 24
cts 30
cp 0.8
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 3

4 Methods

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