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