Completed
Push — master ( db833a...340657 )
by
unknown
04:20
created

PropertyScopeChecker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
13
/**
14
 * @author Lucas Werkmeister
15
 * @license GPL-2.0-or-later
16
 */
17
class PropertyScopeChecker implements ConstraintChecker {
18
19
	/**
20
	 * @var ConstraintParameterParser
21
	 */
22
	private $constraintParameterParser;
23
24
	public function __construct(
25
		ConstraintParameterParser $constraintParameterParser
26
	) {
27
		$this->constraintParameterParser = $constraintParameterParser;
28
	}
29
30
	/**
31
	 * @codeCoverageIgnore This method is purely declarative.
32
	 */
33
	public function getSupportedContextTypes() {
34
		return [
35
			Context::TYPE_STATEMENT => CheckResult::STATUS_COMPLIANCE,
36
			Context::TYPE_QUALIFIER => CheckResult::STATUS_COMPLIANCE,
37
			Context::TYPE_REFERENCE => CheckResult::STATUS_COMPLIANCE,
38
		];
39
	}
40
41
	/**
42
	 * @codeCoverageIgnore This method is purely declarative.
43
	 */
44
	public function getDefaultContextTypes() {
45
		return [
46
			Context::TYPE_STATEMENT,
47
			Context::TYPE_QUALIFIER,
48
			Context::TYPE_REFERENCE,
49
		];
50
	}
51
52
	public function checkConstraint( Context $context, Constraint $constraint ) {
53
		$constraintParameters = $constraint->getConstraintParameters();
54
		$constraintTypeItemId = $constraint->getConstraintTypeItemId();
55
56
		$allowedContextTypes = $this->constraintParameterParser->parsePropertyScopeParameter(
57
			$constraintParameters,
58
			$constraintTypeItemId
59
		);
60
61 View Code Duplication
		if ( in_array( $context->getType(), $allowedContextTypes ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62
			return new CheckResult(
63
				$context->getCursor(),
64
				$constraint,
65
				[],
66
				CheckResult::STATUS_COMPLIANCE
67
			);
68
		} else {
69
			return new CheckResult(
70
				$context->getCursor(),
71
				$constraint,
72
				[],
73
				CheckResult::STATUS_VIOLATION,
74
				( new ViolationMessage( 'wbqc-violation-message-property-scope' ) )
75
					->withEntityId( $context->getSnak()->getPropertyId() )
76
					->withPropertyScope( $context->getType() )
77
					->withPropertyScopeList( $allowedContextTypes )
78
			);
79
		}
80
	}
81
82
	public function checkConstraintParameters( Constraint $constraint ) {
83
		$constraintParameters = $constraint->getConstraintParameters();
84
		$constraintTypeItemId = $constraint->getConstraintTypeItemId();
85
		$exceptions = [];
86
		try {
87
			$this->constraintParameterParser->parsePropertyScopeParameter(
88
				$constraintParameters,
89
				$constraintTypeItemId
90
			);
91
		} catch ( ConstraintParameterException $e ) {
92
			$exceptions[] = $e;
93
		}
94
		return $exceptions;
95
	}
96
97
}
98