ReferenceListParserFunction::doProcess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 27
ccs 12
cts 12
cp 1
rs 9.9332
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SCI;
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
use SMW\DIProperty;
0 ignored issues
show
Bug introduced by
The type SMW\DIProperty 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 SMW\ParserData;
0 ignored issues
show
Bug introduced by
The type SMW\ParserData 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 SMW\DataValueFactory;
0 ignored issues
show
Bug introduced by
The type SMW\DataValueFactory 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
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...
10
11
/**
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class ReferenceListParserFunction {
18
19
	/**
20
	 * @var ParserData
21
	 */
22
	private $parserData;
23
24
	/**
25
	 * @since 1.0
26
	 *
27
	 * @param ParserData $parserData
28
	 */
29 10
	public function __construct( ParserData $parserData ) {
30 10
		$this->parserData = $parserData;
31 10
		$this->dataValueFactory = DataValueFactory::getInstance();
0 ignored issues
show
Bug Best Practice introduced by
The property dataValueFactory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32 10
	}
33
34
	/**
35
	 * {{#referencelist:
36
	 * |columns=2
37
	 * |listtype="ol"
38
	 * |browselinks=true
39
	 * }}
40
	 *
41
	 * @since 1.0
42
	 *
43
	 * @param ParserParameterProcessor $parserParameterProcessor
44
	 */
45 9
	public function doProcess( ParserParameterProcessor $parserParameterProcessor ) {
46
47 9
		list( $header, $headerElement, $attributes ) = $this->getElementsForHtml(
48 9
			$parserParameterProcessor->toArray()
49
		);
50
51
		// Only the Parser can add a section/toc entry therefore the default reference
52
		// list is "too" late for the parser to process/add a toc section therefore only
53
		// the #referencelist can create a placeholder so that by the time the reference
54
		// list is generated the header is recognized.
55
56
		// The parser will set the headerTocId and is later fetched by the
57
		// CachedReferenceListOutputRenderer when replacing the placeholder. This
58
		// also takes care of any encoded title with non-Latin characters
59 9
		$header = Html::element(
60 9
			$headerElement,
61 9
			[],
62 9
			$header
63
		);
64
65 9
		$html = Html::rawElement(
66 9
			'div',
67 9
			$attributes,
68 9
			$header
69
		);
70
71 9
		return $html . "<!-- end marker -->\n";
72
	}
73
74 9
	private function getElementsForHtml( $parameters ) {
75
76 9
		$header = wfMessage( 'sci-referencelist-header' )->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

76
		$header = /** @scrutinizer ignore-call */ wfMessage( 'sci-referencelist-header' )->text();
Loading history...
77
78
		// The span placeholder will hide the header from the TOC by default
79 9
		$headerElement = 'span';
80
81
		$attributes = [
82 9
			'id' => 'scite-custom-referencelist'
83
		];
84
85 9
		foreach ( $parameters as $key => $values ) {
86
87 7
			if ( $key === 'references' ) {
88 4
				$attributes['data-references'] = $this->doProcessReferenceValues( $values );
89 4
				continue;
90
			}
91
92 5
			foreach ( $values as $value ) {
93
94 5
				if ( $key === 'header' ) {
95 3
					$header = $value;
96
				}
97
98 5
				if ( $key === 'toc' && filter_var( $value, FILTER_VALIDATE_BOOLEAN ) ) {
99 1
					$headerElement = 'h2';
100
				}
101
102 5
				$attributes['data-'. $key] = $value;
103
			}
104
		}
105
106 9
		return [ $header, $headerElement, $attributes ];
107
	}
108
109 4
	private function doProcessReferenceValues( array $values ) {
110
111 4
		$subject = $this->parserData->getSubject();
112
113 4
		foreach ( $values as $value ) {
114 4
			$dataValue = $this->dataValueFactory->newPropertyValue(
115 4
					new DIProperty( PropertyRegistry::SCI_CITE_REFERENCE ),
116 4
					trim( $value ),
117 4
					false,
118 4
					$subject
119
				);
120
121 4
			$this->parserData->addDataValue( $dataValue );
122
		}
123
124 4
		if ( !$this->parserData->getSemanticData()->isEmpty() ) {
125 4
			$this->parserData->getSubject()->setContextReference( 'referencelistp:' . uniqid() );
126 4
			$this->parserData->pushSemanticDataToParserOutput();
127
		}
128
129 4
		return json_encode( $values );
130
	}
131
132
}
133