@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | $this->sparqlHelper = $sparqlHelper; |
| 75 | 75 | $this->shellboxClientFactory = $shellboxClientFactory; |
| 76 | 76 | $this->knownGoodPatternsAsKeys = array_fill_keys( |
| 77 | - $this->config->get( 'WBQualityConstraintsFormatCheckerKnownGoodRegexPatterns' ), |
|
| 77 | + $this->config->get('WBQualityConstraintsFormatCheckerKnownGoodRegexPatterns'), |
|
| 78 | 78 | null |
| 79 | 79 | ); |
| 80 | 80 | $this->logger = $logger ?? new NullLogger(); |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | * @throws ConstraintParameterException |
| 109 | 109 | * @return CheckResult |
| 110 | 110 | */ |
| 111 | - public function checkConstraint( Context $context, Constraint $constraint ): CheckResult { |
|
| 111 | + public function checkConstraint(Context $context, Constraint $constraint): CheckResult { |
|
| 112 | 112 | $constraintParameters = $constraint->getConstraintParameters(); |
| 113 | 113 | $constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
| 114 | 114 | |
@@ -123,9 +123,9 @@ discard block |
||
| 123 | 123 | |
| 124 | 124 | $snak = $context->getSnak(); |
| 125 | 125 | |
| 126 | - if ( !$snak instanceof PropertyValueSnak ) { |
|
| 126 | + if (!$snak instanceof PropertyValueSnak) { |
|
| 127 | 127 | // nothing to check |
| 128 | - return new CheckResult( $context, $constraint, CheckResult::STATUS_COMPLIANCE ); |
|
| 128 | + return new CheckResult($context, $constraint, CheckResult::STATUS_COMPLIANCE); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | $dataValue = $snak->getDataValue(); |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | * error handling: |
| 135 | 135 | * type of $dataValue for properties with 'Format' constraint has to be 'string' or 'monolingualtext' |
| 136 | 136 | */ |
| 137 | - switch ( $dataValue->getType() ) { |
|
| 137 | + switch ($dataValue->getType()) { |
|
| 138 | 138 | case 'string': |
| 139 | 139 | $text = $dataValue->getValue(); |
| 140 | 140 | break; |
@@ -144,13 +144,13 @@ discard block |
||
| 144 | 144 | $text = $dataValue->getText(); |
| 145 | 145 | break; |
| 146 | 146 | default: |
| 147 | - $message = ( new ViolationMessage( 'wbqc-violation-message-value-needed-of-types-2' ) ) |
|
| 148 | - ->withEntityId( new ItemId( $constraintTypeItemId ), Role::CONSTRAINT_TYPE_ITEM ) |
|
| 149 | - ->withDataValueType( 'string' ) |
|
| 150 | - ->withDataValueType( 'monolingualtext' ); |
|
| 151 | - return new CheckResult( $context, $constraint, CheckResult::STATUS_VIOLATION, $message ); |
|
| 147 | + $message = (new ViolationMessage('wbqc-violation-message-value-needed-of-types-2')) |
|
| 148 | + ->withEntityId(new ItemId($constraintTypeItemId), Role::CONSTRAINT_TYPE_ITEM) |
|
| 149 | + ->withDataValueType('string') |
|
| 150 | + ->withDataValueType('monolingualtext'); |
|
| 151 | + return new CheckResult($context, $constraint, CheckResult::STATUS_VIOLATION, $message); |
|
| 152 | 152 | } |
| 153 | - $status = $this->runRegexCheck( $text, $format ); |
|
| 153 | + $status = $this->runRegexCheck($text, $format); |
|
| 154 | 154 | $message = $this->formatMessage( |
| 155 | 155 | $status, |
| 156 | 156 | $text, |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | $syntaxClarifications, |
| 160 | 160 | $constraintTypeItemId |
| 161 | 161 | ); |
| 162 | - return new CheckResult( $context, $constraint, $status, $message ); |
|
| 162 | + return new CheckResult($context, $constraint, $status, $message); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | private function formatMessage( |
@@ -171,42 +171,42 @@ discard block |
||
| 171 | 171 | string $constraintTypeItemId |
| 172 | 172 | ): ?ViolationMessage { |
| 173 | 173 | $message = null; |
| 174 | - if ( $status === CheckResult::STATUS_VIOLATION ) { |
|
| 175 | - $message = ( new ViolationMessage( 'wbqc-violation-message-format-clarification' ) ) |
|
| 176 | - ->withEntityId( $propertyId, Role::CONSTRAINT_PROPERTY ) |
|
| 177 | - ->withDataValue( new StringValue( $text ), Role::OBJECT ) |
|
| 178 | - ->withInlineCode( $format, Role::CONSTRAINT_PARAMETER_VALUE ) |
|
| 179 | - ->withMultilingualText( $syntaxClarifications, Role::CONSTRAINT_PARAMETER_VALUE ); |
|
| 180 | - } elseif ( $status === CheckResult::STATUS_TODO ) { |
|
| 181 | - $message = ( new ViolationMessage( 'wbqc-violation-message-security-reason' ) ) |
|
| 182 | - ->withEntityId( new ItemId( $constraintTypeItemId ), Role::CONSTRAINT_TYPE_ITEM ); |
|
| 174 | + if ($status === CheckResult::STATUS_VIOLATION) { |
|
| 175 | + $message = (new ViolationMessage('wbqc-violation-message-format-clarification')) |
|
| 176 | + ->withEntityId($propertyId, Role::CONSTRAINT_PROPERTY) |
|
| 177 | + ->withDataValue(new StringValue($text), Role::OBJECT) |
|
| 178 | + ->withInlineCode($format, Role::CONSTRAINT_PARAMETER_VALUE) |
|
| 179 | + ->withMultilingualText($syntaxClarifications, Role::CONSTRAINT_PARAMETER_VALUE); |
|
| 180 | + } elseif ($status === CheckResult::STATUS_TODO) { |
|
| 181 | + $message = (new ViolationMessage('wbqc-violation-message-security-reason')) |
|
| 182 | + ->withEntityId(new ItemId($constraintTypeItemId), Role::CONSTRAINT_TYPE_ITEM); |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | return $message; |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | - private function runRegexCheck( string $text, string $format ): string { |
|
| 189 | - if ( !$this->config->get( 'WBQualityConstraintsCheckFormatConstraint' ) ) { |
|
| 188 | + private function runRegexCheck(string $text, string $format): string { |
|
| 189 | + if (!$this->config->get('WBQualityConstraintsCheckFormatConstraint')) { |
|
| 190 | 190 | return CheckResult::STATUS_TODO; |
| 191 | 191 | } |
| 192 | - if ( \array_key_exists( $format, $this->knownGoodPatternsAsKeys ) ) { |
|
| 193 | - $checkResult = FormatCheckerHelper::runRegexCheck( $format, $text ); |
|
| 192 | + if (\array_key_exists($format, $this->knownGoodPatternsAsKeys)) { |
|
| 193 | + $checkResult = FormatCheckerHelper::runRegexCheck($format, $text); |
|
| 194 | 194 | } elseif ( |
| 195 | - $this->config->get( 'WBQualityConstraintsFormatCheckerShellboxRatio' ) > (float)wfRandom() |
|
| 195 | + $this->config->get('WBQualityConstraintsFormatCheckerShellboxRatio') > (float) wfRandom() |
|
| 196 | 196 | ) { |
| 197 | - $checkResult = $this->runRegexCheckUsingShellbox( $text, $format ); |
|
| 197 | + $checkResult = $this->runRegexCheckUsingShellbox($text, $format); |
|
| 198 | 198 | } else { |
| 199 | - return $this->runRegexCheckUsingSparql( $text, $format ); |
|
| 199 | + return $this->runRegexCheckUsingSparql($text, $format); |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | - if ( $checkResult === 1 ) { |
|
| 202 | + if ($checkResult === 1) { |
|
| 203 | 203 | return CheckResult::STATUS_COMPLIANCE; |
| 204 | - } elseif ( $checkResult === 0 ) { |
|
| 204 | + } elseif ($checkResult === 0) { |
|
| 205 | 205 | return CheckResult::STATUS_VIOLATION; |
| 206 | - } elseif ( $checkResult === false ) { |
|
| 206 | + } elseif ($checkResult === false) { |
|
| 207 | 207 | throw new ConstraintParameterException( |
| 208 | - ( new ViolationMessage( 'wbqc-violation-message-parameter-regex' ) ) |
|
| 209 | - ->withInlineCode( $format, Role::CONSTRAINT_PARAMETER_VALUE ) |
|
| 208 | + (new ViolationMessage('wbqc-violation-message-parameter-regex')) |
|
| 209 | + ->withInlineCode($format, Role::CONSTRAINT_PARAMETER_VALUE) |
|
| 210 | 210 | ); |
| 211 | 211 | } else { |
| 212 | 212 | return $checkResult; |
@@ -220,51 +220,51 @@ discard block |
||
| 220 | 220 | * - FALSE if $format is invalid regex |
| 221 | 221 | * - CheckResult::STATUS_TODO if Shellbox is not enabled |
| 222 | 222 | */ |
| 223 | - private function runRegexCheckUsingShellbox( string $text, string $format ) { |
|
| 224 | - if ( !$this->shellboxClientFactory->isEnabled( 'constraint-regex-checker' ) ) { |
|
| 223 | + private function runRegexCheckUsingShellbox(string $text, string $format) { |
|
| 224 | + if (!$this->shellboxClientFactory->isEnabled('constraint-regex-checker')) { |
|
| 225 | 225 | return CheckResult::STATUS_TODO; |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | try { |
| 229 | - return $this->shellboxClientFactory->getClient( [ |
|
| 230 | - 'timeout' => $this->config->get( 'WBQualityConstraintsSparqlMaxMillis' ) / 1000, |
|
| 229 | + return $this->shellboxClientFactory->getClient([ |
|
| 230 | + 'timeout' => $this->config->get('WBQualityConstraintsSparqlMaxMillis') / 1000, |
|
| 231 | 231 | 'service' => 'constraint-regex-checker', |
| 232 | - ] )->call( |
|
| 232 | + ])->call( |
|
| 233 | 233 | 'constraint-regex-checker', |
| 234 | - [ FormatCheckerHelper::class, 'runRegexCheck' ], |
|
| 235 | - [ $format, $text ], |
|
| 236 | - [ 'classes' => [ FormatCheckerHelper::class ] ], |
|
| 234 | + [FormatCheckerHelper::class, 'runRegexCheck'], |
|
| 235 | + [$format, $text], |
|
| 236 | + ['classes' => [FormatCheckerHelper::class]], |
|
| 237 | 237 | ); |
| 238 | - } catch ( ClientExceptionInterface $ce ) { |
|
| 239 | - $this->logger->notice( __METHOD__ . ': Network error, skipping check: {exception}', [ |
|
| 238 | + } catch (ClientExceptionInterface $ce) { |
|
| 239 | + $this->logger->notice(__METHOD__.': Network error, skipping check: {exception}', [ |
|
| 240 | 240 | 'exception' => $ce, |
| 241 | 241 | 'text' => $text, |
| 242 | 242 | 'format' => $format, |
| 243 | - ] ); |
|
| 243 | + ]); |
|
| 244 | 244 | return CheckResult::STATUS_TODO; |
| 245 | - } catch ( ShellboxError $e ) { |
|
| 246 | - $this->logger->error( __METHOD__ . ': Shellbox error, skipping check: {exception}', [ |
|
| 245 | + } catch (ShellboxError $e) { |
|
| 246 | + $this->logger->error(__METHOD__.': Shellbox error, skipping check: {exception}', [ |
|
| 247 | 247 | 'exception' => $e, |
| 248 | 248 | 'text' => $text, |
| 249 | 249 | 'format' => $format, |
| 250 | - ] ); |
|
| 250 | + ]); |
|
| 251 | 251 | return CheckResult::STATUS_TODO; |
| 252 | 252 | } |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | - private function runRegexCheckUsingSparql( string $text, string $format ): string { |
|
| 256 | - if ( $this->sparqlHelper instanceof DummySparqlHelper ) { |
|
| 255 | + private function runRegexCheckUsingSparql(string $text, string $format): string { |
|
| 256 | + if ($this->sparqlHelper instanceof DummySparqlHelper) { |
|
| 257 | 257 | return CheckResult::STATUS_TODO; |
| 258 | 258 | } |
| 259 | 259 | |
| 260 | - if ( $this->sparqlHelper->matchesRegularExpression( $text, $format ) ) { |
|
| 260 | + if ($this->sparqlHelper->matchesRegularExpression($text, $format)) { |
|
| 261 | 261 | return CheckResult::STATUS_COMPLIANCE; |
| 262 | 262 | } else { |
| 263 | 263 | return CheckResult::STATUS_VIOLATION; |
| 264 | 264 | } |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | - public function checkConstraintParameters( Constraint $constraint ): array { |
|
| 267 | + public function checkConstraintParameters(Constraint $constraint): array { |
|
| 268 | 268 | $constraintParameters = $constraint->getConstraintParameters(); |
| 269 | 269 | $constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
| 270 | 270 | $exceptions = []; |
@@ -273,14 +273,14 @@ discard block |
||
| 273 | 273 | $constraintParameters, |
| 274 | 274 | $constraintTypeItemId |
| 275 | 275 | ); |
| 276 | - } catch ( ConstraintParameterException $e ) { |
|
| 276 | + } catch (ConstraintParameterException $e) { |
|
| 277 | 277 | $exceptions[] = $e; |
| 278 | 278 | } |
| 279 | 279 | try { |
| 280 | 280 | $this->constraintParameterParser->parseSyntaxClarificationParameter( |
| 281 | 281 | $constraintParameters |
| 282 | 282 | ); |
| 283 | - } catch ( ConstraintParameterException $e ) { |
|
| 283 | + } catch (ConstraintParameterException $e) { |
|
| 284 | 284 | $exceptions[] = $e; |
| 285 | 285 | } |
| 286 | 286 | return $exceptions; |