These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace WikibaseQuality\ExternalValidation\CrossCheck\Comparer; |
||
| 4 | |||
| 5 | use DataValues\DataValue; |
||
| 6 | use DataValues\MonolingualTextValue; |
||
| 7 | use Wikibase\DataModel\Entity\EntityId; |
||
| 8 | use Wikibase\DataModel\Entity\EntityIdValue; |
||
| 9 | use Wikibase\TermIndexEntry; |
||
| 10 | use Wikibase\TermIndex; |
||
| 11 | use Wikimedia\Assert\Assert; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @fixme This class does not compares EntityIdValues! |
||
| 15 | * @author BP2014N1 |
||
| 16 | * @license GPL-2.0-or-later |
||
| 17 | */ |
||
| 18 | class EntityIdValueComparer implements DataValueComparer { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var TermIndex |
||
| 22 | */ |
||
| 23 | private $termIndex; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var StringComparer |
||
| 27 | */ |
||
| 28 | private $stringComparer; |
||
| 29 | |||
| 30 | public function __construct( TermIndex $termIndex, StringComparer $stringComparer ) { |
||
| 31 | $this->termIndex = $termIndex; |
||
| 32 | $this->stringComparer = $stringComparer; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @see DataValueComparer::compare |
||
| 37 | * |
||
| 38 | * @param DataValue $value |
||
| 39 | * @param DataValue $comparativeValue |
||
| 40 | * |
||
| 41 | * @return string|null One of the ComparisonResult::STATUS_... constants. |
||
| 42 | */ |
||
| 43 | public function compare( DataValue $value, DataValue $comparativeValue ) { |
||
| 44 | Assert::parameterType( EntityIdValue::class, $value, '$value' ); |
||
| 45 | Assert::parameterType( MonolingualTextValue::class, $comparativeValue, '$comparativeValue' ); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var EntityIdValue $value |
||
| 49 | * @var MonolingualTextValue $comparativeValue |
||
| 50 | */ |
||
| 51 | |||
| 52 | $entityId = $value->getEntityId(); |
||
| 53 | $language = $comparativeValue->getLanguageCode(); |
||
| 54 | $terms = $this->getTerms( $entityId, $language ); |
||
| 55 | |||
| 56 | if ( $terms ) { |
||
|
0 ignored issues
–
show
|
|||
| 57 | return $this->stringComparer->compareWithArray( $comparativeValue->getText(), $terms ); |
||
| 58 | } |
||
| 59 | |||
| 60 | return null; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Retrieves terms (label and aliases) of a given entity in the given language |
||
| 65 | * |
||
| 66 | * @param EntityId $entityId |
||
| 67 | * @param string $language |
||
| 68 | * |
||
| 69 | * @return string[] |
||
| 70 | */ |
||
| 71 | private function getTerms( EntityId $entityId, $language ) { |
||
| 72 | $terms = $this->termIndex->getTermsOfEntity( |
||
| 73 | $entityId, |
||
| 74 | [ |
||
| 75 | TermIndexEntry::TYPE_LABEL, |
||
| 76 | TermIndexEntry::TYPE_ALIAS |
||
| 77 | ], |
||
| 78 | [ $language ] |
||
| 79 | ); |
||
| 80 | |||
| 81 | return array_map( |
||
| 82 | function( TermIndexEntry $term ) { |
||
| 83 | return $term->getText(); |
||
| 84 | }, |
||
| 85 | $terms |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @see DataValueComparer::canCompare |
||
| 91 | * |
||
| 92 | * @param DataValue $value |
||
| 93 | * @param DataValue $comparativeValue |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | public function canCompare( DataValue $value, DataValue $comparativeValue ) { |
||
| 97 | return $value instanceof EntityIdValue && $comparativeValue instanceof MonolingualTextValue; |
||
| 98 | } |
||
| 99 | |||
| 100 | } |
||
| 101 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.