Completed
Pull Request — master (#22)
by mw
03:56
created

CitationMeta::tryToCollectCitationFrequency()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 33
ccs 0
cts 21
cp 0
rs 8.439
cc 5
eloc 19
nc 4
nop 1
crap 30
1
<?php
2
3
namespace SCI;
4
5
use SMW\Subobject;
6
use SMW\DIWikiPage;
7
use SMW\DIProperty;
8
use SMWDIContainer as DIContainer;
9
use SMWContainerSemanticData as ContainerSemanticData;
10
use SMW\DataValueFactory;
11
use SMW\SemanticData;
12
13
/**
14
 * @license GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author mwjames
18
 */
19
class CitationMeta {
20
21
	/**
22
	 * @var CitationReferencePositionJournal
23
	 */
24
	private $citationReferencePositionJournal = null;
25
26
	/**
27
	 * @var boolean
28
	 */
29
	private $isEnabled = false;
30
31
	/**
32
	 * @since 1.1
33
	 *
34
	 * @param CitationReferencePositionJournal $citationReferencePositionJournal
35
	 */
36 14
	public function __construct( CitationReferencePositionJournal $citationReferencePositionJournal ) {
37 14
		$this->citationReferencePositionJournal = $citationReferencePositionJournal;
38 14
	}
39
40
	/**
41
	 * @since 1.1
42
	 *
43
	 * @param $isEnabled boolean
44
	 */
45 14
	public function setEnabledState( $isEnabled ) {
46 14
		$this->isEnabled = (bool)$isEnabled;
47 14
	}
48
49
	/**
50
	 * @since 1.1
51
	 *
52
	 * @param SemanticData $semanticData
53
	 *
54
	 * @return boolean
55
	 */
56 14
	public function addMetaRecordToSemanticData( SemanticData $semanticData ) {
57
58 14
		if ( !$this->isEnabled ) {
59 14
			return false;
60
		}
61
62
		$containerSemanticData = $this->tryToCollectCitationFrequency(
63
			$semanticData->getSubject()
64
		);
65
66
		if ( $containerSemanticData === null || $containerSemanticData->isEmpty() ) {
67
			return false;
68
		}
69
70
		$semanticData->addPropertyObjectValue(
71
			new DIProperty( PropertyRegistry::SCI_CITE_META ),
72
			new DIContainer( $containerSemanticData )
73
		);
74
75
		return true;
76
	}
77
78
	private function tryToCollectCitationFrequency( DIWikiPage $subject ) {
79
80
		$journal = $this->citationReferencePositionJournal->getJournalBySubject( $subject );
81
82
		if ( $journal === array() || !isset( $journal['reference-list'] ) ) {
83
			return null;
84
		}
85
86
		$subWikiPage = new DIWikiPage(
87
			$subject->getDBkey(),
88
			$subject->getNamespace(),
89
			$subject->getInterwiki(),
90
			'sci.meta'
91
		);
92
93
		$containerSemanticData = new ContainerSemanticData( $subWikiPage );
94
95
		foreach ( $journal['reference-list'] as $hash => $citationKey ) {
96
97
			if ( !isset( $journal['reference-pos'][$hash] ) ) {
98
				continue;
99
			}
100
101
			$this->addFrequencyRecord(
102
				$containerSemanticData,
103
				$subject,
104
				$citationKey,
105
				count( $journal['reference-pos'][$hash] )
106
			);
107
		}
108
109
		return $containerSemanticData;
110
	}
111
112
	private function addFrequencyRecord( $containerSemanticData, $subject, $citationKey, $count ) {
113
114
		$dataValue = DataValueFactory::getInstance()->newPropertyObjectValue(
115
			new DIProperty( PropertyRegistry::SCI_CITE_FREQUENCY ),
116
			$citationKey . ';' . $count,
0 ignored issues
show
Documentation introduced by
$citationKey . ';' . $count is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
			false,
118
			$subject
119
		);
120
121
		$containerSemanticData->addDataValue( $dataValue );
122
	}
123
124
}
125