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