@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare( strict_types = 1 ); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker; |
6 | 6 | |
@@ -69,26 +69,26 @@ discard block |
||
69 | 69 | * @return array first element is the namespace number (default namespace for TitleParser), |
70 | 70 | * second element is a string to prepend to the title before giving it to the TitleParser |
71 | 71 | */ |
72 | - private function getCommonsNamespace( string $namespace ): array { |
|
73 | - switch ( $namespace ) { |
|
72 | + private function getCommonsNamespace(string $namespace): array { |
|
73 | + switch ($namespace) { |
|
74 | 74 | case '': |
75 | - return [ NS_MAIN, '' ]; |
|
75 | + return [NS_MAIN, '']; |
|
76 | 76 | // extra namespaces, see operations/mediawiki-config.git, |
77 | 77 | // wmf-config/InitialiseSettings.php, 'wgExtraNamespaces' key, 'commonswiki' subkey |
78 | 78 | case 'Creator': |
79 | - return [ 100, '' ]; |
|
79 | + return [100, '']; |
|
80 | 80 | case 'TimedText': |
81 | - return [ 102, '' ]; |
|
81 | + return [102, '']; |
|
82 | 82 | case 'Sequence': |
83 | - return [ 104, '' ]; |
|
83 | + return [104, '']; |
|
84 | 84 | case 'Institution': |
85 | - return [ 106, '' ]; |
|
85 | + return [106, '']; |
|
86 | 86 | // extension namespace, see mediawiki/extensions/JsonConfig.git, |
87 | 87 | // extension.json, 'namespaces' key, third element |
88 | 88 | case 'Data': |
89 | - return [ 486, '' ]; |
|
89 | + return [486, '']; |
|
90 | 90 | default: |
91 | - return [ NS_MAIN, $namespace . ':' ]; |
|
91 | + return [NS_MAIN, $namespace.':']; |
|
92 | 92 | } |
93 | 93 | } |
94 | 94 | |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | * |
98 | 98 | * @throws ConstraintParameterException |
99 | 99 | */ |
100 | - public function checkConstraint( Context $context, Constraint $constraint ): CheckResult { |
|
100 | + public function checkConstraint(Context $context, Constraint $constraint): CheckResult { |
|
101 | 101 | $parameters = []; |
102 | 102 | $constraintParameters = $constraint->getConstraintParameters(); |
103 | 103 | $constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
@@ -106,13 +106,13 @@ discard block |
||
106 | 106 | $constraintParameters, |
107 | 107 | $constraintTypeItemId |
108 | 108 | ); |
109 | - $parameters['namespace'] = [ $namespace ]; |
|
109 | + $parameters['namespace'] = [$namespace]; |
|
110 | 110 | |
111 | 111 | $snak = $context->getSnak(); |
112 | 112 | |
113 | - if ( !$snak instanceof PropertyValueSnak ) { |
|
113 | + if (!$snak instanceof PropertyValueSnak) { |
|
114 | 114 | // nothing to check |
115 | - return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_COMPLIANCE ); |
|
115 | + return new CheckResult($context, $constraint, $parameters, CheckResult::STATUS_COMPLIANCE); |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | $dataValue = $snak->getDataValue(); |
@@ -122,37 +122,37 @@ discard block |
||
122 | 122 | * type of $dataValue for properties with 'Commons link' constraint has to be 'string' |
123 | 123 | * parameter $namespace can be null, works for commons galleries |
124 | 124 | */ |
125 | - if ( $dataValue->getType() !== 'string' ) { |
|
126 | - $message = ( new ViolationMessage( 'wbqc-violation-message-value-needed-of-type' ) ) |
|
127 | - ->withEntityId( new ItemId( $constraintTypeItemId ), Role::CONSTRAINT_TYPE_ITEM ) |
|
128 | - ->withDataValueType( 'string' ); |
|
129 | - return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_VIOLATION, $message ); |
|
125 | + if ($dataValue->getType() !== 'string') { |
|
126 | + $message = (new ViolationMessage('wbqc-violation-message-value-needed-of-type')) |
|
127 | + ->withEntityId(new ItemId($constraintTypeItemId), Role::CONSTRAINT_TYPE_ITEM) |
|
128 | + ->withDataValueType('string'); |
|
129 | + return new CheckResult($context, $constraint, $parameters, CheckResult::STATUS_VIOLATION, $message); |
|
130 | 130 | } |
131 | 131 | |
132 | 132 | $commonsLink = $dataValue->getValue(); |
133 | - if ( !$this->commonsLinkIsWellFormed( $commonsLink ) ) { |
|
134 | - return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_VIOLATION, |
|
135 | - new ViolationMessage( 'wbqc-violation-message-commons-link-not-well-formed' ) ); |
|
133 | + if (!$this->commonsLinkIsWellFormed($commonsLink)) { |
|
134 | + return new CheckResult($context, $constraint, $parameters, CheckResult::STATUS_VIOLATION, |
|
135 | + new ViolationMessage('wbqc-violation-message-commons-link-not-well-formed')); |
|
136 | 136 | } |
137 | 137 | |
138 | - $prefix = $this->getCommonsNamespace( $namespace )[1]; |
|
138 | + $prefix = $this->getCommonsNamespace($namespace)[1]; |
|
139 | 139 | $normalizedTitle = $this->pageNameNormalizer->normalizePageName( |
140 | - $prefix . $commonsLink, |
|
140 | + $prefix.$commonsLink, |
|
141 | 141 | 'https://commons.wikimedia.org/w/api.php' |
142 | 142 | ); |
143 | - if ( $normalizedTitle === false ) { |
|
144 | - if ( $this->valueIncludesNamespace( $commonsLink, $namespace ) ) { |
|
145 | - return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_VIOLATION, |
|
146 | - new ViolationMessage( 'wbqc-violation-message-commons-link-not-well-formed' ) ); |
|
143 | + if ($normalizedTitle === false) { |
|
144 | + if ($this->valueIncludesNamespace($commonsLink, $namespace)) { |
|
145 | + return new CheckResult($context, $constraint, $parameters, CheckResult::STATUS_VIOLATION, |
|
146 | + new ViolationMessage('wbqc-violation-message-commons-link-not-well-formed')); |
|
147 | 147 | } |
148 | - return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_VIOLATION, |
|
149 | - new ViolationMessage( 'wbqc-violation-message-commons-link-no-existent' ) ); |
|
148 | + return new CheckResult($context, $constraint, $parameters, CheckResult::STATUS_VIOLATION, |
|
149 | + new ViolationMessage('wbqc-violation-message-commons-link-no-existent')); |
|
150 | 150 | } |
151 | 151 | |
152 | - return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_COMPLIANCE, null ); |
|
152 | + return new CheckResult($context, $constraint, $parameters, CheckResult::STATUS_COMPLIANCE, null); |
|
153 | 153 | } |
154 | 154 | |
155 | - public function checkConstraintParameters( Constraint $constraint ): array { |
|
155 | + public function checkConstraintParameters(Constraint $constraint): array { |
|
156 | 156 | $constraintParameters = $constraint->getConstraintParameters(); |
157 | 157 | $constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
158 | 158 | $exceptions = []; |
@@ -161,15 +161,15 @@ discard block |
||
161 | 161 | $constraintParameters, |
162 | 162 | $constraintTypeItemId |
163 | 163 | ); |
164 | - } catch ( ConstraintParameterException $e ) { |
|
164 | + } catch (ConstraintParameterException $e) { |
|
165 | 165 | $exceptions[] = $e; |
166 | 166 | } |
167 | 167 | return $exceptions; |
168 | 168 | } |
169 | 169 | |
170 | - private function commonsLinkIsWellFormed( string $commonsLink ): bool { |
|
171 | - $toReplace = [ "_", "%20" ]; |
|
172 | - $compareString = trim( str_replace( $toReplace, '', $commonsLink ) ); |
|
170 | + private function commonsLinkIsWellFormed(string $commonsLink): bool { |
|
171 | + $toReplace = ["_", "%20"]; |
|
172 | + $compareString = trim(str_replace($toReplace, '', $commonsLink)); |
|
173 | 173 | |
174 | 174 | return $commonsLink === $compareString; |
175 | 175 | } |
@@ -178,9 +178,9 @@ discard block |
||
178 | 178 | * Checks whether the value of the statement already includes the namespace. |
179 | 179 | * This special case should be reported as “malformed title” instead of “title does not exist”. |
180 | 180 | */ |
181 | - private function valueIncludesNamespace( string $value, string $namespace ): bool { |
|
181 | + private function valueIncludesNamespace(string $value, string $namespace): bool { |
|
182 | 182 | return $namespace !== '' && |
183 | - strncasecmp( $value, $namespace . ':', strlen( $namespace ) + 1 ) === 0; |
|
183 | + strncasecmp($value, $namespace.':', strlen($namespace) + 1) === 0; |
|
184 | 184 | } |
185 | 185 | |
186 | 186 | } |