@@ -21,12 +21,12 @@ |
||
| 21 | 21 | * @return false|int (from the preg_match documentation) returns 1 if the pattern |
| 22 | 22 | * matches given subject, 0 if it does not, or FALSE if an error occurred. |
| 23 | 23 | */ |
| 24 | - public static function runRegexCheck( string $regex, string $text ) { |
|
| 25 | - $pattern = '(^(?:' . $regex . ')$)u'; |
|
| 24 | + public static function runRegexCheck(string $regex, string $text) { |
|
| 25 | + $pattern = '(^(?:'.$regex.')$)u'; |
|
| 26 | 26 | |
| 27 | 27 | // `preg_match` emits an E_WARNING when the pattern is not valid regex. |
| 28 | 28 | // Silence this warning to avoid throwing a ShellboxError. |
| 29 | 29 | // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
| 30 | - return @preg_match( $pattern, $text ); |
|
| 30 | + return @preg_match($pattern, $text); |
|
| 31 | 31 | } |
| 32 | 32 | } |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | * @throws ConstraintParameterException |
| 95 | 95 | * @return CheckResult |
| 96 | 96 | */ |
| 97 | - public function checkConstraint( Context $context, Constraint $constraint ) { |
|
| 97 | + public function checkConstraint(Context $context, Constraint $constraint) { |
|
| 98 | 98 | $constraintParameters = $constraint->getConstraintParameters(); |
| 99 | 99 | $constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
| 100 | 100 | |
@@ -109,9 +109,9 @@ discard block |
||
| 109 | 109 | |
| 110 | 110 | $snak = $context->getSnak(); |
| 111 | 111 | |
| 112 | - if ( !$snak instanceof PropertyValueSnak ) { |
|
| 112 | + if (!$snak instanceof PropertyValueSnak) { |
|
| 113 | 113 | // nothing to check |
| 114 | - return new CheckResult( $context, $constraint, CheckResult::STATUS_COMPLIANCE ); |
|
| 114 | + return new CheckResult($context, $constraint, CheckResult::STATUS_COMPLIANCE); |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | $dataValue = $snak->getDataValue(); |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | * error handling: |
| 121 | 121 | * type of $dataValue for properties with 'Format' constraint has to be 'string' or 'monolingualtext' |
| 122 | 122 | */ |
| 123 | - switch ( $dataValue->getType() ) { |
|
| 123 | + switch ($dataValue->getType()) { |
|
| 124 | 124 | case 'string': |
| 125 | 125 | $text = $dataValue->getValue(); |
| 126 | 126 | break; |
@@ -130,13 +130,13 @@ discard block |
||
| 130 | 130 | $text = $dataValue->getText(); |
| 131 | 131 | break; |
| 132 | 132 | default: |
| 133 | - $message = ( new ViolationMessage( 'wbqc-violation-message-value-needed-of-types-2' ) ) |
|
| 134 | - ->withEntityId( new ItemId( $constraintTypeItemId ), Role::CONSTRAINT_TYPE_ITEM ) |
|
| 135 | - ->withDataValueType( 'string' ) |
|
| 136 | - ->withDataValueType( 'monolingualtext' ); |
|
| 137 | - return new CheckResult( $context, $constraint, CheckResult::STATUS_VIOLATION, $message ); |
|
| 133 | + $message = (new ViolationMessage('wbqc-violation-message-value-needed-of-types-2')) |
|
| 134 | + ->withEntityId(new ItemId($constraintTypeItemId), Role::CONSTRAINT_TYPE_ITEM) |
|
| 135 | + ->withDataValueType('string') |
|
| 136 | + ->withDataValueType('monolingualtext'); |
|
| 137 | + return new CheckResult($context, $constraint, CheckResult::STATUS_VIOLATION, $message); |
|
| 138 | 138 | } |
| 139 | - $status = $this->runRegexCheck( $text, $format ); |
|
| 139 | + $status = $this->runRegexCheck($text, $format); |
|
| 140 | 140 | $message = $this->formatMessage( |
| 141 | 141 | $status, |
| 142 | 142 | $text, |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | $syntaxClarifications, |
| 146 | 146 | $constraintTypeItemId |
| 147 | 147 | ); |
| 148 | - return new CheckResult( $context, $constraint, $status, $message ); |
|
| 148 | + return new CheckResult($context, $constraint, $status, $message); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | private function formatMessage( |
@@ -157,76 +157,76 @@ discard block |
||
| 157 | 157 | string $constraintTypeItemId |
| 158 | 158 | ): ?ViolationMessage { |
| 159 | 159 | $message = null; |
| 160 | - if ( $status === CheckResult::STATUS_VIOLATION ) { |
|
| 161 | - $message = ( new ViolationMessage( 'wbqc-violation-message-format-clarification' ) ) |
|
| 162 | - ->withEntityId( $propertyId, Role::CONSTRAINT_PROPERTY ) |
|
| 163 | - ->withDataValue( new StringValue( $text ), Role::OBJECT ) |
|
| 164 | - ->withInlineCode( $format, Role::CONSTRAINT_PARAMETER_VALUE ) |
|
| 165 | - ->withMultilingualText( $syntaxClarifications, Role::CONSTRAINT_PARAMETER_VALUE ); |
|
| 166 | - } elseif ( $status === CheckResult::STATUS_TODO ) { |
|
| 167 | - $message = ( new ViolationMessage( 'wbqc-violation-message-security-reason' ) ) |
|
| 168 | - ->withEntityId( new ItemId( $constraintTypeItemId ), Role::CONSTRAINT_TYPE_ITEM ); |
|
| 160 | + if ($status === CheckResult::STATUS_VIOLATION) { |
|
| 161 | + $message = (new ViolationMessage('wbqc-violation-message-format-clarification')) |
|
| 162 | + ->withEntityId($propertyId, Role::CONSTRAINT_PROPERTY) |
|
| 163 | + ->withDataValue(new StringValue($text), Role::OBJECT) |
|
| 164 | + ->withInlineCode($format, Role::CONSTRAINT_PARAMETER_VALUE) |
|
| 165 | + ->withMultilingualText($syntaxClarifications, Role::CONSTRAINT_PARAMETER_VALUE); |
|
| 166 | + } elseif ($status === CheckResult::STATUS_TODO) { |
|
| 167 | + $message = (new ViolationMessage('wbqc-violation-message-security-reason')) |
|
| 168 | + ->withEntityId(new ItemId($constraintTypeItemId), Role::CONSTRAINT_TYPE_ITEM); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | return $message; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - private function runRegexCheck( string $text, string $format ): string { |
|
| 175 | - if ( !$this->config->get( 'WBQualityConstraintsCheckFormatConstraint' ) ) { |
|
| 174 | + private function runRegexCheck(string $text, string $format): string { |
|
| 175 | + if (!$this->config->get('WBQualityConstraintsCheckFormatConstraint')) { |
|
| 176 | 176 | return CheckResult::STATUS_TODO; |
| 177 | 177 | } |
| 178 | 178 | if ( |
| 179 | - $this->config->get( 'WBQualityConstraintsFormatCheckerShellboxRatio' ) > (float)wfRandom() |
|
| 179 | + $this->config->get('WBQualityConstraintsFormatCheckerShellboxRatio') > (float) wfRandom() |
|
| 180 | 180 | ) { |
| 181 | - return $this->runRegexCheckUsingShellbox( $text, $format ); |
|
| 181 | + return $this->runRegexCheckUsingShellbox($text, $format); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | - return $this->runRegexCheckUsingSparql( $text, $format ); |
|
| 184 | + return $this->runRegexCheckUsingSparql($text, $format); |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | /** |
| 188 | 188 | * @throws ConstraintParameterException |
| 189 | 189 | */ |
| 190 | - private function runRegexCheckUsingShellbox( string $text, string $format ): string { |
|
| 191 | - if ( !$this->shellboxClientFactory->isEnabled( 'constraint-regex-checker' ) ) { |
|
| 190 | + private function runRegexCheckUsingShellbox(string $text, string $format): string { |
|
| 191 | + if (!$this->shellboxClientFactory->isEnabled('constraint-regex-checker')) { |
|
| 192 | 192 | return CheckResult::STATUS_TODO; |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | - $shellboxResponse = $this->shellboxClientFactory->getClient( [ |
|
| 196 | - 'timeout' => $this->config->get( 'WBQualityConstraintsSparqlMaxMillis' ) / 1000, |
|
| 195 | + $shellboxResponse = $this->shellboxClientFactory->getClient([ |
|
| 196 | + 'timeout' => $this->config->get('WBQualityConstraintsSparqlMaxMillis') / 1000, |
|
| 197 | 197 | 'service' => 'constraint-regex-checker', |
| 198 | - ] )->call( |
|
| 198 | + ])->call( |
|
| 199 | 199 | 'constraint-regex-checker', |
| 200 | - [ ShellboxHelper::class, 'runRegexCheck' ], |
|
| 201 | - [ $format, $text ], |
|
| 202 | - [ 'classes' => [ ShellboxHelper::class ] ], |
|
| 200 | + [ShellboxHelper::class, 'runRegexCheck'], |
|
| 201 | + [$format, $text], |
|
| 202 | + ['classes' => [ShellboxHelper::class]], |
|
| 203 | 203 | ); |
| 204 | 204 | |
| 205 | - if ( $shellboxResponse === 1 ) { |
|
| 205 | + if ($shellboxResponse === 1) { |
|
| 206 | 206 | return CheckResult::STATUS_COMPLIANCE; |
| 207 | - } elseif ( $shellboxResponse === 0 ) { |
|
| 207 | + } elseif ($shellboxResponse === 0) { |
|
| 208 | 208 | return CheckResult::STATUS_VIOLATION; |
| 209 | 209 | } else { |
| 210 | 210 | throw new ConstraintParameterException( |
| 211 | - ( new ViolationMessage( 'wbqc-violation-message-parameter-regex' ) ) |
|
| 212 | - ->withInlineCode( $format, Role::CONSTRAINT_PARAMETER_VALUE ) |
|
| 211 | + (new ViolationMessage('wbqc-violation-message-parameter-regex')) |
|
| 212 | + ->withInlineCode($format, Role::CONSTRAINT_PARAMETER_VALUE) |
|
| 213 | 213 | ); |
| 214 | 214 | } |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | - private function runRegexCheckUsingSparql( string $text, string $format ): string { |
|
| 218 | - if ( $this->sparqlHelper instanceof DummySparqlHelper ) { |
|
| 217 | + private function runRegexCheckUsingSparql(string $text, string $format): string { |
|
| 218 | + if ($this->sparqlHelper instanceof DummySparqlHelper) { |
|
| 219 | 219 | return CheckResult::STATUS_TODO; |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | - if ( $this->sparqlHelper->matchesRegularExpression( $text, $format ) ) { |
|
| 222 | + if ($this->sparqlHelper->matchesRegularExpression($text, $format)) { |
|
| 223 | 223 | return CheckResult::STATUS_COMPLIANCE; |
| 224 | 224 | } else { |
| 225 | 225 | return CheckResult::STATUS_VIOLATION; |
| 226 | 226 | } |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | - public function checkConstraintParameters( Constraint $constraint ) { |
|
| 229 | + public function checkConstraintParameters(Constraint $constraint) { |
|
| 230 | 230 | $constraintParameters = $constraint->getConstraintParameters(); |
| 231 | 231 | $constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
| 232 | 232 | $exceptions = []; |
@@ -235,14 +235,14 @@ discard block |
||
| 235 | 235 | $constraintParameters, |
| 236 | 236 | $constraintTypeItemId |
| 237 | 237 | ); |
| 238 | - } catch ( ConstraintParameterException $e ) { |
|
| 238 | + } catch (ConstraintParameterException $e) { |
|
| 239 | 239 | $exceptions[] = $e; |
| 240 | 240 | } |
| 241 | 241 | try { |
| 242 | 242 | $this->constraintParameterParser->parseSyntaxClarificationParameter( |
| 243 | 243 | $constraintParameters |
| 244 | 244 | ); |
| 245 | - } catch ( ConstraintParameterException $e ) { |
|
| 245 | + } catch (ConstraintParameterException $e) { |
|
| 246 | 246 | $exceptions[] = $e; |
| 247 | 247 | } |
| 248 | 248 | return $exceptions; |