Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
28 / 30
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SingleValueChecker
93.33% covered (success)
93.33%
28 / 30
75.00% covered (warning)
75.00%
3 / 4
11.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getSupportedContextTypes
n/a
0 / 0
n/a
0 / 0
1
 getDefaultContextTypes
n/a
0 / 0
n/a
0 / 0
1
 getSupportedEntityTypes
n/a
0 / 0
n/a
0 / 0
1
 checkConstraint
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
 getViolationMessage
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 checkConstraintParameters
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
2.15
1<?php
2
3namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker;
4
5use Wikibase\DataModel\Entity\PropertyId;
6use Wikibase\DataModel\Statement\Statement;
7use WikibaseQuality\ConstraintReport\Constraint;
8use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker;
9use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
10use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterException;
11use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
12use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ValueCountCheckerHelper;
13use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage;
14use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
15
16/**
17 * @author BP2014N1
18 * @license GPL-2.0-or-later
19 */
20class SingleValueChecker implements ConstraintChecker {
21
22    /**
23     * @var ConstraintParameterParser
24     */
25    private $constraintParameterParser;
26
27    /**
28     * @var ValueCountCheckerHelper
29     */
30    private $valueCountCheckerHelper;
31
32    public function __construct(
33        ConstraintParameterParser $constraintParameterParser
34    ) {
35        $this->constraintParameterParser = $constraintParameterParser;
36        $this->valueCountCheckerHelper = new ValueCountCheckerHelper();
37    }
38
39    /**
40     * @codeCoverageIgnore This method is purely declarative.
41     */
42    public function getSupportedContextTypes() {
43        return self::ALL_CONTEXT_TYPES_SUPPORTED;
44    }
45
46    /**
47     * @codeCoverageIgnore This method is purely declarative.
48     */
49    public function getDefaultContextTypes() {
50        return Context::ALL_CONTEXT_TYPES;
51    }
52
53    /** @codeCoverageIgnore This method is purely declarative. */
54    public function getSupportedEntityTypes() {
55        return self::ALL_ENTITY_TYPES_SUPPORTED;
56    }
57
58    /**
59     * Checks 'Single value' constraint.
60     *
61     * @param Context $context
62     * @param Constraint $constraint
63     *
64     * @throws ConstraintParameterException
65     * @return CheckResult
66     */
67    public function checkConstraint( Context $context, Constraint $constraint ) {
68        if ( $context->getSnakRank() === Statement::RANK_DEPRECATED ) {
69            return new CheckResult( $context, $constraint, CheckResult::STATUS_DEPRECATED );
70        }
71
72        $separators = $this->constraintParameterParser->parseSeparatorsParameter(
73            $constraint->getConstraintParameters()
74        );
75
76        $propertyId = $context->getSnak()->getPropertyId();
77        $propertyCount = $this->valueCountCheckerHelper->getPropertyCount(
78            $context->getSnakGroup( Context::GROUP_NON_DEPRECATED, $separators ),
79            $propertyId
80        );
81
82        if ( $propertyCount > 1 ) {
83            $message = $this->getViolationMessage( $separators, $propertyId );
84            $status = CheckResult::STATUS_VIOLATION;
85        } else {
86            $message = null;
87            $status = CheckResult::STATUS_COMPLIANCE;
88        }
89
90        return new CheckResult( $context, $constraint, $status, $message );
91    }
92
93    /**
94     * @param PropertyId[] $separators
95     * @param PropertyId $propertyId
96     * @return ViolationMessage
97     */
98    private function getViolationMessage( array $separators, PropertyId $propertyId ) {
99        $messageKey = $separators === [] ?
100            'wbqc-violation-message-single-value' :
101            'wbqc-violation-message-single-value-separators';
102
103        return ( new ViolationMessage( $messageKey ) )
104            ->withEntityId( $propertyId )
105            ->withEntityIdList( $separators );
106    }
107
108    public function checkConstraintParameters( Constraint $constraint ) {
109        $constraintParameters = $constraint->getConstraintParameters();
110        $exceptions = [];
111        try {
112            $this->constraintParameterParser->parseSeparatorsParameter( $constraintParameters );
113        } catch ( ConstraintParameterException $e ) {
114            $exceptions[] = $e;
115        }
116        return $exceptions;
117    }
118
119}