Completed
Push — master ( 8cad24...404a00 )
by mw
07:20
created

CitationReferenceValue   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 82.19%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 152
ccs 60
cts 73
cp 0.8219
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setCaptionFormat() 0 3 1
B parseUserValue() 0 41 7
B getShortWikiText() 0 58 8
1
<?php
2
3
namespace SCI\DataValues;
4
5
use SCI\CitationReferencePositionJournal;
6
use SMWStringValue as StringValue;
7
use SMWDIBlob as DIBlob;
8
use Html;
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 = '' ) {
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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 9
		} 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() );
76 1
			$this->m_dataitem = new DIBlob( 'ERROR' );
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 1
		}
90
91 8
		if ( !$this->m_caption ) {
92 7
			$this->m_caption = $this->reference;
93 7
		}
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 8
		);
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 7
		);
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 );
0 ignored issues
show
Unused Code introduced by
The assignment to $minor is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
134 4
			$caption = $major;
135 4
			$captionClass = 'number';
136
137
			// [[CiteRef::Foo, 1970|:50-52]] will add the caption to the outside
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% 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...
138 4
			$shortFormCaption = $this->usesShortFormCaption ? $this->m_caption : '';
139 4
		} 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
				$shortFormCaption
150 1
			);
151 1
		}
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 7
			],
161 7
			'[[' .'#scite-' . $referenceHash . '|' . $caption . ']]'
162 7
		) . $shortFormCaption;
163
164 7
		return $html;
165
	}
166
167
}
168