Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.66% |
26 / 29 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
IntegerChecker | |
89.66% |
26 / 29 |
|
60.00% |
3 / 5 |
17.32 | |
0.00% |
0 / 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 | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
3.01 | |||
checkSnak | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
8.30 | |||
isInteger | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getViolationMessage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
checkConstraintParameters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker; |
4 | |
5 | use DataValues\DecimalValue; |
6 | use DataValues\QuantityValue; |
7 | use DataValues\UnboundedQuantityValue; |
8 | use Wikibase\DataModel\Snak\PropertyValueSnak; |
9 | use WikibaseQuality\ConstraintReport\Constraint; |
10 | use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker; |
11 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context; |
12 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage; |
13 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult; |
14 | use WikibaseQuality\ConstraintReport\Role; |
15 | |
16 | /** |
17 | * @author Amir Sarabadani |
18 | * @license GPL-2.0-or-later |
19 | */ |
20 | class IntegerChecker implements ConstraintChecker { |
21 | |
22 | /** |
23 | * @codeCoverageIgnore This method is purely declarative. |
24 | */ |
25 | public function getSupportedContextTypes() { |
26 | return self::ALL_CONTEXT_TYPES_SUPPORTED; |
27 | } |
28 | |
29 | /** |
30 | * @codeCoverageIgnore This method is purely declarative. |
31 | */ |
32 | public function getDefaultContextTypes() { |
33 | return Context::ALL_CONTEXT_TYPES; |
34 | } |
35 | |
36 | /** @codeCoverageIgnore This method is purely declarative. */ |
37 | public function getSupportedEntityTypes() { |
38 | return self::ALL_ENTITY_TYPES_SUPPORTED; |
39 | } |
40 | |
41 | public function checkConstraint( Context $context, Constraint $constraint ) { |
42 | $snak = $context->getSnak(); |
43 | |
44 | if ( !$snak instanceof PropertyValueSnak ) { |
45 | // nothing to check |
46 | return new CheckResult( $context, $constraint, CheckResult::STATUS_COMPLIANCE ); |
47 | } |
48 | |
49 | $violationMessage = $this->checkSnak( $snak ); |
50 | |
51 | return new CheckResult( |
52 | $context, |
53 | $constraint, |
54 | $violationMessage === null ? |
55 | CheckResult::STATUS_COMPLIANCE : |
56 | CheckResult::STATUS_VIOLATION, |
57 | $violationMessage |
58 | ); |
59 | } |
60 | |
61 | /** |
62 | * @param PropertyValueSnak $snak |
63 | * @return ViolationMessage|null |
64 | */ |
65 | public function checkSnak( PropertyValueSnak $snak ) { |
66 | $dataValue = $snak->getDataValue(); |
67 | |
68 | if ( $dataValue instanceof DecimalValue ) { |
69 | if ( !$this->isInteger( $dataValue ) ) { |
70 | return $this->getViolationMessage( 'wbqc-violation-message-integer', $snak ); |
71 | } |
72 | } elseif ( $dataValue instanceof UnboundedQuantityValue ) { |
73 | if ( !$this->isInteger( $dataValue->getAmount() ) ) { |
74 | return $this->getViolationMessage( 'wbqc-violation-message-integer', $snak ); |
75 | } elseif ( |
76 | $dataValue instanceof QuantityValue && ( |
77 | !$this->isInteger( $dataValue->getLowerBound() ) || |
78 | !$this->isInteger( $dataValue->getUpperBound() ) |
79 | ) |
80 | ) { |
81 | return $this->getViolationMessage( 'wbqc-violation-message-integer-bounds', $snak ); |
82 | } |
83 | } |
84 | |
85 | return null; |
86 | } |
87 | |
88 | /** |
89 | * @param DecimalValue $decimalValue |
90 | * @return bool |
91 | */ |
92 | private function isInteger( DecimalValue $decimalValue ) { |
93 | return $decimalValue->getTrimmed()->getFractionalPart() === ''; |
94 | } |
95 | |
96 | /** |
97 | * @param string $messageKey |
98 | * @param PropertyValueSnak $snak |
99 | * @return ViolationMessage |
100 | */ |
101 | private function getViolationMessage( $messageKey, PropertyValueSnak $snak ) { |
102 | return ( new ViolationMessage( $messageKey ) ) |
103 | ->withEntityId( $snak->getPropertyId(), Role::CONSTRAINT_PROPERTY ) |
104 | ->withDataValue( $snak->getDataValue() ); |
105 | } |
106 | |
107 | public function checkConstraintParameters( Constraint $constraint ) { |
108 | // no parameters |
109 | return []; |
110 | } |
111 | |
112 | } |