1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker; |
4
|
|
|
|
5
|
|
|
use DataValues\DecimalValue; |
6
|
|
|
use DataValues\UnboundedQuantityValue; |
7
|
|
|
use Wikibase\DataModel\Snak\PropertyValueSnak; |
8
|
|
|
use WikibaseQuality\ConstraintReport\Constraint; |
9
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker; |
10
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context; |
11
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage; |
12
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult; |
13
|
|
|
use WikibaseQuality\ConstraintReport\Role; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Amir Sarabadani |
17
|
|
|
* @license GPL-2.0-or-later |
18
|
|
|
*/ |
19
|
|
|
class IntegerChecker implements ConstraintChecker { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @codeCoverageIgnore This method is purely declarative. |
23
|
|
|
*/ |
24
|
|
|
public function getSupportedContextTypes() { |
25
|
|
|
return [ |
26
|
|
|
Context::TYPE_STATEMENT => CheckResult::STATUS_COMPLIANCE, |
27
|
|
|
Context::TYPE_QUALIFIER => CheckResult::STATUS_COMPLIANCE, |
28
|
|
|
Context::TYPE_REFERENCE => CheckResult::STATUS_COMPLIANCE, |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @codeCoverageIgnore This method is purely declarative. |
34
|
|
|
*/ |
35
|
|
|
public function getDefaultContextTypes() { |
36
|
|
|
return [ |
37
|
|
|
Context::TYPE_STATEMENT, |
38
|
|
|
Context::TYPE_QUALIFIER, |
39
|
|
|
Context::TYPE_REFERENCE, |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function checkConstraint( Context $context, Constraint $constraint ) { |
44
|
|
|
if ( $context->getSnak()->getType() !== 'value' ) { |
45
|
|
|
return new CheckResult( $context, $constraint, [], CheckResult::STATUS_COMPLIANCE ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** @var PropertyValueSnak $snak */ |
49
|
|
|
$snak = $context->getSnak(); |
50
|
|
|
|
51
|
|
|
if ( $snak->getDataValue() instanceof DecimalValue ) { |
|
|
|
|
52
|
|
|
return $this->checkDecimalValue( $snak->getDataValue(), $context, $constraint ); |
53
|
|
|
} elseif ( $snak->getDataValue() instanceof UnboundedQuantityValue ) { |
|
|
|
|
54
|
|
|
return $this->checkDecimalValue( $snak->getDataValue()->getAmount(), $context, $constraint ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return new CheckResult( $context, $constraint, [], CheckResult::STATUS_COMPLIANCE ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function checkDecimalValue( DecimalValue $decimalValue, Context $context, Constraint $constraint ) { |
61
|
|
|
if ( $decimalValue->getTrimmed()->getFractionalPart() === '' ) { |
62
|
|
|
return new CheckResult( $context, $constraint, [], CheckResult::STATUS_COMPLIANCE ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$message = ( new ViolationMessage( 'wbqc-violation-message-integer' ) ) |
66
|
|
|
->withEntityId( $context->getSnak()->getPropertyId(), Role::CONSTRAINT_PROPERTY ) |
67
|
|
|
->withDataValue( $decimalValue ); |
68
|
|
|
return new CheckResult( |
69
|
|
|
$context, |
70
|
|
|
$constraint, |
71
|
|
|
[], |
72
|
|
|
CheckResult::STATUS_VIOLATION, |
73
|
|
|
$message |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function checkConstraintParameters( Constraint $constraint ) { |
78
|
|
|
// no parameters |
79
|
|
|
return []; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.