Completed
Push — master ( 16ffe4...e0f73c )
by
unknown
02:11
created
src/ConstraintCheck/Checker/DiffWithinRangeChecker.php 1 patch
Spacing   +42 added lines, -43 removed lines patch added patch discarded remove patch
@@ -84,8 +84,8 @@  discard block
 block discarded – undo
84 84
 	 * @throws ConstraintParameterException
85 85
 	 * @return array [ DataValue|null $min, DataValue|null $max, PropertyId $property, array $parameters ]
86 86
 	 */
87
-	private function parseConstraintParameters( Constraint $constraint ) {
88
-		list( $min, $max ) = $this->constraintParameterParser->parseQuantityRangeParameter(
87
+	private function parseConstraintParameters(Constraint $constraint) {
88
+		list($min, $max) = $this->constraintParameterParser->parseQuantityRangeParameter(
89 89
 			$constraint->getConstraintParameters(),
90 90
 			$constraint->getConstraintTypeItemId()
91 91
 		);
@@ -94,15 +94,15 @@  discard block
 block discarded – undo
94 94
 			$constraint->getConstraintTypeItemId()
95 95
 		);
96 96
 
97
-		if ( $min !== null ) {
98
-			$parameters['minimum_quantity'] = [ $min ];
97
+		if ($min !== null) {
98
+			$parameters['minimum_quantity'] = [$min];
99 99
 		}
100
-		if ( $max !== null ) {
101
-			$parameters['maximum_quantity'] = [ $max ];
100
+		if ($max !== null) {
101
+			$parameters['maximum_quantity'] = [$max];
102 102
 		}
103
-		$parameters['property'] = [ $property ];
103
+		$parameters['property'] = [$property];
104 104
 
105
-		return [ $min, $max, $property, $parameters ];
105
+		return [$min, $max, $property, $parameters];
106 106
 	}
107 107
 
108 108
 	/**
@@ -112,13 +112,13 @@  discard block
 block discarded – undo
112 112
 	 *
113 113
 	 * @return bool
114 114
 	 */
115
-	private function rangeInYears( $min, $max ) {
116
-		$yearUnit = $this->config->get( 'WBQualityConstraintsYearUnit' );
115
+	private function rangeInYears($min, $max) {
116
+		$yearUnit = $this->config->get('WBQualityConstraintsYearUnit');
117 117
 
118
-		if ( $min !== null && $min->getUnit() === $yearUnit ) {
118
+		if ($min !== null && $min->getUnit() === $yearUnit) {
119 119
 			return true;
120 120
 		}
121
-		if ( $max !== null && $max->getUnit() === $yearUnit ) {
121
+		if ($max !== null && $max->getUnit() === $yearUnit) {
122 122
 			return true;
123 123
 		}
124 124
 
@@ -134,59 +134,58 @@  discard block
 block discarded – undo
134 134
 	 * @throws ConstraintParameterException
135 135
 	 * @return CheckResult
136 136
 	 */
137
-	public function checkConstraint( Context $context, Constraint $constraint ) {
138
-		if ( $context->getSnakRank() === Statement::RANK_DEPRECATED ) {
139
-			return new CheckResult( $context, $constraint, [], CheckResult::STATUS_DEPRECATED );
137
+	public function checkConstraint(Context $context, Constraint $constraint) {
138
+		if ($context->getSnakRank() === Statement::RANK_DEPRECATED) {
139
+			return new CheckResult($context, $constraint, [], CheckResult::STATUS_DEPRECATED);
140 140
 		}
141 141
 
142 142
 		$parameters = [];
143 143
 
144 144
 		$snak = $context->getSnak();
145 145
 
146
-		if ( !$snak instanceof PropertyValueSnak ) {
146
+		if (!$snak instanceof PropertyValueSnak) {
147 147
 			// nothing to check
148
-			return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_COMPLIANCE );
148
+			return new CheckResult($context, $constraint, $parameters, CheckResult::STATUS_COMPLIANCE);
149 149
 		}
150 150
 
151 151
 		$minuend = $snak->getDataValue();
152 152
 
153 153
 		/** @var PropertyId $property */
154
-		list( $min, $max, $property, $parameters ) = $this->parseConstraintParameters( $constraint );
154
+		list($min, $max, $property, $parameters) = $this->parseConstraintParameters($constraint);
155 155
 
156 156
 		// checks only the first occurrence of the referenced property (this constraint implies a single value constraint on that property)
157
-		foreach ( $context->getSnakGroup() as $otherSnak ) {
157
+		foreach ($context->getSnakGroup() as $otherSnak) {
158 158
 			if (
159
-				!$property->equals( $otherSnak->getPropertyId() ) ||
159
+				!$property->equals($otherSnak->getPropertyId()) ||
160 160
 				!$otherSnak instanceof PropertyValueSnak
161 161
 			) {
162 162
 				continue;
163 163
 			}
164 164
 
165 165
 			$subtrahend = $otherSnak->getDataValue();
166
-			if ( $subtrahend->getType() === $minuend->getType() ) {
167
-				$diff = $this->rangeInYears( $min, $max ) && $minuend->getType() === 'time' ?
168
-					$this->rangeCheckerHelper->getDifferenceInYears( $minuend, $subtrahend ) :
169
-					$this->rangeCheckerHelper->getDifference( $minuend, $subtrahend );
166
+			if ($subtrahend->getType() === $minuend->getType()) {
167
+				$diff = $this->rangeInYears($min, $max) && $minuend->getType() === 'time' ?
168
+					$this->rangeCheckerHelper->getDifferenceInYears($minuend, $subtrahend) : $this->rangeCheckerHelper->getDifference($minuend, $subtrahend);
170 169
 
171
-				if ( $this->rangeCheckerHelper->getComparison( $min, $diff ) > 0 ||
172
-					$this->rangeCheckerHelper->getComparison( $diff, $max ) > 0
170
+				if ($this->rangeCheckerHelper->getComparison($min, $diff) > 0 ||
171
+					$this->rangeCheckerHelper->getComparison($diff, $max) > 0
173 172
 				) {
174 173
 					// at least one of $min, $max is set at this point, otherwise there could be no violation
175
-					$openness = $min !== null ? ( $max !== null ? '' : '-rightopen' ) : '-leftopen';
174
+					$openness = $min !== null ? ($max !== null ? '' : '-rightopen') : '-leftopen';
176 175
 					// possible message keys:
177 176
 					// wbqc-violation-message-diff-within-range
178 177
 					// wbqc-violation-message-diff-within-range-leftopen
179 178
 					// wbqc-violation-message-diff-within-range-rightopen
180
-					$message = ( new ViolationMessage( "wbqc-violation-message-diff-within-range$openness" ) )
181
-						->withEntityId( $context->getSnak()->getPropertyId(), Role::PREDICATE )
182
-						->withDataValue( $minuend, Role::OBJECT )
183
-						->withEntityId( $otherSnak->getPropertyId(), Role::PREDICATE )
184
-						->withDataValue( $subtrahend, Role::OBJECT );
185
-					if ( $min !== null ) {
186
-						$message = $message->withDataValue( $min, Role::OBJECT );
179
+					$message = (new ViolationMessage("wbqc-violation-message-diff-within-range$openness"))
180
+						->withEntityId($context->getSnak()->getPropertyId(), Role::PREDICATE)
181
+						->withDataValue($minuend, Role::OBJECT)
182
+						->withEntityId($otherSnak->getPropertyId(), Role::PREDICATE)
183
+						->withDataValue($subtrahend, Role::OBJECT);
184
+					if ($min !== null) {
185
+						$message = $message->withDataValue($min, Role::OBJECT);
187 186
 					}
188
-					if ( $max !== null ) {
189
-						$message = $message->withDataValue( $max, Role::OBJECT );
187
+					if ($max !== null) {
188
+						$message = $message->withDataValue($max, Role::OBJECT);
190 189
 					}
191 190
 					$status = CheckResult::STATUS_VIOLATION;
192 191
 				} else {
@@ -194,17 +193,17 @@  discard block
 block discarded – undo
194 193
 					$status = CheckResult::STATUS_COMPLIANCE;
195 194
 				}
196 195
 			} else {
197
-				$message = new ViolationMessage( 'wbqc-violation-message-diff-within-range-must-have-equal-types' );
196
+				$message = new ViolationMessage('wbqc-violation-message-diff-within-range-must-have-equal-types');
198 197
 				$status = CheckResult::STATUS_VIOLATION;
199 198
 			}
200 199
 
201
-			return new CheckResult( $context, $constraint, $parameters, $status, $message );
200
+			return new CheckResult($context, $constraint, $parameters, $status, $message);
202 201
 		}
203 202
 
204
-		return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_COMPLIANCE );
203
+		return new CheckResult($context, $constraint, $parameters, CheckResult::STATUS_COMPLIANCE);
205 204
 	}
206 205
 
207
-	public function checkConstraintParameters( Constraint $constraint ) {
206
+	public function checkConstraintParameters(Constraint $constraint) {
208 207
 		$constraintParameters = $constraint->getConstraintParameters();
209 208
 		$exceptions = [];
210 209
 		try {
@@ -212,12 +211,12 @@  discard block
 block discarded – undo
212 211
 				$constraintParameters,
213 212
 				$constraint->getConstraintTypeItemId()
214 213
 			);
215
-		} catch ( ConstraintParameterException $e ) {
214
+		} catch (ConstraintParameterException $e) {
216 215
 			$exceptions[] = $e;
217 216
 		}
218 217
 		try {
219
-			$this->constraintParameterParser->parsePropertyParameter( $constraintParameters, $constraint->getConstraintTypeItemId() );
220
-		} catch ( ConstraintParameterException $e ) {
218
+			$this->constraintParameterParser->parsePropertyParameter($constraintParameters, $constraint->getConstraintTypeItemId());
219
+		} catch (ConstraintParameterException $e) {
221 220
 			$exceptions[] = $e;
222 221
 		}
223 222
 		return $exceptions;
Please login to merge, or discard this patch.