Completed
Push — master ( 425b0a...8cb89a )
by
unknown
04:47
created

SingleBestValueChecker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker;
4
5
use Wikibase\DataModel\Statement\Statement;
6
use WikibaseQuality\ConstraintReport\Constraint;
7
use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker;
8
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
9
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ValueCountCheckerHelper;
10
use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage;
11
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
12
13
/**
14
 * @author Lucas Werkmeister
15
 * @license GPL-2.0-or-later
16
 */
17
class SingleBestValueChecker implements ConstraintChecker {
18
19
	/**
20
	 * @var ValueCountCheckerHelper
21
	 */
22
	private $valueCountCheckerHelper;
23
24
	public function __construct() {
25
		$this->valueCountCheckerHelper = new ValueCountCheckerHelper();
26
	}
27
28
	/**
29
	 * @codeCoverageIgnore This method is purely declarative.
30
	 */
31
	public function getSupportedContextTypes() {
32
		return [
33
			Context::TYPE_STATEMENT => CheckResult::STATUS_COMPLIANCE,
34
			Context::TYPE_QUALIFIER => CheckResult::STATUS_COMPLIANCE,
35
			Context::TYPE_REFERENCE => CheckResult::STATUS_COMPLIANCE,
36
		];
37
	}
38
39
	/**
40
	 * @codeCoverageIgnore This method is purely declarative.
41
	 */
42
	public function getDefaultContextTypes() {
43
		return [
44
			Context::TYPE_STATEMENT,
45
			Context::TYPE_QUALIFIER,
46
			Context::TYPE_REFERENCE,
47
		];
48
	}
49
50
	/**
51
	 * Checks 'Single best value' constraint.
52
	 *
53
	 * @param Context $context
54
	 * @param Constraint $constraint
55
	 *
56
	 * @return CheckResult
57
	 */
58
	public function checkConstraint( Context $context, Constraint $constraint ) {
59
		if ( $context->getSnakRank() === Statement::RANK_DEPRECATED ) {
60
			return new CheckResult( $context, $constraint, [], CheckResult::STATUS_DEPRECATED );
61
		}
62
63
		$propertyId = $context->getSnak()->getPropertyId();
64
65
		$parameters = [];
66
67
		$bestRankCount = $this->valueCountCheckerHelper->getPropertyCount(
68
			$context->getSnakGroup( Context::GROUP_BEST_RANK ),
69
			$propertyId
70
		);
71
72
		if ( $bestRankCount > 1 ) {
73
			$nonDeprecatedCount = $this->valueCountCheckerHelper->getPropertyCount(
74
				$context->getSnakGroup( Context::GROUP_NON_DEPRECATED ),
75
				$propertyId
76
			);
77
			if ( $bestRankCount === $nonDeprecatedCount ) {
78
				$messageKey = 'wbqc-violation-message-single-best-value-no-preferred';
79
			} else {
80
				$messageKey = 'wbqc-violation-message-single-best-value-multi-preferred';
81
			}
82
			$message = new ViolationMessage( $messageKey );
83
			$status = CheckResult::STATUS_VIOLATION;
84
		} else {
85
			$message = null;
86
			$status = CheckResult::STATUS_COMPLIANCE;
87
		}
88
89
		return new CheckResult( $context, $constraint, $parameters, $status, $message );
90
	}
91
92
	public function checkConstraintParameters( Constraint $constraint ) {
93
		// no parameters
94
		return [];
95
	}
96
97
}
98