BibtexProcessor::doPreprocess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SCI\Bibtex;
4
5
use SMW\ParserParameterProcessor;
0 ignored issues
show
Bug introduced by
The type SMW\ParserParameterProcessor 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...
6
7
/**
8
 * @license GNU GPL v2+
9
 * @since 1.0
10
 */
11
class BibtexProcessor {
12
13
	/**
14
	 * @var BibtexParser
15
	 */
16
	private $bibtexParser;
17
18
	/**
19
	 * @var BibtexAuthorListParser
20
	 */
21
	private $bibtexAuthorListParser;
22
23
	/**
24
	 * @since 1.0
25
	 *
26
	 * @param BibtexParser $bibtexParser
27
	 * @param BibtexAuthorListParser $bibtexAuthorListParser
28
	 */
29 19
	public function __construct( BibtexParser $bibtexParser, BibtexAuthorListParser $bibtexAuthorListParser ) {
30 19
		$this->bibtexParser = $bibtexParser;
31 19
		$this->bibtexAuthorListParser = $bibtexAuthorListParser;
32 19
	}
33
34
	/**
35
	 * @since 1.0
36
	 *
37
	 * @param ParserParameterProcessor $parserParameterProcessor
38
	 */
39 5
	public function doProcess( ParserParameterProcessor $parserParameterProcessor ) {
40
41 5
		$bibtex = $this->doPreprocess(
42 5
			$parserParameterProcessor->getParameterValuesFor( 'bibtex' )
43
		);
44
45 5
		$parameters = $this->bibtexParser->parse( $bibtex );
46
47 5
		foreach ( $parameters as $key => $value ) {
48
49
			// The explicit parameters precedes the one found in bibtex
50 5
			if ( $key === 'reference' && ( $parserParameterProcessor->hasParameter( 'reference' ) || $parserParameterProcessor->getFirstParameter() !== '' ) ) {
51 2
				continue;
52
			}
53
54 4
			if ( $key === 'type' && $parserParameterProcessor->hasParameter( 'type' ) ) {
55 2
				continue;
56
			}
57
58 3
			if ( $key === 'author' ) {
59
60 2
				$parserParameterProcessor->setParameter(
61 2
					$key,
62 2
					$this->bibtexAuthorListParser->parse( $value )
63
				);
64
65
				// Add the original value for easier post-processing in a template
66
				// as hidden parameter
67 2
				$key = 'bibtex-author';
68
			}
69
70 3
			$parserParameterProcessor->addParameter(
71 3
				$key,
72 3
				$value
73
			);
74
		}
75 5
	}
76
77 5
	private function doPreprocess( array $bibtex ) {
78
79 5
		$bibtex = end( $bibtex );
80
81
		// Avoid things like {{Stable theories}}" which are not supported in MW
82
		// since the parser replaces it with [[:Template:Stable theories]]
83 5
		$this->replace( '{{', "{", $bibtex );
84 5
		$this->replace( '}}', "}", $bibtex );
85
86 5
		$this->replace( '{\textquotesingle}', "'", $bibtex );
87 5
		$this->replace( '$\upgamma$', "γ", $bibtex );
88
89 5
		return $bibtex;
90
	}
91
92 5
	private function replace( $search, $with, &$on ) {
93 5
		$on = strpos( $on, $search ) !== false ? str_replace( $search, $with, $on ) : $on;
94 5
	}
95
96
}
97