Completed
Push — master ( 866e51...05d0de )
by
unknown
02:58 queued 10s
created

CheckResultsRenderer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 44.09 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 11
dl 41
loc 93
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A render() 0 11 2
B checkResultToArray() 41 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
4
namespace WikibaseQuality\ConstraintReport\Api;
5
6
use Config;
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\CachedCheckConstraintsResponse;
12
use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedCheckResults;
13
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
14
use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessageRenderer;
15
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
16
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\NullResult;
17
18
/**
19
 * Converts check results into arrays and stores them inside one big response array.
20
 *
21
 * @author Lucas Werkmeister
22
 * @license GPL-2.0-or-later
23
 */
24
class CheckResultsRenderer {
25
26
	/**
27
	 * @var EntityTitleLookup
28
	 */
29
	private $entityTitleLookup;
30
31
	/**
32
	 * @var EntityIdFormatter
33
	 */
34
	private $entityIdLabelFormatter;
35
36
	/**
37
	 * @var ViolationMessageRenderer
38
	 */
39
	private $violationMessageRenderer;
40
41
	/**
42
	 * @var Config
43
	 */
44
	private $config;
45
46
	public function __construct(
47
		EntityTitleLookup $entityTitleLookup,
48
		EntityIdFormatter $entityIdLabelFormatter,
49
		ViolationMessageRenderer $violationMessageRenderer,
50
		Config $config
51
	) {
52
		$this->entityTitleLookup = $entityTitleLookup;
53
		$this->entityIdLabelFormatter = $entityIdLabelFormatter;
54
		$this->violationMessageRenderer = $violationMessageRenderer;
55
		$this->config = $config;
56
	}
57
58
	/**
59
	 * @param CachedCheckResults $checkResults
60
	 * @return CachedCheckConstraintsResponse
61
	 */
62
	public function render( CachedCheckResults $checkResults ) {
63
		$response = [];
64
		foreach ( $checkResults->getArray() as $checkResult ) {
65
			$resultArray = $this->checkResultToArray( $checkResult );
66
			$checkResult->getContextCursor()->storeCheckResultInArray( $resultArray, $response );
67
		}
68
		return new CachedCheckConstraintsResponse(
69
			$response,
70
			$checkResults->getMetadata()
71
		);
72
	}
73
74 View Code Duplication
	public function checkResultToArray( CheckResult $checkResult ) {
0 ignored issues
show
Duplication introduced by
This method 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...
75
		if ( $checkResult instanceof NullResult ) {
76
			return null;
77
		}
78
79
		$constraintId = $checkResult->getConstraint()->getConstraintId();
80
		$typeItemId = $checkResult->getConstraint()->getConstraintTypeItemId();
81
		$constraintPropertyId = new PropertyId( $checkResult->getContextCursor()->getSnakPropertyId() );
82
83
		$title = $this->entityTitleLookup->getTitleForId( $constraintPropertyId );
84
		$typeLabel = $this->entityIdLabelFormatter->formatEntityId( new ItemId( $typeItemId ) );
85
		// TODO link to the statement when possible (T169224)
86
		$link = $title->getFullURL() . '#' . $this->config->get( 'WBQualityConstraintsPropertyConstraintId' );
87
88
		$constraint = [
89
			'id' => $constraintId,
90
			'type' => $typeItemId,
91
			'typeLabel' => $typeLabel,
92
			'link' => $link,
93
			'discussLink' => $title->getTalkPage()->getFullURL(),
94
		];
95
96
		$result = [
97
			'status' => $checkResult->getStatus(),
98
			'property' => $constraintPropertyId->getSerialization(),
99
			'constraint' => $constraint
100
		];
101
		$message = $checkResult->getMessage();
102
		if ( $message ) {
103
			$result['message-html'] = $this->violationMessageRenderer->render( $message );
104
		}
105
		if ( $checkResult->getContextCursor()->getType() === Context::TYPE_STATEMENT ) {
106
			$result['claim'] = $checkResult->getContextCursor()->getStatementGuid();
107
		}
108
		$cachingMetadataArray = $checkResult->getMetadata()->getCachingMetadata()->toArray();
109
		if ( $cachingMetadataArray !== null ) {
110
			$result['cached'] = $cachingMetadataArray;
111
		}
112
113
		return $result;
114
	}
115
116
}
117