Item::set()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
1
<?php
2
3
namespace SRF\BibTex;
4
5
use SMWDataValue as DataValue;
6
7
/**
8
 * @see http://www.semantic-mediawiki.org/wiki/BibTex
9
 *
10
 * @license GNU GPL v2+
11
 * @since 3.1
12
 *
13
 * @author mwjames
14
 */
15
class Item {
16
17
	/**
18
	 * @see https://en.wikipedia.org/wiki/BibTeX
19
	 *
20
	 * @var string
21
	 */
22
	private $type = '';
23
24
	/**
25
	 * @var []
26
	 */
27
	protected $fields = [
28
		'address' => '',
29
		'annote' => '',
30
		'author' => [],
31
		'booktitle' => '',
32
		'chapter' => '',
33
		'crossref' => '',
34
		'doi' => '',
35
		'edition' => '',
36
		'editor' => [],
37
		'eprint' => '',
38
		'howpublished' => '',
39
		'institution' => '',
40
		'journal' => '',
41
		'key' => '',
42
		'month' => '',
43
		'note' => '',
44
		'number' => '',
45
		'organization' => '',
46
		'pages' => '',
47
		'publisher' => '',
48
		'school' => '',
49
		'series' => '',
50
		'title' => '',
51
		'url' => '',
52
		'volume' => '',
53
		'year' => ''
54
	];
55
56
	/**
57
	 * @var callable
58
	 */
59
	private $formatterCallback;
60
61
	/**
62
	 * @since 3.1
63
	 */
64 7
	public function __construct() {
65 7
		$this->type = 'Book';
66 7
	}
67
68
	/**
69
	 * @since 3.1
70
	 *
71
	 * @param callable $compoundLabelCallback
0 ignored issues
show
Bug introduced by mwjames
There is no parameter named $compoundLabelCallback. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
72
	 */
73 3
	public function setFormatterCallback( callable $formatterCallback ) {
74 3
		$this->formatterCallback = $formatterCallback;
75 3
	}
76
77
	/**
78
	 * @since 3.1
79
	 *
80
	 * @param $key
81
	 * @param string $text
82
	 *
83
	 * @return string
84
	 */
85 6
	public function replace( $key, $text ) {
86
87 6
		if ( $key === 'uri' ) {
88 6
			$text = str_replace(
89 6
				[ "Ä", "ä", "Ö", "ö", "Ü", "ü", "ß" ],
90 6
				[ 'Ae', 'ae', 'Oe', 'oe', 'Ue', 'ue', 'ss' ],
91
				$text
92
			);
93 6
			$text = preg_replace("/[^a-zA-Z0-9]+/", "", $text );
94
		}
95
96 6
		return $text;
97
	}
98
99
	/**
100
	 * @since 3.1
101
	 *
102
	 * @param $key
103
	 * @param mixed $value
104
	 */
105 5
	public function set( $key, $value ) {
106
107 5
		$key = strtolower( $key );
108
109 5
		if ( $key === 'type' ) {
110 1
			$this->type = ucfirst( $value );
111
		}
112
113 5
		if ( isset( $this->fields[$key] ) ) {
114 5
			$this->fields[$key] = $value;
115
		}
116 5
	}
117
118
	/**
119
	 * @since 3.1
120
	 *
121
	 * @return string
122
	 */
123 5
	public function text() {
124
125 5
		$formatterCallback = $this->formatterCallback;
126
127 5
		$text = '@' . $this->type . '{' . $this->buildURI() . ",\r\n";
128
129 5
		foreach ( $this->fields as $key => $value ) {
130
131 5
			if ( ( $key === 'author' || $key === 'editor' ) && is_array( $value ) ) {
132 5
				if ( is_callable( $formatterCallback ) ) {
133 3
					$value = $formatterCallback( $key, $value );
134
				} else {
135 2
					$value = implode( ', ', $value );
136
				}
137
			}
138
139 5
			if ( $value === '' ) {
140 5
				continue;
141
			}
142
143 5
			$text .= '  ' . $key . ' = "' . $value . '", ' . "\r\n";
144
		}
145
146 5
		$text .= "}";
147
148 5
		return $text;
149
	}
150
151
	/**
152
	 * Consist of `author last name` + `year` + `first word of title`
153
	 *
154
	 * @return string
155
	 */
156 5
	protected function buildURI() {
157
158 5
		$uri = '';
159
160 5
		if ( isset( $this->fields['author'] ) ) {
161 5
			foreach ( $this->fields['author'] as $key => $author ) {
162 3
				$elements = explode( ' ', $author );
163 3
				$uri .= array_pop( $elements );
164 3
				break;
165
			}
166
		}
167
168 5
		if ( isset( $this->fields['year'] ) ) {
169 5
			$uri .= $this->fields['year'];
170
		}
171
172 5
		if ( isset( $this->fields['title'] ) ) {
173 5
			foreach ( explode( ' ', $this->fields['title'] ) as $titleWord ) {
174 5
				$charsTitleWord = preg_split( '//', $titleWord, -1, PREG_SPLIT_NO_EMPTY );
175
176 5
				if ( !empty( $charsTitleWord ) ) {
177 3
					$uri .= $charsTitleWord[0];
178
				}
179
			}
180
		}
181
182 5
		return strtolower( $this->replace( 'uri', $uri ) );
183
	}
184
185
}
186