Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
77.78% |
28 / 36 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
LabelInLanguageChecker | |
77.78% |
28 / 36 |
|
75.00% |
3 / 4 |
11.10 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSupportedContextTypes | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
getDefaultContextTypes | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
getSupportedEntityTypes | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
checkConstraint | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |||
checkConstraintParameters | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker; |
4 | |
5 | use Wikibase\DataModel\Statement\Statement; |
6 | use Wikibase\DataModel\Term\LabelsProvider; |
7 | use WikibaseQuality\ConstraintReport\Constraint; |
8 | use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker; |
9 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context; |
10 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterException; |
11 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser; |
12 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage; |
13 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult; |
14 | use WikibaseQuality\ConstraintReport\Role; |
15 | |
16 | /** |
17 | * @license GPL-2.0-or-later |
18 | */ |
19 | class LabelInLanguageChecker implements ConstraintChecker { |
20 | /** |
21 | * @var ConstraintParameterParser |
22 | */ |
23 | private $constraintParameterParser; |
24 | |
25 | public function __construct( ConstraintParameterParser $constraintParameterParser ) { |
26 | $this->constraintParameterParser = $constraintParameterParser; |
27 | } |
28 | |
29 | /** |
30 | * @codeCoverageIgnore This method is purely declarative. |
31 | */ |
32 | public function getSupportedContextTypes() { |
33 | return [ |
34 | Context::TYPE_STATEMENT => CheckResult::STATUS_COMPLIANCE, |
35 | Context::TYPE_QUALIFIER => CheckResult::STATUS_COMPLIANCE, |
36 | Context::TYPE_REFERENCE => CheckResult::STATUS_COMPLIANCE, |
37 | ]; |
38 | } |
39 | |
40 | /** |
41 | * @codeCoverageIgnore This method is purely declarative. |
42 | */ |
43 | public function getDefaultContextTypes() { |
44 | return [ |
45 | Context::TYPE_STATEMENT, |
46 | Context::TYPE_QUALIFIER, |
47 | Context::TYPE_REFERENCE, |
48 | ]; |
49 | } |
50 | |
51 | public function getSupportedEntityTypes() { |
52 | return [ |
53 | 'item' => CheckResult::STATUS_COMPLIANCE, |
54 | 'property' => CheckResult::STATUS_COMPLIANCE, |
55 | 'lexeme' => CheckResult::STATUS_NOT_IN_SCOPE, |
56 | 'form' => CheckResult::STATUS_NOT_IN_SCOPE, |
57 | 'sense' => CheckResult::STATUS_NOT_IN_SCOPE, |
58 | 'mediainfo' => CheckResult::STATUS_NOT_IN_SCOPE, |
59 | ]; |
60 | } |
61 | |
62 | /** |
63 | * Checks 'Language' constraint. |
64 | * |
65 | * @param Context $context |
66 | * @param Constraint $constraint |
67 | * |
68 | * @throws ConstraintParameterException |
69 | * @return CheckResult |
70 | */ |
71 | public function checkConstraint( Context $context, Constraint $constraint ): CheckResult { |
72 | if ( $context->getSnakRank() === Statement::RANK_DEPRECATED ) { |
73 | return new CheckResult( $context, $constraint, CheckResult::STATUS_DEPRECATED ); |
74 | } |
75 | |
76 | $constraintParameters = $constraint->getConstraintParameters(); |
77 | |
78 | $languages = $this->constraintParameterParser->parseLanguageParameter( |
79 | $constraintParameters, |
80 | $constraint->getConstraintTypeItemId() |
81 | ); |
82 | |
83 | $status = CheckResult::STATUS_VIOLATION; |
84 | $message = ( new ViolationMessage( 'wbqc-violation-message-label-lacking' ) ) |
85 | ->withEntityId( $context->getSnak()->getPropertyId(), Role::PREDICATE ) |
86 | ->withLanguages( $languages ); |
87 | |
88 | /** @var LabelsProvider $entity */ |
89 | $entity = $context->getEntity(); |
90 | '@phan-var LabelsProvider $entity'; |
91 | |
92 | foreach ( $languages as $language ) { |
93 | if ( $entity->getLabels()->hasTermForLanguage( $language ) ) { |
94 | $message = null; |
95 | $status = CheckResult::STATUS_COMPLIANCE; |
96 | break; |
97 | } |
98 | } |
99 | |
100 | return new CheckResult( $context, $constraint, $status, $message ); |
101 | } |
102 | |
103 | public function checkConstraintParameters( Constraint $constraint ): array { |
104 | $constraintParameters = $constraint->getConstraintParameters(); |
105 | $exceptions = []; |
106 | try { |
107 | $this->constraintParameterParser->parseLanguageParameter( |
108 | $constraintParameters, |
109 | $constraint->getConstraintTypeItemId() |
110 | ); |
111 | } catch ( ConstraintParameterException $e ) { |
112 | $exceptions[] = $e; |
113 | } |
114 | return $exceptions; |
115 | } |
116 | |
117 | } |