1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WMDE\HamcrestHtml; |
4
|
|
|
|
5
|
|
|
use Hamcrest\Description; |
6
|
|
|
use Hamcrest\Matcher; |
7
|
|
|
use Hamcrest\TypeSafeDiagnosingMatcher; |
8
|
|
|
|
9
|
|
|
class ChildElementMatcher extends TypeSafeDiagnosingMatcher { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var Matcher|string|null |
13
|
|
|
*/ |
14
|
|
|
private $matcher; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param Matcher|string|null $elementMatcher |
18
|
|
|
* |
19
|
|
|
* @return self |
20
|
|
|
*/ |
21
|
|
|
public static function havingChild( $elementMatcher = null ) { |
22
|
|
|
return new static( $elementMatcher ); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Matcher|string|null $matcher |
27
|
|
|
*/ |
28
|
|
|
public function __construct( $matcher = null ) { |
29
|
|
|
parent::__construct( \DOMNode::class ); |
30
|
|
|
$this->matcher = $matcher; |
31
|
|
|
} |
32
|
|
|
|
33
|
7 |
View Code Duplication |
public function describeTo( Description $description ) { |
|
|
|
|
34
|
7 |
|
$description->appendText( 'having child ' ); |
35
|
7 |
|
if ( $this->matcher ) { |
36
|
6 |
|
if ( $this->matcher instanceof Matcher ) { |
37
|
6 |
|
$description->appendDescriptionOf( $this->matcher ); |
38
|
6 |
|
} else { |
39
|
|
|
$description->appendValue( $this->matcher ); |
40
|
|
|
} |
41
|
6 |
|
} |
42
|
7 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \DOMDocument|\DOMNode $item |
46
|
|
|
* @param Description $mismatchDescription |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
17 |
|
protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { |
51
|
17 |
|
if ( $item instanceof \DOMDocument ) { |
52
|
16 |
|
$item = $item->documentElement->childNodes->item( 0 ); |
53
|
|
|
/** @var \DOMElement $item */ |
54
|
16 |
|
} |
55
|
17 |
|
$directChildren = $item->childNodes; |
56
|
|
|
|
57
|
17 |
|
if ( $directChildren->length === 0 ) { |
58
|
1 |
|
$mismatchDescription->appendText( 'having no children' ); |
59
|
1 |
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
16 |
|
if ( !$this->matcher ) { |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
16 |
|
if ( $this->matcher instanceof Matcher ) { |
67
|
16 |
|
foreach ( new XmlNodeRecursiveIterator( $directChildren ) as $child ) { |
68
|
16 |
|
if ( $this->matcher->matches( $child ) ) { |
69
|
10 |
|
return true; |
70
|
|
|
} |
71
|
11 |
|
} |
72
|
6 |
|
} else { |
73
|
|
|
if ( $item->getElementsByTagName( $this->matcher )->length !== 0 ) { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
$mismatchDescription->appendText( 'having no children ' )->appendDescriptionOf( $this->matcher ); |
|
|
|
|
79
|
6 |
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
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.