Code Duplication    Length = 92-92 lines in 2 locations

src/ConstraintCheck/Checker/NoneOfChecker.php 1 location

@@ 20-111 (lines=92) @@
17
 * @author Amir Sarabadani
18
 * @license GPL-2.0-or-later
19
 */
20
class NoneOfChecker implements ConstraintChecker {
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

src/ConstraintCheck/Checker/OneOfChecker.php 1 location

@@ 20-111 (lines=92) @@
17
 * @author BP2014N1
18
 * @license GPL-2.0-or-later
19
 */
20
class OneOfChecker implements ConstraintChecker {
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 'One 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 = ( new ViolationMessage( 'wbqc-violation-message-one-of' ) )
85
			->withEntityId( $context->getSnak()->getPropertyId(), Role::PREDICATE )
86
			->withItemIdSnakValueList( $items, Role::OBJECT );
87
		$status = CheckResult::STATUS_VIOLATION;
88
89
		foreach ( $items as $item ) {
90
			if ( $item->matchesSnak( $snak ) ) {
91
				$message = null;
92
				$status = CheckResult::STATUS_COMPLIANCE;
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