Completed
Push — master ( 89292f...abf49f )
by
unknown
02:20
created

NoneOfChecker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker;
4
5
use WikibaseQuality\ConstraintReport\Constraint;
6
use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker;
7
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
8
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterException;
9
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
10
use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage;
11
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
12
use WikibaseQuality\ConstraintReport\ConstraintParameterRenderer;
13
use WikibaseQuality\ConstraintReport\Role;
14
use Wikibase\DataModel\Statement\Statement;
15
16
/**
17
 * @author Amir Sarabadani
18
 * @license GPL-2.0-or-later
19
 */
20 View Code Duplication
class NoneOfChecker implements ConstraintChecker {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
22
	/**
23
	 * @var ConstraintParameterParser
24
	 */
25
	private $constraintParameterParser;
26
27
	/**
28
	 * @var ConstraintParameterRenderer
29
	 */
30
	private $constraintParameterRenderer;
31
32
	public function __construct(
33
		ConstraintParameterParser $constraintParameterParser,
34
		ConstraintParameterRenderer $constraintParameterRenderer
35
	) {
36
		$this->constraintParameterParser = $constraintParameterParser;
37
		$this->constraintParameterRenderer = $constraintParameterRenderer;
38
	}
39
40
	/**
41
	 * @codeCoverageIgnore This method is purely declarative.
42
	 */
43
	public function getSupportedContextTypes() {
44
		return [
45
			Context::TYPE_STATEMENT => CheckResult::STATUS_COMPLIANCE,
46
			Context::TYPE_QUALIFIER => CheckResult::STATUS_COMPLIANCE,
47
			Context::TYPE_REFERENCE => CheckResult::STATUS_COMPLIANCE,
48
		];
49
	}
50
51
	/**
52
	 * @codeCoverageIgnore This method is purely declarative.
53
	 */
54
	public function getDefaultContextTypes() {
55
		return [
56
			Context::TYPE_STATEMENT,
57
			Context::TYPE_QUALIFIER,
58
			Context::TYPE_REFERENCE,
59
		];
60
	}
61
62
	/**
63
	 * Checks 'None of' constraint.
64
	 *
65
	 * @param Context $context
66
	 * @param Constraint $constraint
67
	 *
68
	 * @throws ConstraintParameterException
69
	 * @return CheckResult
70
	 */
71
	public function checkConstraint( Context $context, Constraint $constraint ) {
72
		if ( $context->getSnakRank() === Statement::RANK_DEPRECATED ) {
73
			return new CheckResult( $context, $constraint, [], CheckResult::STATUS_DEPRECATED );
74
		}
75
76
		$parameters = [];
77
		$constraintParameters = $constraint->getConstraintParameters();
78
79
		$items = $this->constraintParameterParser->parseItemsParameter( $constraintParameters, $constraint->getConstraintTypeItemId(), true );
80
		$parameters['item'] = $items;
81
82
		$snak = $context->getSnak();
83
84
		$message = null;
85
		$status = CheckResult::STATUS_COMPLIANCE;
86
87
		foreach ( $items as $item ) {
88
			if ( $item->matchesSnak( $snak ) ) {
89
				$message = ( new ViolationMessage( 'wbqc-violation-message-none-of' ) )
90
					->withEntityId( $context->getSnak()->getPropertyId(), Role::PREDICATE )
91
					->withItemIdSnakValueList( $items, Role::OBJECT );
92
				$status = CheckResult::STATUS_VIOLATION;
93
				break;
94
			}
95
		}
96
97
		return new CheckResult( $context, $constraint, $parameters, $status, $message );
98
	}
99
100
	public function checkConstraintParameters( Constraint $constraint ) {
101
		$constraintParameters = $constraint->getConstraintParameters();
102
		$exceptions = [];
103
		try {
104
			$this->constraintParameterParser->parseItemsParameter( $constraintParameters, $constraint->getConstraintTypeItemId(), true );
105
		} catch ( ConstraintParameterException $e ) {
106
			$exceptions[] = $e;
107
		}
108
		return $exceptions;
109
	}
110
111
}
112