Completed
Push — master ( bf5a23...7792f5 )
by
unknown
08:02 queued 10s
created

IntegerChecker::checkSnak()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 6.6037
cc 8
eloc 14
nc 6
nop 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 [
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 );
0 ignored issues
show
Compatibility introduced by
$snak of type object<Wikibase\DataModel\Snak\Snak> is not a sub-type of object<Wikibase\DataModel\Snak\PropertyValueSnak>. It seems like you assume a concrete implementation of the interface Wikibase\DataModel\Snak\Snak to be always present.

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.

Loading history...
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 ) {
0 ignored issues
show
Bug introduced by
The class DataValues\DecimalValue does not exist. Did you forget a USE statement, or did you not list all dependencies?

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 the composer.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 or require-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 ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
72
			if ( !$this->isInteger( $dataValue ) ) {
73
				return $this->getViolationMessage( 'wbqc-violation-message-integer', $snak );
74
			}
75
		} elseif ( $dataValue instanceof UnboundedQuantityValue ) {
0 ignored issues
show
Bug introduced by
The class DataValues\UnboundedQuantityValue does not exist. Did you forget a USE statement, or did you not list all dependencies?

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 the composer.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 or require-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 ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
76
			if ( !$this->isInteger( $dataValue->getAmount() ) ) {
77
				return $this->getViolationMessage( 'wbqc-violation-message-integer', $snak );
78
			} elseif (
79
				$dataValue instanceof QuantityValue && (
0 ignored issues
show
Bug introduced by
The class DataValues\QuantityValue does not exist. Did you forget a USE statement, or did you not list all dependencies?

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 the composer.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 or require-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 ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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