Completed
Push — master ( 368b39...82a3c0 )
by
unknown
11:54
created

AllowedUnitsChecker   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 167
Duplicated Lines 30.54 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 8
dl 51
loc 167
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSupportedContextTypes() 0 7 1
A getDefaultContextTypes() 0 7 1
C checkConstraint() 16 50 8
A checkUnitless() 9 18 2
A standardize() 12 12 3
A checkConstraintParameters() 14 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker;
4
5
use DataValues\UnboundedQuantityValue;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\Snak\PropertyValueSnak;
8
use Wikibase\Lib\Units\UnitConverter;
9
use WikibaseQuality\ConstraintReport\Constraint;
10
use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker;
11
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
12
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterException;
13
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
14
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\UnitsParameter;
15
use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage;
16
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
17
use WikibaseQuality\ConstraintReport\Role;
18
19
/**
20
 * @author Lucas Werkmeister
21
 * @license GPL-2.0-or-later
22
 */
23
class AllowedUnitsChecker implements ConstraintChecker {
24
25
	/**
26
	 * @var ConstraintParameterParser
27
	 */
28
	private $constraintParameterParser;
29
30
	/**
31
	 * @var UnitConverter|null
32
	 */
33
	private $unitConverter;
34
35
	/**
36
	 * @param ConstraintParameterParser $constraintParameterParser
37
	 * @param UnitConverter|null $unitConverter
38
	 */
39
	public function __construct(
40
		ConstraintParameterParser $constraintParameterParser,
41
		UnitConverter $unitConverter = null
42
	) {
43
		$this->constraintParameterParser = $constraintParameterParser;
44
		$this->unitConverter = $unitConverter;
45
	}
46
47
	/**
48
	 * @codeCoverageIgnore This method is purely declarative.
49
	 */
50
	public function getSupportedContextTypes() {
51
		return [
52
			Context::TYPE_STATEMENT => CheckResult::STATUS_COMPLIANCE,
53
			Context::TYPE_QUALIFIER => CheckResult::STATUS_COMPLIANCE,
54
			Context::TYPE_REFERENCE => CheckResult::STATUS_COMPLIANCE,
55
		];
56
	}
57
58
	/**
59
	 * @codeCoverageIgnore This method is purely declarative.
60
	 */
61
	public function getDefaultContextTypes() {
62
		return [
63
			Context::TYPE_STATEMENT,
64
			Context::TYPE_QUALIFIER,
65
			Context::TYPE_REFERENCE,
66
		];
67
	}
68
69
	/**
70
	 * Checks an “allowed units” constraint.
71
	 *
72
	 * @param Context $context
73
	 * @param Constraint $constraint
74
	 *
75
	 * @throws ConstraintParameterException
76
	 * @return CheckResult
77
	 */
78
	public function checkConstraint( Context $context, Constraint $constraint ) {
79
		$parameters = [];
80
		$constraintParameters = $constraint->getConstraintParameters();
81
		$unitsParameter = $this->constraintParameterParser
82
			->parseUnitsParameter(
83
				$constraintParameters,
84
				$constraint->getConstraintTypeItemId()
85
			);
86
87
		$snak = $context->getSnak();
88
		if ( !$snak instanceof PropertyValueSnak ) {
89
			// nothing to check
90
			return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_COMPLIANCE );
91
		}
92
93
		$dataValue = $snak->getDataValue();
94 View Code Duplication
		if ( !$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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
			$message = ( new ViolationMessage( 'wbqc-violation-message-value-needed-of-type' ) )
96
				->withEntityId( new ItemId( $constraint->getConstraintTypeItemId() ), Role::CONSTRAINT_TYPE_ITEM )
97
				->withDataValueType( 'quantity' );
98
			return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_VIOLATION, $message );
99
		}
100
101
		if ( $dataValue->getUnit() === '1' ) {
102
			return $this->checkUnitless( $context, $constraint, $unitsParameter, $snak );
103
		}
104
105
		$status = CheckResult::STATUS_VIOLATION;
106
		$actualUnit = $this->standardize( $dataValue )->getUnit();
107
		foreach ( $unitsParameter->getUnitQuantities() as $unitQuantity ) {
108
			$allowedUnit = $this->standardize( $unitQuantity )->getUnit();
109
			if ( $actualUnit === $allowedUnit ) {
110
				$status = CheckResult::STATUS_COMPLIANCE;
111
				break;
112
			}
113
		}
114
115 View Code Duplication
		if ( $status === CheckResult::STATUS_VIOLATION ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
			$messageKey = $unitsParameter->getUnitlessAllowed() ?
117
				'wbqc-violation-message-units-or-none' :
118
				'wbqc-violation-message-units';
119
			$message = ( new ViolationMessage( $messageKey ) )
120
				->withEntityId( $snak->getPropertyId() )
121
				->withEntityIdList( $unitsParameter->getUnitItemIds() );
122
		} else {
123
			$message = null;
124
		}
125
126
		return new CheckResult( $context, $constraint, $parameters, $status, $message );
127
	}
128
129
	/**
130
	 * @param Context $context
131
	 * @param Constraint $constraint
132
	 * @param UnitsParameter $unitsParameter
133
	 * @param PropertyValueSnak $snak
134
	 * @return CheckResult
135
	 */
136
	private function checkUnitless(
137
		Context $context,
138
		Constraint $constraint,
139
		UnitsParameter $unitsParameter,
140
		PropertyValueSnak $snak
141
	) {
142 View Code Duplication
		if ( $unitsParameter->getUnitlessAllowed() ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
			$message = null;
144
			$status = CheckResult::STATUS_COMPLIANCE;
145
		} else {
146
			$message = ( new ViolationMessage( 'wbqc-violation-message-units' ) )
147
				->withEntityId( $snak->getPropertyId() )
148
				->withEntityIdList( $unitsParameter->getUnitItemIds() );
149
			$status = CheckResult::STATUS_VIOLATION;
150
		}
151
152
		return new CheckResult( $context, $constraint, [], $status, $message );
153
	}
154
155
	/**
156
	 * Convert $value to standard units.
157
	 *
158
	 * @param UnboundedQuantityValue $value
159
	 * @return UnboundedQuantityValue
160
	 */
161 View Code Duplication
	private function standardize( UnboundedQuantityValue $value ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
		if ( $this->unitConverter === null ) {
163
			return $value;
164
		}
165
166
		$standard = $this->unitConverter->toStandardUnits( $value );
167
		if ( $standard !== null ) {
168
			return $standard;
169
		} else {
170
			return $value;
171
		}
172
	}
173
174 View Code Duplication
	public function checkConstraintParameters( Constraint $constraint ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
		$constraintParameters = $constraint->getConstraintParameters();
176
		$exceptions = [];
177
		try {
178
			$this->constraintParameterParser->parseItemsParameter(
179
				$constraintParameters,
180
				$constraint->getConstraintTypeItemId(),
181
				true
182
			);
183
		} catch ( ConstraintParameterException $e ) {
184
			$exceptions[] = $e;
185
		}
186
		return $exceptions;
187
	}
188
189
}
190