Lucas Werkmeister (WMDE) has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/402882 )
Change subject: Add ConstraintChecker::getSupportedContextTypes()
......................................................................
Add ConstraintChecker::getSupportedContextTypes()
This method describes which context types (scopes) a constraint type
supports, and which should be checked by default. Using this
information, the constraint scope parameter can be parsed and validated
in a single location (most likely DelegatingConstraintChecker) without
duplicating error handling code in several locations.
Bug: T183542
Change-Id: I6720b4a984a6ba2daa7425a6de21a30e09af22db
---
M src/ConstraintCheck/Checker/CommonsLinkChecker.php
M src/ConstraintCheck/Checker/ConflictsWithChecker.php
M src/ConstraintCheck/Checker/DiffWithinRangeChecker.php
M src/ConstraintCheck/Checker/FormatChecker.php
M src/ConstraintCheck/Checker/InverseChecker.php
M src/ConstraintCheck/Checker/ItemChecker.php
M src/ConstraintCheck/Checker/MandatoryQualifiersChecker.php
M src/ConstraintCheck/Checker/MultiValueChecker.php
M src/ConstraintCheck/Checker/OneOfChecker.php
M src/ConstraintCheck/Checker/QualifierChecker.php
M src/ConstraintCheck/Checker/QualifiersChecker.php
M src/ConstraintCheck/Checker/RangeChecker.php
M src/ConstraintCheck/Checker/ReferenceChecker.php
M src/ConstraintCheck/Checker/SingleValueChecker.php
M src/ConstraintCheck/Checker/SymmetricChecker.php
M src/ConstraintCheck/Checker/TargetRequiredClaimChecker.php
M src/ConstraintCheck/Checker/TypeChecker.php
M src/ConstraintCheck/Checker/UniqueValueChecker.php
M src/ConstraintCheck/Checker/ValueOnlyChecker.php
M src/ConstraintCheck/Checker/ValueTypeChecker.php
M src/ConstraintCheck/ConstraintChecker.php
M tests/phpunit/Fake/FakeChecker.php
22 files changed, 267 insertions(+), 0 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
refs/changes/82/402882/1
diff --git a/src/ConstraintCheck/Checker/CommonsLinkChecker.php
b/src/ConstraintCheck/Checker/CommonsLinkChecker.php
index 59e0f1e..618ccd8 100644
--- a/src/ConstraintCheck/Checker/CommonsLinkChecker.php
+++ b/src/ConstraintCheck/Checker/CommonsLinkChecker.php
@@ -52,6 +52,17 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_COMPLIANCE, true ],
+ ];
+ }
+
+ /**
* Get the number of a namespace on Wikimedia Commons (commonswiki).
* All namespaces not known to this function will be looked up by the
TitleParser.
*
diff --git a/src/ConstraintCheck/Checker/ConflictsWithChecker.php
b/src/ConstraintCheck/Checker/ConflictsWithChecker.php
index b2f4ddc..a1adec6 100644
--- a/src/ConstraintCheck/Checker/ConflictsWithChecker.php
+++ b/src/ConstraintCheck/Checker/ConflictsWithChecker.php
@@ -60,6 +60,18 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ // TODO T175562
+ Context::TYPE_QUALIFIER => [ CheckResult::STATUS_TODO,
false ],
+ Context::TYPE_REFERENCE => [ CheckResult::STATUS_TODO,
false ],
+ ];
+ }
+
+ /**
* Checks 'Conflicts with' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/DiffWithinRangeChecker.php
b/src/ConstraintCheck/Checker/DiffWithinRangeChecker.php
index 3f48e92..f52fc82 100644
--- a/src/ConstraintCheck/Checker/DiffWithinRangeChecker.php
+++ b/src/ConstraintCheck/Checker/DiffWithinRangeChecker.php
@@ -62,6 +62,18 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ // TODO T175565
+ Context::TYPE_QUALIFIER => [ CheckResult::STATUS_TODO,
false ],
+ Context::TYPE_REFERENCE => [ CheckResult::STATUS_TODO,
false ],
+ ];
+ }
+
+ /**
* @param Constraint $constraint
*
* @throws ConstraintParameterException
diff --git a/src/ConstraintCheck/Checker/FormatChecker.php
b/src/ConstraintCheck/Checker/FormatChecker.php
index b3f845c..509f3cf 100644
--- a/src/ConstraintCheck/Checker/FormatChecker.php
+++ b/src/ConstraintCheck/Checker/FormatChecker.php
@@ -63,6 +63,17 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_COMPLIANCE, true ],
+ ];
+ }
+
+ /**
* Checks 'Format' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/InverseChecker.php
b/src/ConstraintCheck/Checker/InverseChecker.php
index 42b9611..3b81432 100644
--- a/src/ConstraintCheck/Checker/InverseChecker.php
+++ b/src/ConstraintCheck/Checker/InverseChecker.php
@@ -63,6 +63,18 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ // TODO T175594
+ Context::TYPE_QUALIFIER => [ CheckResult::STATUS_TODO,
false ],
+ Context::TYPE_REFERENCE => [ CheckResult::STATUS_TODO,
false ],
+ ];
+ }
+
+ /**
* Checks 'Inverse' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/ItemChecker.php
b/src/ConstraintCheck/Checker/ItemChecker.php
index 135a8af..738803b 100644
--- a/src/ConstraintCheck/Checker/ItemChecker.php
+++ b/src/ConstraintCheck/Checker/ItemChecker.php
@@ -59,6 +59,18 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ // TODO T175562
+ Context::TYPE_QUALIFIER => [ CheckResult::STATUS_TODO,
false ],
+ Context::TYPE_REFERENCE => [ CheckResult::STATUS_TODO,
false ],
+ ];
+ }
+
+ /**
* Checks 'Item' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/MandatoryQualifiersChecker.php
b/src/ConstraintCheck/Checker/MandatoryQualifiersChecker.php
index 3a1843a..4aaef1a 100644
--- a/src/ConstraintCheck/Checker/MandatoryQualifiersChecker.php
+++ b/src/ConstraintCheck/Checker/MandatoryQualifiersChecker.php
@@ -42,6 +42,17 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_NOT_IN_SCOPE, false ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_NOT_IN_SCOPE, false ],
+ ];
+ }
+
+ /**
* Checks 'Mandatory qualifiers' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/MultiValueChecker.php
b/src/ConstraintCheck/Checker/MultiValueChecker.php
index e21a53d..afa2851 100644
--- a/src/ConstraintCheck/Checker/MultiValueChecker.php
+++ b/src/ConstraintCheck/Checker/MultiValueChecker.php
@@ -25,6 +25,18 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ // TODO T175566
+ Context::TYPE_QUALIFIER => [ CheckResult::STATUS_TODO,
true ],
+ Context::TYPE_REFERENCE => [ CheckResult::STATUS_TODO,
true ],
+ ];
+ }
+
+ /**
* Checks 'Multi value' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/OneOfChecker.php
b/src/ConstraintCheck/Checker/OneOfChecker.php
index d1215c2..3169679 100644
--- a/src/ConstraintCheck/Checker/OneOfChecker.php
+++ b/src/ConstraintCheck/Checker/OneOfChecker.php
@@ -41,6 +41,17 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_COMPLIANCE, true ],
+ ];
+ }
+
+ /**
* Checks 'One of' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/QualifierChecker.php
b/src/ConstraintCheck/Checker/QualifierChecker.php
index 9095431..9b9b6c2 100644
--- a/src/ConstraintCheck/Checker/QualifierChecker.php
+++ b/src/ConstraintCheck/Checker/QualifierChecker.php
@@ -17,6 +17,17 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_COMPLIANCE, true ],
+ ];
+ }
+
+ /**
* Checks 'Qualifier' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/QualifiersChecker.php
b/src/ConstraintCheck/Checker/QualifiersChecker.php
index ad2c279..ea10235 100644
--- a/src/ConstraintCheck/Checker/QualifiersChecker.php
+++ b/src/ConstraintCheck/Checker/QualifiersChecker.php
@@ -42,6 +42,17 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_NOT_IN_SCOPE, false ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_NOT_IN_SCOPE, false ],
+ ];
+ }
+
+ /**
* Checks 'Qualifiers' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/RangeChecker.php
b/src/ConstraintCheck/Checker/RangeChecker.php
index bfb7b8e..35c977b 100644
--- a/src/ConstraintCheck/Checker/RangeChecker.php
+++ b/src/ConstraintCheck/Checker/RangeChecker.php
@@ -60,6 +60,17 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_COMPLIANCE, true ],
+ ];
+ }
+
+ /**
* Checks 'Range' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/ReferenceChecker.php
b/src/ConstraintCheck/Checker/ReferenceChecker.php
index 9f546a3..e5e9112 100644
--- a/src/ConstraintCheck/Checker/ReferenceChecker.php
+++ b/src/ConstraintCheck/Checker/ReferenceChecker.php
@@ -13,6 +13,17 @@
*/
class ReferenceChecker implements ConstraintChecker {
+ /**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_COMPLIANCE, true ],
+ ];
+ }
+
public function checkConstraint( Context $context, Constraint
$constraint ) {
if ( $context->getType() === Context::TYPE_REFERENCE ) {
return new CheckResult( $context, $constraint, [],
CheckResult::STATUS_COMPLIANCE, '' );
diff --git a/src/ConstraintCheck/Checker/SingleValueChecker.php
b/src/ConstraintCheck/Checker/SingleValueChecker.php
index 95ceaf1..9665366 100644
--- a/src/ConstraintCheck/Checker/SingleValueChecker.php
+++ b/src/ConstraintCheck/Checker/SingleValueChecker.php
@@ -25,6 +25,18 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ // TODO T175566
+ Context::TYPE_QUALIFIER => [ CheckResult::STATUS_TODO,
true ],
+ Context::TYPE_REFERENCE => [ CheckResult::STATUS_TODO,
true ],
+ ];
+ }
+
+ /**
* Checks 'Single value' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/SymmetricChecker.php
b/src/ConstraintCheck/Checker/SymmetricChecker.php
index e806e2f..a97d9f1 100644
--- a/src/ConstraintCheck/Checker/SymmetricChecker.php
+++ b/src/ConstraintCheck/Checker/SymmetricChecker.php
@@ -53,6 +53,18 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ // TODO T175594
+ Context::TYPE_QUALIFIER => [ CheckResult::STATUS_TODO,
false ],
+ Context::TYPE_REFERENCE => [ CheckResult::STATUS_TODO,
false ],
+ ];
+ }
+
+ /**
* Checks 'Symmetric' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/TargetRequiredClaimChecker.php
b/src/ConstraintCheck/Checker/TargetRequiredClaimChecker.php
index acb2ef6..6cedd24 100644
--- a/src/ConstraintCheck/Checker/TargetRequiredClaimChecker.php
+++ b/src/ConstraintCheck/Checker/TargetRequiredClaimChecker.php
@@ -63,6 +63,17 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_COMPLIANCE, true ],
+ ];
+ }
+
+ /**
* Checks 'Target required claim' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/TypeChecker.php
b/src/ConstraintCheck/Checker/TypeChecker.php
index ea1e4c2..4e82cce 100644
--- a/src/ConstraintCheck/Checker/TypeChecker.php
+++ b/src/ConstraintCheck/Checker/TypeChecker.php
@@ -60,6 +60,17 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_COMPLIANCE, false ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_COMPLIANCE, false ],
+ ];
+ }
+
+ /**
* Checks 'Type' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/UniqueValueChecker.php
b/src/ConstraintCheck/Checker/UniqueValueChecker.php
index d921196..de1ecab 100644
--- a/src/ConstraintCheck/Checker/UniqueValueChecker.php
+++ b/src/ConstraintCheck/Checker/UniqueValueChecker.php
@@ -42,6 +42,17 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_COMPLIANCE, true ],
+ ];
+ }
+
+ /**
* Checks 'Unique value' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/Checker/ValueOnlyChecker.php
b/src/ConstraintCheck/Checker/ValueOnlyChecker.php
index 6fb601b..62141aa 100644
--- a/src/ConstraintCheck/Checker/ValueOnlyChecker.php
+++ b/src/ConstraintCheck/Checker/ValueOnlyChecker.php
@@ -13,6 +13,17 @@
*/
class ValueOnlyChecker implements ConstraintChecker {
+ /**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_COMPLIANCE, true ],
+ ];
+ }
+
public function checkConstraint( Context $context, Constraint
$constraint ) {
if ( $context->getType() === Context::TYPE_STATEMENT ) {
return new CheckResult( $context, $constraint, [],
CheckResult::STATUS_COMPLIANCE, '' );
diff --git a/src/ConstraintCheck/Checker/ValueTypeChecker.php
b/src/ConstraintCheck/Checker/ValueTypeChecker.php
index f2f1806..d158343 100644
--- a/src/ConstraintCheck/Checker/ValueTypeChecker.php
+++ b/src/ConstraintCheck/Checker/ValueTypeChecker.php
@@ -73,6 +73,17 @@
}
/**
+ * @codeCoverageIgnore This method is purely declarative.
+ */
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_COMPLIANCE, true ],
+ ];
+ }
+
+ /**
* Checks 'Value type' constraint.
*
* @param Context $context
diff --git a/src/ConstraintCheck/ConstraintChecker.php
b/src/ConstraintCheck/ConstraintChecker.php
index bcbe747..dbcd3a6 100644
--- a/src/ConstraintCheck/ConstraintChecker.php
+++ b/src/ConstraintCheck/ConstraintChecker.php
@@ -17,6 +17,38 @@
interface ConstraintChecker {
/**
+ * Determines which context types this constraint type supports.
+ * checkConstraint() should only be called for contexts with one of the
supported types.
+ *
+ * Returns an array from context types
+ * (i. e., Context::TYPE_* constants)
+ * to arrays of result status
+ * (i. e., CheckResult::STATUS_* constants)
+ * and booleans indicating whether the context is in the default scope
or not.
+ * STATUS_COMPLIANCE means that the constraint type supports this
context type
+ * (checkConstraint() might of course return a different status, e. g.
VIOLATION);
+ * STATUS_TODO means that the constraint type might support this
context type in the future,
+ * but it is not currently supported;
+ * and STATUS_NOT_IN_SCOPE means that the constraint type does not
support this context type.
+ *
+ * For example, the array
+ *
+ * [
+ * Context::TYPE_STATEMENT => [ CheckResult::STATUS_COMPLIANCE,
true ],
+ * Context::TYPE_QUALIFIER => [ CheckResult::STATUS_TODO, false
],
+ * Context::TYPE_REFERENCE => [
CheckResult::STATUS_NOT_IN_SCOPE, false ],
+ * ]
+ *
+ * indicates that a constraint type makes sense on statements and
qualifiers
+ * (but not references), but has only been implemented on statements so
far,
+ * and that it should be checked only on the statement
+ * unless a constraint specifies an explicit scope.
+ *
+ * @return array[]
+ */
+ public function getSupportedContextTypes();
+
+ /**
* @param Context $context
* @param Constraint $constraint
*
diff --git a/tests/phpunit/Fake/FakeChecker.php
b/tests/phpunit/Fake/FakeChecker.php
index b097652..716d5ab 100644
--- a/tests/phpunit/Fake/FakeChecker.php
+++ b/tests/phpunit/Fake/FakeChecker.php
@@ -26,6 +26,14 @@
$this->status = $status;
}
+ public function getSupportedContextTypes() {
+ return [
+ Context::TYPE_STATEMENT => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_QUALIFIER => [
CheckResult::STATUS_COMPLIANCE, true ],
+ Context::TYPE_REFERENCE => [
CheckResult::STATUS_COMPLIANCE, true ],
+ ];
+ }
+
/**
* @see ConstraintChecker::checkConstraint
*/
--
To view, visit https://gerrit.wikimedia.org/r/402882
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I6720b4a984a6ba2daa7425a6de21a30e09af22db
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits