CitationReferenceValue   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 82.54%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
eloc 64
c 2
b 0
f 0
dl 0
loc 149
ccs 52
cts 63
cp 0.8254
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setCaptionFormat() 0 2 1
B getShortWikiText() 0 57 8
B parseUserValue() 0 40 7
A __construct() 0 5 1
1
<?php
2
3
namespace SCI\DataValues;
4
5
use SCI\CitationReferencePositionJournal;
6
use SMW\DataValues\StringValue;
0 ignored issues
show
Bug introduced by
The type SMW\DataValues\StringValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SMWDIBlob as DIBlob;
0 ignored issues
show
Bug introduced by
The type SMWDIBlob was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Html;
0 ignored issues
show
Bug introduced by
The type Html was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class CitationReferenceValue extends StringValue {
17
18
	/**
19
	 * @var integer
20
	 */
21
	private $captionFormat;
22
23
	/**
24
	 * @var CitationReferencePositionJournal
25
	 */
26
	private $citationReferencePositionJournal;
27
28
	/**
29
	 * @var string
30
	 */
31
	private $reference;
32
33
	/**
34
	 * To display something like [[CiteRef::Foo, 1970|:50-52]] as [1]:50-52 for
35
	 * SCI_CITEREF_NUM
36
	 *
37
	 * @var boolean
38
	 */
39
	private $usesShortFormCaption = false;
40
41
	/**
42
	 * @param string $typeid
43
	 */
44 10
	public function __construct( $typeid = '' ) {
45 10
		parent::__construct( '_sci_ref' );
46
47
		// Currently there is no good way to inject the setting
48 10
		$this->captionFormat = $GLOBALS['scigCitationReferenceCaptionFormat'];
49 10
	}
50
51
	/**
52
	 * @since 1.0
53
	 *
54
	 * @param  integer $captionFormat
55
	 */
56
	public function setCaptionFormat( $captionFormat ) {
57
		$this->captionFormat = $captionFormat;
58
	}
59
60
	/**
61
	 * @see StringValue::parseUserValue
62
	 */
63 9
	protected function parseUserValue( $value ) {
64
65 9
		if ( method_exists( $this, 'getCallable' ) ) {
66 9
			$citationReferencePositionJournal = $this->getCallable( 'sci.citationreferencepositionjournal' );
67 9
			$this->citationReferencePositionJournal = $citationReferencePositionJournal();
68
		} else {
69
			$this->citationReferencePositionJournal = $this->getExtraneousFunctionFor( '\SCI\CitationReferencePositionJournal' );
70
		}
71
72 9
		$value = trim( $value );
73
74 9
		if ( $value === '' ) {
75 1
			$this->addError( wfMessage( 'sci-datavalue-empty-reference' )->inContentLanguage()->text() );
0 ignored issues
show
Bug introduced by
The function wfMessage was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
			$this->addError( /** @scrutinizer ignore-call */ wfMessage( 'sci-datavalue-empty-reference' )->inContentLanguage()->text() );
Loading history...
76 1
			$this->m_dataitem = new DIBlob( 'ERROR' );
0 ignored issues
show
Bug Best Practice introduced by
The property m_dataitem does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77 1
			return;
78
		}
79
80 8
		if ( $this->m_contextPage === null ) {
81
			parent::parseUserValue( $value );
82
			return;
83
		}
84
85 8
		$this->reference = $value;
86
87 8
		if ( $this->m_caption && $this->captionFormat === SCI_CITEREF_NUM ) {
88 1
			$this->usesShortFormCaption = true;
89
		}
90
91 8
		if ( !$this->m_caption ) {
92 7
			$this->m_caption = $this->reference;
0 ignored issues
show
Bug Best Practice introduced by
The property m_caption does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
93
		}
94
95
		// This is where the magic happens, compute the position of
96
		// a reference relative to previous CiteRef annotations
97 8
		$this->citationReferencePositionJournal->addJournalEntryFor(
98 8
			$this->m_contextPage,
99 8
			$this->reference
100
		);
101
102 8
		parent::parseUserValue( $this->reference );
103 8
	}
104
105
	/**
106
	 * @see StringValue::parseUserValue
107
	 */
108 7
	public function getShortWikiText( $linked = null ) {
109
110 7
		if ( $this->citationReferencePositionJournal === null ) {
111
			if ( method_exists( $this, 'getCallable' ) ) {
112
				$citationReferencePositionJournal = $this->getCallable( 'sci.citationreferencepositionjournal' );
113
				$this->citationReferencePositionJournal = $citationReferencePositionJournal();
114
			} else {
115
				$this->citationReferencePositionJournal = $this->getExtraneousFunctionFor( '\SCI\CitationReferencePositionJournal' );
116
			}
117
		}
118
119
		// We want the last entry here to get the major/minor
120
		// number that was internally recorded
121 7
		$referencePosition = $this->citationReferencePositionJournal->findLastReferencePositionEntryFor(
122 7
			$this->m_contextPage,
123 7
			$this->reference
124
		);
125
126 7
		if ( $referencePosition === null || $this->m_caption === false ) {
127
			return '';
128
		}
129
130 7
		$referenceHash = md5( $this->reference );
131
132 7
		if ( $this->captionFormat === SCI_CITEREF_NUM ) {
133 4
			list( $major, $minor ) = explode( '-', $referencePosition );
134 4
			$caption = $major;
135 4
			$captionClass = 'number';
136
137
			// [[CiteRef::Foo, 1970|:50-52]] will add the caption to the outside
138 4
			$shortFormCaption = $this->usesShortFormCaption ? $this->m_caption : '';
139
		} else {
140 3
			$captionClass = 'key';
141 3
			$caption = $this->m_caption;
142 3
			$shortFormCaption = ''; // Never has a short form
143
		}
144
145 7
		if ( $shortFormCaption !== '' ) {
146 1
			$shortFormCaption = Html::rawElement(
147 1
				'span',
148 1
				[ 'class' => 'scite-citeref-shortcaption' ],
149 1
				$shortFormCaption
150
			);
151
		}
152
153
		// Build element with back and forth link anchor
154 7
		$html = Html::rawElement(
155 7
			'span',
156
			[
157 7
				'id'    => 'scite-ref-'. $referenceHash . '-' . $referencePosition,
158 7
				'class' => 'scite-citeref-' . $captionClass,
159 7
				'data-reference' => $this->reference
160
			],
161 7
			'[[' .'#scite-' . $referenceHash . '|' . $caption . ']]'
162 7
		) . $shortFormCaption;
163
164 7
		return $html;
165
	}
166
167
}
168