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 [ |
27
|
|
|
Context::TYPE_STATEMENT => CheckResult::STATUS_COMPLIANCE, |
28
|
|
|
Context::TYPE_QUALIFIER => CheckResult::STATUS_COMPLIANCE, |
29
|
|
|
Context::TYPE_REFERENCE => CheckResult::STATUS_COMPLIANCE, |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @codeCoverageIgnore This method is purely declarative. |
35
|
|
|
*/ |
36
|
|
|
public function getDefaultContextTypes() { |
37
|
|
|
return [ |
38
|
|
|
Context::TYPE_STATEMENT, |
39
|
|
|
Context::TYPE_QUALIFIER, |
40
|
|
|
Context::TYPE_REFERENCE, |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function checkConstraint( Context $context, Constraint $constraint ) { |
45
|
|
|
$snak = $context->getSnak(); |
46
|
|
|
|
47
|
|
|
if ( $context->getSnak()->getType() !== 'value' ) { |
48
|
|
|
return new CheckResult( $context, $constraint, [], CheckResult::STATUS_COMPLIANCE ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$violationMessage = $this->checkSnak( $snak ); |
|
|
|
|
52
|
|
|
|
53
|
|
|
return new CheckResult( |
54
|
|
|
$context, |
55
|
|
|
$constraint, |
56
|
|
|
[], |
57
|
|
|
$violationMessage === null ? |
58
|
|
|
CheckResult::STATUS_COMPLIANCE : |
59
|
|
|
CheckResult::STATUS_VIOLATION, |
60
|
|
|
$violationMessage |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param PropertyValueSnak $snak |
66
|
|
|
* @return ViolationMessage|null |
67
|
|
|
*/ |
68
|
|
|
public function checkSnak( PropertyValueSnak $snak ) { |
69
|
|
|
$dataValue = $snak->getDataValue(); |
70
|
|
|
|
71
|
|
|
if ( $dataValue instanceof DecimalValue ) { |
|
|
|
|
72
|
|
|
if ( !$this->isInteger( $dataValue ) ) { |
73
|
|
|
return $this->getViolationMessage( 'wbqc-violation-message-integer', $snak ); |
74
|
|
|
} |
75
|
|
|
} elseif ( $dataValue instanceof UnboundedQuantityValue ) { |
|
|
|
|
76
|
|
|
if ( !$this->isInteger( $dataValue->getAmount() ) ) { |
77
|
|
|
return $this->getViolationMessage( 'wbqc-violation-message-integer', $snak ); |
78
|
|
|
} elseif ( |
79
|
|
|
$dataValue instanceof QuantityValue && ( |
|
|
|
|
80
|
|
|
!$this->isInteger( $dataValue->getLowerBound() ) || |
81
|
|
|
!$this->isInteger( $dataValue->getUpperBound() ) |
82
|
|
|
) |
83
|
|
|
) { |
84
|
|
|
return $this->getViolationMessage( 'wbqc-violation-message-integer-bounds', $snak ); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param DecimalValue $decimalValue |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
private function isInteger( DecimalValue $decimalValue ) { |
96
|
|
|
return $decimalValue->getTrimmed()->getFractionalPart() === ''; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $messageKey |
101
|
|
|
* @param PropertyValueSnak $snak |
102
|
|
|
* @return ViolationMessage |
103
|
|
|
*/ |
104
|
|
|
private function getViolationMessage( $messageKey, PropertyValueSnak $snak ) { |
105
|
|
|
return ( new ViolationMessage( $messageKey ) ) |
106
|
|
|
->withEntityId( $snak->getPropertyId(), Role::CONSTRAINT_PROPERTY ) |
107
|
|
|
->withDataValue( $snak->getDataValue() ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function checkConstraintParameters( Constraint $constraint ) { |
111
|
|
|
// no parameters |
112
|
|
|
return []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.