Completed
Push — master ( 3eefa2...fea31f )
by Sebastian
03:06
created

CiteProc   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 9
dl 0
loc 156
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getContext() 0 4 1
A setContext() 0 4 1
A loadStyleSheet() 0 4 1
A __construct() 0 5 1
C parse() 0 30 7
A bibliography() 0 5 1
A citation() 0 4 1
B render() 0 33 6
A init() 0 7 1
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
        $this->lang = $lang;
0 ignored issues
show
Bug introduced by
The property lang does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
78
    }
79
80
    /**
81
     * @param \SimpleXMLElement $style
82
     */
83
    private function parse(\SimpleXMLElement $style)
84
    {
85
        $root = new Root();
86
        $root->initInheritableNameAttributes($style);
87
        self::$context->setRoot($root);
88
89
        /** @var \SimpleXMLElement $node */
90
        foreach ($style as $node) {
91
            $name = $node->getName();
92
            switch ($name) {
93
                case 'info':
94
                    break;
95
                case 'locale':
96
                    self::$context->getLocale()->addXml($node);
97
                    break;
98
                case 'macro':
99
                    $macro = new Macro($node, $root);
100
                    self::$context->addMacro($macro->getName(), $macro);
101
                    break;
102
                case 'bibliography':
103
                    $bibliography = new Bibliography($node, $root);
104
                    self::$context->setBibliography($bibliography);
105
                    break;
106
                case 'citation':
107
                    $citation = new Citation($node, $root);
108
                    self::$context->setCitation($citation);
109
                    break;
110
            }
111
        }
112
    }
113
114
    /**
115
     * @param array|DataList $data
116
     * @return string
117
     */
118
    protected function bibliography($data)
119
    {
120
121
        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...
122
    }
123
124
    /**
125
     * @param array|DataList $data
126
     * @return string
127
     */
128
    protected function citation($data)
129
    {
130
        return self::$context->getCitation()->render($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 128 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...
131
    }
132
133
    /**
134
     * @param array|DataList $data
135
     * @param string $mode (citation|bibliography)
136
     * @return string
137
     * @throws CiteProcException
138
     */
139
    public function render($data, $mode = "bibliography")
140
    {
141
142
        if (!in_array($mode, ['citation', 'bibliography'])) {
143
            throw new \InvalidArgumentException("\"$mode\" is not a valid mode.");
144
        }
145
146
        $this->init(); //initialize
147
148
        $res = "";
149
150
        if (is_array($data)) {
151
            $data = new DataList($data);
152
        } else if (!($data instanceof DataList)) {
153
            throw new CiteProcException('No valid format for variable data. Either DataList or array expected');
154
        }
155
156
        // set CitationItems to Context
157
        self::getContext()->setCitationItems($data);
158
159
        switch ($mode) {
160
            case 'bibliography':
161
                self::$context->setMode($mode);
162
                $res = $this->bibliography($data);
163
                break;
164
            case 'citation':
165
                self::$context->setMode($mode);
166
                $res = $this->citation($data);
167
        }
168
        self::setContext(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Seboettg\CiteProc\Context>.

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...
169
170
        return $res;
171
    }
172
173
    private function init()
174
    {
175
        self::$context = new Context($this);
176
        self::$context->setLocale(new Locale\Locale($this->lang)); //init locale
177
        $this->styleSheetXml = new \SimpleXMLElement($this->styleSheet);
178
        $this->parse($this->styleSheetXml);
179
    }
180
}