Test Failed
Push — master ( 9d4e41...1cd52c )
by Sebastian
03:24
created

CiteProc::render()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 15
nc 7
nop 2
1
<?php
2
/**
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc;
11
use Seboettg\CiteProc\Data\DataList;
12
use Seboettg\CiteProc\Exception\CiteProcException;
13
use Seboettg\CiteProc\Style\Bibliography;
14
use Seboettg\CiteProc\Style\Citation;
15
use Seboettg\CiteProc\Style\Macro;
16
use Seboettg\CiteProc\Style\Root;
17
18
19
/**
20
 * Class CiteProc
21
 * @package Seboettg\CiteProc
22
 *
23
 * @author Sebastian Böttger <[email protected]>
24
 */
25
class CiteProc
26
{
27
28
    /**
29
     * @var Context
30
     */
31
    private static $context;
32
33
34
    /**
35
     * @return Context
36
     */
37
    public static function getContext()
38
    {
39
        return self::$context;
40
    }
41
42
    /**
43
     * @param Context $context
44
     */
45
    public static function setContext($context)
46
    {
47
        self::$context = $context;
48
    }
49
50
    /**
51
     * @param $styleName
52
     * @deprecated
53
     */
54
    public static function loadStyleSheet($styleName)
55
    {
56
        return StyleSheet::loadStyleSheet($styleName);
57
    }
58
59
60
    /**
61
     * @var string
62
     */
63
    private $styleSheet;
64
65
    /**
66
     * @var \SimpleXMLElement
67
     */
68
    private $styleSheetXml;
69
70
    /**
71
     * CiteProc constructor.
72
     * @param string $styleSheet xml formatted csl stylesheet
73
     */
74
    public function __construct($styleSheet, $lang = "en-US")
75
    {
76
        $this->styleSheet = $styleSheet;
77
        self::$context = new Context($this);
78
        self::$context->setLocale(new Locale\Locale($lang)); //init locale
79
        $this->styleSheetXml = new \SimpleXMLElement($this->styleSheet);
80
        $this->parse($this->styleSheetXml);
81
    }
82
83
    /**
84
     * @param \SimpleXMLElement $style
85
     */
86
    private function parse(\SimpleXMLElement $style)
87
    {
88
        $root = new Root();
89
        $root->initInheritableNameAttributes($style);
90
        self::$context->setRoot($root);
91
92
        /** @var \SimpleXMLElement $node */
93
        foreach ($style as $node) {
94
            $name = $node->getName();
95
            switch ($name) {
96
                case 'info':
97
                    break;
98
                case 'locale':
99
                    self::$context->getLocale()->addXml($node);
100
                    break;
101
                case 'macro':
102
                    $macro = new Macro($node);
103
                    self::$context->addMacro($macro->getName(), $macro);
104
                    break;
105
                case 'bibliography':
106
                    $bibliography = new Bibliography($node);
107
                    self::$context->setBibliography($bibliography);
108
                    break;
109
                case 'citation':
110
                    $citation = new Citation($node);
111
                    self::$context->setCitation($citation);
112
                    break;
113
            }
114
        }
115
    }
116
117
    /**
118
     * @param array|DataList $data
119
     * @return string
120
     */
121
    protected function bibliography($data)
122
    {
123
124
        return self::$context->getBibliography()->render($data);
0 ignored issues
show
Documentation introduced by
$data is of type array|object<Seboettg\CiteProc\Data\DataList>, but the function expects a object<stdClass>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
125
    }
126
127
    /**
128
     * @param array|DataList $data
129
     * @return string
130
     */
131
    protected function citation($data)
132
    {
133
        return self::$context->getCitation()->render($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 131 can also be of type object<Seboettg\CiteProc\Data\DataList>; however, Seboettg\CiteProc\Style\Citation::render() does only seem to accept array|object<Seboettg\CiteProc\Style\DataList>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
134
    }
135
136
    /**
137
     * @param array|DataList $data
138
     * @param string $mode (citation|bibliography)
139
     * @return string
140
     * @throws CiteProcException
141
     */
142
    public function render($data, $mode = "bibliography") {
143
144
        if (is_array($data)) {
145
            $data = new DataList($data);
146
        } else if (!($data instanceof DataList)) {
147
            throw new CiteProcException('No valid format for variable data. Either DataList or array expected');
148
        }
149
150
        // set CitationItems to Context
151
        self::getContext()->setCitationItems($data);
152
153
        switch ($mode) {
154
            case 'bibliography':
155
                self::$context->setMode($mode);
156
                return $this->bibliography($data);
157
            case 'citation':
158
                self::$context->setMode($mode);
159
                return $this->citation($data);
160
            default:
161
                throw new \InvalidArgumentException("\"$mode\" is not a valid mode.");
162
        }
163
    }
164
}