Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
80.00% |
32 / 40 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
LabelInLanguageChecker | |
80.00% |
32 / 40 |
|
75.00% |
3 / 4 |
11.97 | |
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% |
22 / 22 |
|
100.00% |
1 / 1 |
5 | |||
checkConstraintParameters | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker; |
6 | |
7 | use Wikibase\DataModel\Statement\Statement; |
8 | use Wikibase\DataModel\Term\LabelsProvider; |
9 | use WikibaseQuality\ConstraintReport\Constraint; |
10 | use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker; |
11 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context; |
12 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterException; |
13 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser; |
14 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage; |
15 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult; |
16 | use WikibaseQuality\ConstraintReport\Role; |
17 | |
18 | /** |
19 | * @license GPL-2.0-or-later |
20 | */ |
21 | class LabelInLanguageChecker implements ConstraintChecker { |
22 | |
23 | private ConstraintParameterParser $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(): array { |
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(): array { |
44 | return [ |
45 | Context::TYPE_STATEMENT, |
46 | Context::TYPE_QUALIFIER, |
47 | Context::TYPE_REFERENCE, |
48 | ]; |
49 | } |
50 | |
51 | public function getSupportedEntityTypes(): array { |
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 | * @throws ConstraintParameterException |
66 | */ |
67 | public function checkConstraint( Context $context, Constraint $constraint ): CheckResult { |
68 | if ( $context->getSnakRank() === Statement::RANK_DEPRECATED ) { |
69 | return new CheckResult( $context, $constraint, CheckResult::STATUS_DEPRECATED ); |
70 | } |
71 | |
72 | $constraintParameters = $constraint->getConstraintParameters(); |
73 | |
74 | $languages = $this->constraintParameterParser->parseLanguageParameter( |
75 | $constraintParameters, |
76 | $constraint->getConstraintTypeItemId() |
77 | ); |
78 | |
79 | $status = CheckResult::STATUS_VIOLATION; |
80 | $message = ( new ViolationMessage( 'wbqc-violation-message-label-lacking' ) ) |
81 | ->withEntityId( $context->getSnak()->getPropertyId(), Role::PREDICATE ) |
82 | ->withLanguages( $languages ); |
83 | |
84 | /** @var LabelsProvider $entity */ |
85 | $entity = $context->getEntity(); |
86 | '@phan-var LabelsProvider $entity'; |
87 | $labels = $entity->getLabels(); |
88 | |
89 | if ( $labels->hasTermForLanguage( 'mul' ) ) { |
90 | $message = null; |
91 | $status = CheckResult::STATUS_COMPLIANCE; |
92 | } else { |
93 | foreach ( $languages as $language ) { |
94 | if ( $labels->hasTermForLanguage( $language ) ) { |
95 | $message = null; |
96 | $status = CheckResult::STATUS_COMPLIANCE; |
97 | break; |
98 | } |
99 | } |
100 | } |
101 | |
102 | return new CheckResult( $context, $constraint, $status, $message ); |
103 | } |
104 | |
105 | public function checkConstraintParameters( Constraint $constraint ): array { |
106 | $constraintParameters = $constraint->getConstraintParameters(); |
107 | $exceptions = []; |
108 | try { |
109 | $this->constraintParameterParser->parseLanguageParameter( |
110 | $constraintParameters, |
111 | $constraint->getConstraintTypeItemId() |
112 | ); |
113 | } catch ( ConstraintParameterException $e ) { |
114 | $exceptions[] = $e; |
115 | } |
116 | return $exceptions; |
117 | } |
118 | |
119 | } |