Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
14 / 14
CRAP
100.00% covered (success)
100.00%
1 / 1
CheckResult
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
14 / 14
16
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
 getContextCursor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSnakType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDataValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getConstraint
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getConstraintId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStatus
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setStatus
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMessage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMessage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withMetadata
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getMetadata
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getConstraintClarification
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setConstraintClarification
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare( strict_types = 1 );
4
5namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Result;
6
7use DataValues\DataValue;
8use DataValues\MultilingualTextValue;
9use Wikibase\DataModel\Snak\PropertyValueSnak;
10use WikibaseQuality\ConstraintReport\Constraint;
11use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\Metadata;
12use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
13use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\ContextCursor;
14use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage;
15
16/**
17 * Used for getting information about the result of a constraint check
18 *
19 * @author BP2014N1
20 * @license GPL-2.0-or-later
21 */
22class CheckResult {
23
24    // Constants for statuses
25    /**
26     * The statement satisfies the constraint.
27     */
28    public const STATUS_COMPLIANCE = 'compliance';
29    /**
30     * The statement violates the constraint.
31     */
32    public const STATUS_VIOLATION = 'violation';
33    /**
34     * The subject entity of the statement is a known exception to the constraint.
35     */
36    public const STATUS_EXCEPTION = 'exception';
37    /**
38     * The constraint is not implemented.
39     */
40    public const STATUS_TODO = 'todo';
41    /**
42     * The constraint parameters are broken.
43     */
44    public const STATUS_BAD_PARAMETERS = 'bad-parameters';
45    /**
46     * The constraint has not been checked because the statement is deprecated.
47     */
48    public const STATUS_DEPRECATED = 'deprecated';
49    /**
50     * The statement violates the constraint, but the constraint is not mandatory.
51     *
52     * DelegatingConstraintChecker downgrades violations to warnings automatically based on the constraint parameters;
53     * constraint checkers should not assign this status directly.
54     */
55    public const STATUS_WARNING = 'warning';
56    /**
57     * The statement violates the constraint, but the constraint is just a suggestion.
58     *
59     * DelegatingConstraintChecker downgrades violations to suggestions automatically based on the constraint parameters;
60     * constraint checkers should not assign this status directly.
61     */
62    public const STATUS_SUGGESTION = 'suggestion';
63    /**
64     * The constraint is not checked on this kind of snak
65     * (main snak, qualifier or reference),
66     * so the constraint check is skipped.
67     */
68    public const STATUS_NOT_IN_SCOPE = 'not-in-scope';
69    /*
70     * When adding another status, don’t forget to also do the following:
71     * * define messages for it in i18n/:
72     *   * wbqc-constraintreport-status-
73     *   * apihelp-wbcheckconstraints-paramvalue-status-
74     * * declare a color for it in modules/SpecialConstraintReportPage.less
75     * * update $order in DelegatingConstraintChecker::sortResult
76     * * update PARAM_STATUS type in CheckConstraints::getAllowedParams
77     */
78
79    // phpcs:ignore MediaWiki.Commenting.PropertyDocumentation.WrongStyle
80    private Constraint $constraint;
81    private ContextCursor $contextCursor;
82
83    /**
84     * @var string One of the self::STATUS_… constants
85     */
86    private string $status;
87
88    private ?ViolationMessage $message;
89
90    private Metadata $metadata;
91
92    private ?string $snakType;
93
94    private ?DataValue $dataValue;
95
96    private MultilingualTextValue $constraintClarification;
97
98    /**
99     * @param Context|ContextCursor $contextCursor
100     * @param Constraint $constraint
101     * @param string $status One of the self::STATUS_… constants
102     * @param ViolationMessage|null $message
103     */
104    public function __construct(
105        $contextCursor,
106        Constraint $constraint,
107        string $status = self::STATUS_TODO,
108        ?ViolationMessage $message = null
109    ) {
110        if ( $contextCursor instanceof Context ) {
111            $context = $contextCursor;
112            $this->contextCursor = $context->getCursor();
113            $this->snakType = $context->getSnak()->getType();
114            $mainSnak = $context->getSnak();
115            if ( $mainSnak instanceof PropertyValueSnak ) {
116                $this->dataValue = $mainSnak->getDataValue();
117            } else {
118                $this->dataValue = null;
119            }
120        } else {
121            $this->contextCursor = $contextCursor;
122            $this->snakType = null;
123            $this->dataValue = null;
124        }
125        $this->constraint = $constraint;
126        $this->status = $status;
127        $this->message = $message;
128        $this->metadata = Metadata::blank();
129        $this->constraintClarification = new MultilingualTextValue( [] );
130    }
131
132    public function getContextCursor(): ContextCursor {
133        return $this->contextCursor;
134    }
135
136    /**
137     * @return string|null only available if the CheckResult was created from a full Context
138     */
139    public function getSnakType(): ?string {
140        return $this->snakType;
141    }
142
143    /**
144     * @return DataValue|null only available if the CheckResult was created from a full Context
145     */
146    public function getDataValue(): ?DataValue {
147        return $this->dataValue;
148    }
149
150    public function getConstraint(): Constraint {
151        return $this->constraint;
152    }
153
154    public function getConstraintId(): string {
155        return $this->constraint->getConstraintId();
156    }
157
158    /**
159     * @return string One of the self::STATUS_… constants
160     */
161    public function getStatus(): string {
162        return $this->status;
163    }
164
165    public function setStatus( string $status ): void {
166        $this->status = $status;
167    }
168
169    public function getMessage(): ?ViolationMessage {
170        return $this->message;
171    }
172
173    public function setMessage( ?ViolationMessage $message ) {
174        $this->message = $message;
175    }
176
177    public function withMetadata( Metadata $metadata ): self {
178        $this->metadata = $metadata;
179        return $this;
180    }
181
182    public function getMetadata(): Metadata {
183        return $this->metadata;
184    }
185
186    public function getConstraintClarification(): MultilingualTextValue {
187        return $this->constraintClarification;
188    }
189
190    public function setConstraintClarification( MultilingualTextValue $constraintClarification ) {
191        $this->constraintClarification = $constraintClarification;
192    }
193
194}