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