Completed
Push — master ( c10acf...f9d335 )
by
unknown
05:55
created

SingleValueChecker   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 106
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSupportedContextTypes() 0 7 1
A getDefaultContextTypes() 0 7 1
B checkConstraint() 0 28 3
A getViolationMessage() 0 9 2
A checkConstraintParameters() 0 10 2
1
<?php
2
3
namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker;
4
5
use Wikibase\DataModel\Entity\PropertyId;
6
use WikibaseQuality\ConstraintReport\Constraint;
7
use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker;
8
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
9
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterException;
10
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
11
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ValueCountCheckerHelper;
12
use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage;
13
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
14
use Wikibase\DataModel\Statement\Statement;
15
16
/**
17
 * @author BP2014N1
18
 * @license GPL-2.0-or-later
19
 */
20
class SingleValueChecker implements ConstraintChecker {
21
22
	/**
23
	 * @var ConstraintParameterParser
24
	 */
25
	private $constraintParameterParser;
26
27
	/**
28
	 * @var ValueCountCheckerHelper
29
	 */
30
	private $valueCountCheckerHelper;
31
32
	public function __construct(
33
		ConstraintParameterParser $constraintParameterParser
34
	) {
35
		$this->constraintParameterParser = $constraintParameterParser;
36
		$this->valueCountCheckerHelper = new ValueCountCheckerHelper();
37
	}
38
39
	/**
40
	 * @codeCoverageIgnore This method is purely declarative.
41
	 */
42
	public function getSupportedContextTypes() {
43
		return [
44
			Context::TYPE_STATEMENT => CheckResult::STATUS_COMPLIANCE,
45
			Context::TYPE_QUALIFIER => CheckResult::STATUS_COMPLIANCE,
46
			Context::TYPE_REFERENCE => CheckResult::STATUS_COMPLIANCE,
47
		];
48
	}
49
50
	/**
51
	 * @codeCoverageIgnore This method is purely declarative.
52
	 */
53
	public function getDefaultContextTypes() {
54
		return [
55
			Context::TYPE_STATEMENT,
56
			Context::TYPE_QUALIFIER,
57
			Context::TYPE_REFERENCE,
58
		];
59
	}
60
61
	/**
62
	 * Checks 'Single value' constraint.
63
	 *
64
	 * @param Context $context
65
	 * @param Constraint $constraint
66
	 *
67
	 * @throws ConstraintParameterException
68
	 * @return CheckResult
69
	 */
70
	public function checkConstraint( Context $context, Constraint $constraint ) {
71
		if ( $context->getSnakRank() === Statement::RANK_DEPRECATED ) {
72
			return new CheckResult( $context, $constraint, [], CheckResult::STATUS_DEPRECATED );
73
		}
74
75
		$parameters = [];
76
77
		$separators = $this->constraintParameterParser->parseSeparatorsParameter(
78
			$constraint->getConstraintParameters()
79
		);
80
		$parameters['separator'] = $separators;
81
82
		$propertyId = $context->getSnak()->getPropertyId();
83
		$propertyCount = $this->valueCountCheckerHelper->getPropertyCount(
84
			$context->getSnakGroup( Context::GROUP_NON_DEPRECATED, $separators ),
85
			$propertyId
86
		);
87
88
		if ( $propertyCount > 1 ) {
89
			$message = $this->getViolationMessage( $separators, $propertyId );
90
			$status = CheckResult::STATUS_VIOLATION;
91
		} else {
92
			$message = null;
93
			$status = CheckResult::STATUS_COMPLIANCE;
94
		}
95
96
		return new CheckResult( $context, $constraint, $parameters, $status, $message );
97
	}
98
99
	/**
100
	 * @param PropertyId[] $separators
101
	 * @param PropertyId $propertyId
102
	 * @return ViolationMessage
103
	 */
104
	private function getViolationMessage( array $separators, PropertyId $propertyId ) {
105
		$messageKey = $separators === [] ?
106
			'wbqc-violation-message-single-value' :
107
			'wbqc-violation-message-single-value-separators';
108
109
		return ( new ViolationMessage( $messageKey ) )
110
			->withEntityId( $propertyId )
111
			->withEntityIdList( $separators );
112
	}
113
114
	public function checkConstraintParameters( Constraint $constraint ) {
115
		$constraintParameters = $constraint->getConstraintParameters();
116
		$exceptions = [];
117
		try {
118
			$this->constraintParameterParser->parseSeparatorsParameter( $constraintParameters );
119
		} catch ( ConstraintParameterException $e ) {
120
			$exceptions[] = $e;
121
		}
122
		return $exceptions;
123
	}
124
125
}
126