Completed
Push — master ( a0ade2...0d2db5 )
by
unknown
05:31
created

findCitationResourceLinks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
ccs 13
cts 13
cp 1
cc 2
eloc 12
nc 2
nop 2
crap 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 1
	public function findCitationResourceLinks( array $subjects, $linkClass = '' ) {
53
54 1
		$citationResourceLinks = array();
55
56 1
		foreach ( $subjects as $subject ) {
57
58 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...
59 1
				$subject,
60
				null
61 1
			);
62
63 1
			$browselink = \SMWInfolink::newBrowsingLink(
64 1
				'&#8593;', // ↑; $reference,
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65 1
				$dataValue->getWikiValue(),
66
				$linkClass
67 1
			);
68
69 1
			$citationResourceLinks[] = $browselink->getHTML();
70 1
		}
71
72 1
		return $citationResourceLinks;
73
	}
74
75
	/**
76
	 * Find match for [[OCLC::SomeOclcKey]]
77
	 *
78
	 * @since 1.0
79
	 *
80
	 * @param string $type
81
	 * @param string|null $id
82
	 *
83
	 * @return array
84
	 */
85 6
	public function findMatchForResourceIdentifierTypeToValue( $type, $id = null ) {
86
87 6
		if ( $id === null || $id === '' ) {
88
			return array();
89
		}
90
91 6
		$resourceIdentifierFactory = new ResourceIdentifierFactory();
92
93 6
		$resourceIdentifierStringValue = $resourceIdentifierFactory->newResourceIdentifierStringValueForType( $type );
94 6
		$resourceIdentifierStringValue->setUserValue( $id );
95 6
		$id = $resourceIdentifierStringValue->getWikiValue();
96
97 6
		$description = new SomeProperty(
98 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...
99 6
			new ValueDescription( new DIBlob( $id ) )
100 6
		);
101
102 6
		$query = new Query(
103 6
			$description,
104 6
			false,
105
			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...
106 6
		);
107
108 6
		$query->querymode = Query::MODE_INSTANCES;
109 6
		$query->setLimit( 10 );
110
111 6
		if ( defined( 'SMWQuery::PROC_CONTEXT' ) ) {
112 6
			$query->setOption( Query::PROC_CONTEXT, 'SCI.CitationResourceMatchFinder' );
113 6
		}
114
115 6
		return $this->store->getQueryResult( $query )->getResults();
116
	}
117
118
	/**
119
	 * @since 1.0
120
	 *
121
	 * @param string $citationReference
122
	 *
123
	 * @return array
124
	 */
125 8
	public function findCitationTextFor( $citationReference ) {
126
127 8
		$text = '';
128 8
		$subjects = array();
129
130 8
		$queryResult = $this->findMatchForCitationReference(
131
			$citationReference
132 8
		);
133
134 8
		if ( !$queryResult instanceof \SMWQueryResult ) {
135
			return array( $subjects, $text );
136
		}
137
138 8
		while ( $resultArray = $queryResult->getNext() ) {
139 8
			foreach ( $resultArray as $result ) {
140
141
				// Collect all subjects for the same reference because it can happen
142
				// that the same reference key is used for different citation
143
				// resources therefore only return one (the last) valid citation
144
				// text but return all subjects to make it easier to find them later
145 8
				$subjects[] = $result->getResultSubject();
146
147 8
				while ( ( $dataValue = $result->getNextDataValue() ) !== false ) {
148 8
					$text = $dataValue->getShortWikiText();
149 8
				}
150 8
			}
151 8
		}
152
153 8
		return array( $subjects, $text );
154
	}
155
156
	/**
157
	 * Find match for [[Citation key::SomeKey]]|?Citation text
158
	 *
159
	 * @since 1.0
160
	 *
161
	 * @param string $citationReference
162
	 *
163
	 * @return QueryResult
164
	 */
165 8
	public function findMatchForCitationReference( $citationReference ) {
166
167 8
		$description = new SomeProperty(
168 8
			new DIProperty( PropertyRegistry::SCI_CITE_KEY ),
169 8
			new ValueDescription( new DIBlob( $citationReference ) )
170 8
		);
171
172 8
		$propertyValue = $this->dataValueFactory->newDataValueByType( '__pro' );
173 8
		$propertyValue->setDataItem(
174 8
			new DIProperty( PropertyRegistry::SCI_CITE_TEXT )
175 8
		);
176
177 8
		$description->addPrintRequest(
178 8
			new PrintRequest( PrintRequest::PRINT_PROP, null, $propertyValue )
179 8
		);
180
181 8
		$query = new Query(
182 8
			$description,
183 8
			false,
184
			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...
185 8
		);
186
187 8
		$query->querymode = Query::MODE_INSTANCES;
188 8
		$query->setLimit( 10 );
189
190 8
		if ( defined( 'SMWQuery::PROC_CONTEXT' ) ) {
191 8
			$query->setOption( Query::PROC_CONTEXT, 'SCI.CitationResourceMatchFinder' );
192 8
		}
193
194 8
		return $this->store->getQueryResult( $query );
195
	}
196
197
}
198