Completed
Push — master ( 00c6d5...91d4a5 )
by
unknown
06:31 queued 11s
created

CheckingResultsBuilder   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 157
Duplicated Lines 18.47 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 15
dl 29
loc 157
rs 9.1666
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
C getResults() 29 43 7
A defaultResultsPerContext() 0 5 2
A defaultResultsPerEntity() 0 3 1
A statusSelected() 0 4 2
B checkResultToArray() 0 41 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace WikibaseQuality\ConstraintReport\Api;
4
5
use Config;
6
use Wikibase\DataModel\Entity\EntityId;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\Services\EntityId\EntityIdFormatter;
10
use Wikibase\Lib\Store\EntityTitleLookup;
11
use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\Metadata;
12
use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedCheckConstraintsResponse;
13
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
14
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\EntityContextCursor;
15
use WikibaseQuality\ConstraintReport\ConstraintCheck\DelegatingConstraintChecker;
16
use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessageRenderer;
17
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
18
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\NullResult;
19
use WikibaseQuality\ConstraintReport\ConstraintParameterRenderer;
20
21
/**
22
 * @author Lucas Werkmeister
23
 * @license GPL-2.0-or-later
24
 */
25
class CheckingResultsBuilder implements ResultsBuilder {
26
27
	/**
28
	 * @var DelegatingConstraintChecker
29
	 */
30
	private $delegatingConstraintChecker;
31
32
	/**
33
	 * @var EntityTitleLookup
34
	 */
35
	private $entityTitleLookup;
36
37
	/**
38
	 * @var EntityIdFormatter
39
	 */
40
	private $entityIdLabelFormatter;
41
42
	/**
43
	 * @var ConstraintParameterRenderer
44
	 */
45
	private $constraintParameterRenderer;
46
47
	/**
48
	 * @var ViolationMessageRenderer
49
	 */
50
	private $violationMessageRenderer;
51
52
	/**
53
	 * @var Config
54
	 */
55
	private $config;
56
57
	public function __construct(
58
		DelegatingConstraintChecker $delegatingConstraintChecker,
59
		EntityTitleLookup $entityTitleLookup,
60
		EntityIdFormatter $entityIdLabelFormatter,
61
		ConstraintParameterRenderer $constraintParameterRenderer,
62
		ViolationMessageRenderer $violationMessageRenderer,
63
		Config $config
64
	) {
65
		$this->delegatingConstraintChecker = $delegatingConstraintChecker;
66
		$this->entityTitleLookup = $entityTitleLookup;
67
		$this->entityIdLabelFormatter = $entityIdLabelFormatter;
68
		$this->constraintParameterRenderer = $constraintParameterRenderer;
69
		$this->violationMessageRenderer = $violationMessageRenderer;
70
		$this->config = $config;
71
	}
72
73
	/**
74
	 * @param EntityId[] $entityIds
75
	 * @param string[] $claimIds
76
	 * @param string[]|null $constraintIds
77
	 * @param string[] $statuses
78
	 * @return CachedCheckConstraintsResponse
79
	 */
80
	public function getResults(
81
		array $entityIds,
82
		array $claimIds,
83
		array $constraintIds = null,
84
		array $statuses
85
	) {
86
		$response = [];
87
		$metadatas = [];
88
		$statusesFlipped = array_flip( $statuses );
89 View Code Duplication
		foreach ( $entityIds as $entityId ) {
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...
90
			$results = $this->delegatingConstraintChecker->checkAgainstConstraintsOnEntityId(
91
				$entityId,
92
				$constraintIds,
93
				[ $this, 'defaultResultsPerContext' ],
94
				[ $this, 'defaultResultsPerEntity' ]
95
			);
96
			foreach ( $results as $result ) {
97
				$metadatas[] = $result->getMetadata();
98
				if ( $this->statusSelected( $statusesFlipped, $result ) ) {
99
					$resultArray = $this->checkResultToArray( $result );
100
					$result->getContextCursor()->storeCheckResultInArray( $resultArray, $response );
101
				}
102
			}
103
		}
104 View Code Duplication
		foreach ( $claimIds as $claimId ) {
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...
105
			$results = $this->delegatingConstraintChecker->checkAgainstConstraintsOnClaimId(
106
				$claimId,
107
				$constraintIds,
108
				[ $this, 'defaultResultsPerContext' ]
109
			);
110
			foreach ( $results as $result ) {
111
				$metadatas[] = $result->getMetadata();
112
				if ( $this->statusSelected( $statusesFlipped, $result ) ) {
113
					$resultArray = $this->checkResultToArray( $result );
114
					$result->getContextCursor()->storeCheckResultInArray( $resultArray, $response );
115
				}
116
			}
117
		}
118
		return new CachedCheckConstraintsResponse(
119
			$response,
120
			Metadata::merge( $metadatas )
121
		);
122
	}
123
124
	public function defaultResultsPerContext( Context $context ) {
125
		return $context->getType() === Context::TYPE_STATEMENT ?
126
			[ new NullResult( $context->getCursor() ) ] :
127
			[];
128
	}
129
130
	public function defaultResultsPerEntity( EntityId $entityId ) {
131
		return [ new NullResult( new EntityContextCursor( $entityId->getSerialization() ) ) ];
132
	}
133
134
	public function statusSelected( array $statusesFlipped, CheckResult $result ) {
135
		return array_key_exists( $result->getStatus(), $statusesFlipped ) ||
136
			$result instanceof NullResult;
137
	}
138
139
	public function checkResultToArray( CheckResult $checkResult ) {
140
		if ( $checkResult instanceof NullResult ) {
141
			return null;
142
		}
143
144
		$constraintId = $checkResult->getConstraint()->getConstraintId();
145
		$typeItemId = $checkResult->getConstraint()->getConstraintTypeItemId();
146
		$constraintPropertyId = new PropertyId( $checkResult->getContextCursor()->getSnakPropertyId() );
147
148
		$title = $this->entityTitleLookup->getTitleForId( $constraintPropertyId );
149
		$typeLabel = $this->entityIdLabelFormatter->formatEntityId( new ItemId( $typeItemId ) );
150
		// TODO link to the statement when possible (T169224)
151
		$link = $title->getFullURL() . '#' . $this->config->get( 'WBQualityConstraintsPropertyConstraintId' );
152
153
		$constraint = [
154
			'id' => $constraintId,
155
			'type' => $typeItemId,
156
			'typeLabel' => $typeLabel,
157
			'link' => $link,
158
			'discussLink' => $title->getTalkPage()->getFullURL(),
159
		];
160
161
		$result = [
162
			'status' => $checkResult->getStatus(),
163
			'property' => $constraintPropertyId->getSerialization(),
164
			'constraint' => $constraint
165
		];
166
		$message = $checkResult->getMessage();
167
		if ( $message ) {
168
			$result['message-html'] = $this->violationMessageRenderer->render( $message );
169
		}
170
		if ( $checkResult->getContextCursor()->getType() === Context::TYPE_STATEMENT ) {
171
			$result['claim'] = $checkResult->getContextCursor()->getStatementGuid();
172
		}
173
		$cachingMetadataArray = $checkResult->getMetadata()->getCachingMetadata()->toArray();
174
		if ( $cachingMetadataArray !== null ) {
175
			$result['cached'] = $cachingMetadataArray;
176
		}
177
178
		return $result;
179
	}
180
181
}
182