| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace WMDE\HamcrestHtml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Hamcrest\Description; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Hamcrest\Matcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class AttributeMatcher extends TagMatcher { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	 * @var Matcher|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	private $attributeNameMatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 * @var Matcher|string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	private $valueMatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @param Matcher|string $attributeName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 * @return self | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 	public static function withAttribute( $attributeName ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 		return new static( $attributeName ); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 * @param Matcher|string $attributeNameMatcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	public function __construct( $attributeNameMatcher ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		$this->attributeNameMatcher = $attributeNameMatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * @param Matcher|string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * @return AttributeMatcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	public function havingValue( $value ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		// TODO: Throw exception if value is set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$result = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		$result->valueMatcher = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  | 	public function describeTo( Description $description ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  | 		$description->appendText( 'with attribute ' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 2 |  | 		if ( $this->attributeNameMatcher instanceof Matcher ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			$description->appendDescriptionOf( $this->attributeNameMatcher ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  | 			$description->appendValue( $this->attributeNameMatcher ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  | 		if ( $this->valueMatcher !== null ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  | 			$description->appendText( ' having value ' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  | 			if ( $this->attributeNameMatcher instanceof Matcher ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 				$description->appendDescriptionOf( $this->valueMatcher ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  | 				$description->appendValue( $this->valueMatcher ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 * @param \DOMElement $item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * @param Description $mismatchDescription | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 6 |  | 	protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 6 |  | 		if ( $this->valueMatcher === null ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 2 |  | 			if ( $this->attributeNameMatcher instanceof Matcher ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 				/** @var \DOMAttr $attribute */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 				foreach ( $item->attributes as $attribute ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 					if ( $this->attributeNameMatcher->matches( $attribute->name ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 						return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  | 				return $item->hasAttribute( $this->attributeNameMatcher ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  | 			if ( $this->attributeNameMatcher instanceof Matcher ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 				if ( $this->valueMatcher instanceof Matcher ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 					/** @var \DOMAttr $attribute */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 90 |  | View Code Duplication | 					foreach ( $item->attributes as $attribute ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 						if ( $this->attributeNameMatcher->matches( $attribute->name ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 							&& $this->valueMatcher->matches( $attribute->value ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 						) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 							return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 						} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 				} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 					/** @var \DOMAttr $attribute */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 99 |  | View Code Duplication | 					foreach ( $item->attributes as $attribute ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 						if ( $this->attributeNameMatcher->matches( $attribute->name ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 							&& $attribute->value === $this->valueMatcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 						) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 							return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 						} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 4 |  | 				if ( $this->valueMatcher instanceof Matcher ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 					return $item->hasAttribute( $this->attributeNameMatcher ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 						&& $this->valueMatcher->matches( $item->getAttribute( $this->attributeNameMatcher ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 				} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 4 |  | 					return $item->getAttribute( $this->attributeNameMatcher ) === $this->valueMatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                        
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.