1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker; |
4
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Snak\Snak; |
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\Message\ViolationMessage; |
12
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult; |
13
|
|
|
use WikibaseQuality\ConstraintReport\Role; |
14
|
|
|
use Wikibase\DataModel\Statement\Statement; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author BP2014N1 |
18
|
|
|
* @license GPL-2.0-or-later |
19
|
|
|
*/ |
20
|
|
|
class QualifiersChecker implements ConstraintChecker { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ConstraintParameterParser |
24
|
|
|
*/ |
25
|
|
|
private $constraintParameterParser; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ConstraintParameterParser $constraintParameterParser |
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
|
|
ConstraintParameterParser $constraintParameterParser |
32
|
|
|
) { |
33
|
|
|
$this->constraintParameterParser = $constraintParameterParser; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @codeCoverageIgnore This method is purely declarative. |
38
|
|
|
*/ |
39
|
|
|
public function getSupportedContextTypes() { |
40
|
|
|
return [ |
41
|
|
|
Context::TYPE_STATEMENT => CheckResult::STATUS_COMPLIANCE, |
42
|
|
|
Context::TYPE_QUALIFIER => CheckResult::STATUS_NOT_IN_SCOPE, |
43
|
|
|
Context::TYPE_REFERENCE => CheckResult::STATUS_NOT_IN_SCOPE, |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @codeCoverageIgnore This method is purely declarative. |
49
|
|
|
*/ |
50
|
|
|
public function getDefaultContextTypes() { |
51
|
|
|
return [ |
52
|
|
|
Context::TYPE_STATEMENT, |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Checks 'Qualifiers' constraint. |
58
|
|
|
* |
59
|
|
|
* @param Context $context |
60
|
|
|
* @param Constraint $constraint |
61
|
|
|
* |
62
|
|
|
* @throws ConstraintParameterException |
63
|
|
|
* @return CheckResult |
64
|
|
|
*/ |
65
|
|
|
public function checkConstraint( Context $context, Constraint $constraint ) { |
66
|
|
|
if ( $context->getSnakRank() === Statement::RANK_DEPRECATED ) { |
67
|
|
|
return new CheckResult( $context, $constraint, [], CheckResult::STATUS_DEPRECATED ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$parameters = []; |
71
|
|
|
$constraintParameters = $constraint->getConstraintParameters(); |
72
|
|
|
|
73
|
|
|
$properties = $this->constraintParameterParser->parsePropertiesParameter( $constraintParameters, $constraint->getConstraintTypeItemId() ); |
74
|
|
|
$parameters['property'] = $properties; |
75
|
|
|
|
76
|
|
|
$message = null; |
77
|
|
|
$status = CheckResult::STATUS_COMPLIANCE; |
78
|
|
|
|
79
|
|
|
/** @var Snak $qualifier */ |
80
|
|
|
foreach ( $context->getSnakStatement()->getQualifiers() as $qualifier ) { |
81
|
|
|
$allowedQualifier = false; |
82
|
|
|
foreach ( $properties as $property ) { |
83
|
|
|
if ( $qualifier->getPropertyId()->equals( $property ) ) { |
84
|
|
|
$allowedQualifier = true; |
85
|
|
|
break; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
if ( !$allowedQualifier ) { |
89
|
|
View Code Duplication |
if ( empty( $properties ) || $properties === [ '' ] ) { |
|
|
|
|
90
|
|
|
$message = ( new ViolationMessage( 'wbqc-violation-message-no-qualifiers' ) ) |
91
|
|
|
->withEntityId( $context->getSnak()->getPropertyId(), Role::CONSTRAINT_PROPERTY ); |
92
|
|
|
} else { |
93
|
|
|
$message = ( new ViolationMessage( 'wbqc-violation-message-qualifiers' ) ) |
94
|
|
|
->withEntityId( $context->getSnak()->getPropertyId(), Role::CONSTRAINT_PROPERTY ) |
95
|
|
|
->withEntityId( $qualifier->getPropertyId(), Role::QUALIFIER_PREDICATE ) |
96
|
|
|
->withEntityIdList( $properties, Role::QUALIFIER_PREDICATE ); |
97
|
|
|
} |
98
|
|
|
$status = CheckResult::STATUS_VIOLATION; |
99
|
|
|
break; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return new CheckResult( $context, $constraint, $parameters, $status, $message ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function checkConstraintParameters( Constraint $constraint ) { |
107
|
|
|
$constraintParameters = $constraint->getConstraintParameters(); |
108
|
|
|
$exceptions = []; |
109
|
|
|
try { |
110
|
|
|
$this->constraintParameterParser->parsePropertiesParameter( $constraintParameters, $constraint->getConstraintTypeItemId() ); |
111
|
|
|
} catch ( ConstraintParameterException $e ) { |
112
|
|
|
$exceptions[] = $e; |
113
|
|
|
} |
114
|
|
|
return $exceptions; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
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.