Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
32 / 32 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
PropertyScopeChecker | |
100.00% |
32 / 32 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 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 | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
checkConstraint | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
2 | |||
checkConstraintParameters | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker; |
4 | |
5 | use WikibaseQuality\ConstraintReport\Constraint; |
6 | use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker; |
7 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context; |
8 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterException; |
9 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser; |
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 PropertyScopeChecker implements ConstraintChecker { |
18 | |
19 | /** |
20 | * @var ConstraintParameterParser |
21 | */ |
22 | private $constraintParameterParser; |
23 | |
24 | public function __construct( |
25 | ConstraintParameterParser $constraintParameterParser |
26 | ) { |
27 | $this->constraintParameterParser = $constraintParameterParser; |
28 | } |
29 | |
30 | /** |
31 | * @codeCoverageIgnore This method is purely declarative. |
32 | */ |
33 | public function getSupportedContextTypes() { |
34 | return self::ALL_CONTEXT_TYPES_SUPPORTED; |
35 | } |
36 | |
37 | /** |
38 | * @codeCoverageIgnore This method is purely declarative. |
39 | */ |
40 | public function getDefaultContextTypes() { |
41 | return Context::ALL_CONTEXT_TYPES; |
42 | } |
43 | |
44 | /** @codeCoverageIgnore This method is purely declarative. */ |
45 | public function getSupportedEntityTypes() { |
46 | return self::ALL_ENTITY_TYPES_SUPPORTED; |
47 | } |
48 | |
49 | public function checkConstraint( Context $context, Constraint $constraint ) { |
50 | $constraintParameters = $constraint->getConstraintParameters(); |
51 | $constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
52 | |
53 | $allowedContextTypes = $this->constraintParameterParser->parsePropertyScopeParameter( |
54 | $constraintParameters, |
55 | $constraintTypeItemId |
56 | ); |
57 | |
58 | if ( in_array( $context->getType(), $allowedContextTypes ) ) { |
59 | return new CheckResult( |
60 | $context->getCursor(), |
61 | $constraint, |
62 | CheckResult::STATUS_COMPLIANCE |
63 | ); |
64 | } else { |
65 | return new CheckResult( |
66 | $context->getCursor(), |
67 | $constraint, |
68 | CheckResult::STATUS_VIOLATION, |
69 | ( new ViolationMessage( 'wbqc-violation-message-property-scope' ) ) |
70 | ->withEntityId( $context->getSnak()->getPropertyId() ) |
71 | ->withPropertyScope( $context->getType() ) |
72 | ->withPropertyScopeList( $allowedContextTypes ) |
73 | ); |
74 | } |
75 | } |
76 | |
77 | public function checkConstraintParameters( Constraint $constraint ) { |
78 | $constraintParameters = $constraint->getConstraintParameters(); |
79 | $constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
80 | $exceptions = []; |
81 | try { |
82 | $this->constraintParameterParser->parsePropertyScopeParameter( |
83 | $constraintParameters, |
84 | $constraintTypeItemId |
85 | ); |
86 | } catch ( ConstraintParameterException $e ) { |
87 | $exceptions[] = $e; |
88 | } |
89 | return $exceptions; |
90 | } |
91 | |
92 | } |