Completed
Pull Request — master (#52)
by mw
02:11
created

CitationResourceMatchFinder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 97.5%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 13
dl 0
loc 183
ccs 78
cts 80
cp 0.975
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B findCitationResourceLinks() 0 25 3
B findMatchForResourceIdentifierTypeToValue() 0 36 5
B findCitationTextFor() 0 30 5
B findMatchForCitationReference() 0 31 2
1
<?php
2
3
namespace SCI;
4
5
use SMW\Query\Language\SomeProperty;
6
use SMW\Query\Language\ValueDescription;
7
use SMW\Query\PrintRequest;
8
use SMW\Store;
9
use SMW\DIProperty;
10
use SMWPropertyValue as PropertyValue;
11
use SMWQuery as Query;
12
use SMWDIBlob as DIBlob;
13
use SMW\DataValueFactory;
14
use SCI\DataValues\ResourceIdentifierFactory;
15
16
/**
17
 * @license GNU GPL v2+
18
 * @since 2.2
19
 *
20
 * @author mwjames
21
 */
22
class CitationResourceMatchFinder {
23
24
	/**
25
	 * @var Store
26
	 */
27
	private $store;
28
29
	/**
30
	 * @var DataValueFactory
31
	 */
32
	private $dataValueFactory;
33
34
	/**
35
	 * @since 1.0
36
	 *
37
	 * @param Store $store
38
	 */
39 16
	public function __construct( Store $store ) {
40 16
		$this->store = $store;
41 16
		$this->dataValueFactory = DataValueFactory::getInstance();
42 16
	}
43
44
	/**
45
	 * @since 1.0
46
	 *
47
	 * @param array $subjects
48
	 * @param string $linkClass
49
	 *
50
	 * @return array
51
	 */
52 2
	public function findCitationResourceLinks( array $subjects, $linkClass = '', $caption = '' ) {
53
54 1
		$citationResourceLinks = array();
55
56
		// ↑; $reference
57 1
		$caption = $caption !== '' ? $caption : '&#8593;';
58
59 1
		foreach ( $subjects as $subject ) {
60
61 1
			$dataValue = $this->dataValueFactory->newDataItemValue(
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::newDataItemValue() has been deprecated with message: since 2.4, use DataValueFactory::newDataValueByItem

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
62 1
				$subject,
63
				null
64 1
			);
65
66 1
			$browselink = \SMWInfolink::newBrowsingLink(
67 1
				$caption,
68 2
				$dataValue->getWikiValue(),
69
				$linkClass
70 1
			);
71
72 1
			$citationResourceLinks[] = $browselink->getHTML();
73 1
		}
74
75 1
		return $citationResourceLinks;
76
	}
77
78
	/**
79
	 * Find match for [[OCLC::SomeOclcKey]]
80
	 *
81
	 * @since 1.0
82
	 *
83
	 * @param string $type
84
	 * @param string|null $id
85
	 *
86
	 * @return array
87
	 */
88 6
	public function findMatchForResourceIdentifierTypeToValue( $type, $id = null ) {
89
90 6
		if ( $id === null || $id === '' ) {
91
			return array();
92
		}
93
94 6
		$resourceIdentifierFactory = new ResourceIdentifierFactory();
95
96 6
		$resourceIdentifierStringValue = $resourceIdentifierFactory->newResourceIdentifierStringValueForType( $type );
97 6
		$resourceIdentifierStringValue->setUserValue( $id );
98 6
		$id = $resourceIdentifierStringValue->getWikiValue();
99
100 6
		$description = new SomeProperty(
101 6
			$resourceIdentifierStringValue->getProperty(),
0 ignored issues
show
Bug introduced by
It seems like $resourceIdentifierStringValue->getProperty() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
102 6
			new ValueDescription( new DIBlob( $id ) )
103 6
		);
104
105 6
		$query = new Query(
106 6
			$description,
107 6
			false,
108
			false
0 ignored issues
show
Unused Code introduced by
The call to SMWQuery::__construct() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
109 6
		);
110
111 6
		$query->querymode = Query::MODE_INSTANCES;
112 6
		$query->setLimit( 10 );
113
114 6
		if ( defined( 'SMWQuery::NO_CACHE' ) ) {
115 6
			$query->setOption( Query::NO_CACHE, true );
116 6
		}
117
118 6
		if ( defined( 'SMWQuery::PROC_CONTEXT' ) ) {
119 6
			$query->setOption( Query::PROC_CONTEXT, 'SCI.CitationResourceMatchFinder' );
120 6
		}
121
122 6
		return $this->store->getQueryResult( $query )->getResults();
123
	}
124
125
	/**
126
	 * @since 1.0
127
	 *
128
	 * @param string $citationReference
129
	 *
130
	 * @return array
131
	 */
132 8
	public function findCitationTextFor( $citationReference ) {
133
134 8
		$text = '';
135 8
		$subjects = array();
136
137 8
		$queryResult = $this->findMatchForCitationReference(
138
			$citationReference
139 8
		);
140
141 8
		if ( !$queryResult instanceof \SMWQueryResult ) {
142
			return array( $subjects, $text );
143
		}
144
145 8
		while ( $resultArray = $queryResult->getNext() ) {
146 8
			foreach ( $resultArray as $result ) {
147
148
				// Collect all subjects for the same reference because it can happen
149
				// that the same reference key is used for different citation
150
				// resources therefore only return one (the last) valid citation
151
				// text but return all subjects to make it easier to find them later
152 8
				$subjects[] = $result->getResultSubject();
153
154 8
				while ( ( $dataValue = $result->getNextDataValue() ) !== false ) {
155 8
					$text = $dataValue->getShortWikiText();
156 8
				}
157 8
			}
158 8
		}
159
160 8
		return array( $subjects, $text );
161
	}
162
163
	/**
164
	 * Find match for [[Citation key::SomeKey]]|?Citation text
165
	 *
166
	 * @since 1.0
167
	 *
168
	 * @param string $citationReference
169
	 *
170
	 * @return QueryResult
171
	 */
172 9
	public function findMatchForCitationReference( $citationReference ) {
173
174 8
		$description = new SomeProperty(
175 8
			new DIProperty( PropertyRegistry::SCI_CITE_KEY ),
176 8
			new ValueDescription( new DIBlob( $citationReference ) )
177 8
		);
178
179 8
		$propertyValue = $this->dataValueFactory->newDataValueByType( '__pro' );
180 8
		$propertyValue->setDataItem(
181 8
			new DIProperty( PropertyRegistry::SCI_CITE_TEXT )
182 9
		);
183
184 8
		$description->addPrintRequest(
185 8
			new PrintRequest( PrintRequest::PRINT_PROP, null, $propertyValue )
186 8
		);
187
188 8
		$query = new Query(
189 8
			$description,
190 8
			false,
191
			false
0 ignored issues
show
Unused Code introduced by
The call to SMWQuery::__construct() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
192 8
		);
193
194 8
		$query->querymode = Query::MODE_INSTANCES;
195 8
		$query->setLimit( 10 );
196
197 8
		if ( defined( 'SMWQuery::PROC_CONTEXT' ) ) {
198 8
			$query->setOption( Query::PROC_CONTEXT, 'SCI.CitationResourceMatchFinder' );
199 8
		}
200
201 8
		return $this->store->getQueryResult( $query );
202
	}
203
204
}
205