Passed
Push — new-api ( 220eb6...4bfe18 )
by Sebastian
06:58
created

Context   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 432
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
eloc 89
dl 0
loc 432
ccs 120
cts 136
cp 0.8824
rs 8.4
c 0
b 0
f 0
wmc 50

48 Methods

Rating   Name   Duplication   Size   Complexity  
A setRoot() 0 3 1
A setGlobalOptions() 0 3 1
A getMacros() 0 3 1
A setMode() 0 4 1
A setMarkupExtension() 0 3 1
A getNameOptions() 0 3 1
A getCitation() 0 3 1
A setLocale() 0 3 1
A getMode() 0 3 1
A setInfo() 0 3 1
A getCitedItems() 0 3 1
A getCssStyle() 0 3 1
A hasCitationItems() 0 3 1
A getBibliography() 0 3 1
A addObserver() 0 3 1
A getGlobalOptions() 0 3 1
A setSorting() 0 3 1
A setCitation() 0 3 1
A getSorting() 0 3 1
A getRenderingState() 0 3 1
A setCitedItems() 0 4 1
A isModeCitation() 0 3 1
A isModeBibliography() 0 3 1
A addMacro() 0 3 1
A setNameOptions() 0 3 1
A getMarkupExtension() 0 3 1
A getRoot() 0 3 1
A getMacro() 0 3 1
A setBibliography() 0 3 1
A setCssStyle() 0 3 1
A setCitationSpecificOptions() 0 3 1
A getCitationItems() 0 3 1
A setCitationsAsArray() 0 3 1
A setBibliographySpecificOptions() 0 3 1
A __construct() 0 15 2
A getResults() 0 3 1
A getCitationData() 0 3 1
A getInfo() 0 3 1
A getCitationSpecificOptions() 0 3 1
A setCitationData() 0 4 1
A getLocale() 0 3 1
A getBibliographySpecificOptions() 0 3 1
A getCitationItemById() 0 5 1
A appendCitedItem() 0 4 1
A isCitationsAsArray() 0 3 1
A notifyObservers() 0 5 2
A setRenderingState() 0 4 1
A setCitationItems() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Context often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Context, and based on these observations, apply Extract Interface, too.

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
12
use Seboettg\CiteProc\Config\RenderingMode;
13
use Seboettg\CiteProc\Config\RenderingState;
14
use Seboettg\CiteProc\Data\DataList;
15
use Seboettg\CiteProc\Locale\Locale;
16
use Seboettg\CiteProc\Rendering\Observer\CitationDataChangedEvent;
17
use Seboettg\CiteProc\Rendering\Observer\CitationItemsChangedEvent;
18
use Seboettg\CiteProc\Rendering\Observer\CitedItemsChanged;
19
use Seboettg\CiteProc\Rendering\Observer\ModeChangedEvent;
20
use Seboettg\CiteProc\Rendering\Observer\RenderingEvent;
21
use Seboettg\CiteProc\Rendering\Observer\RenderingObservable;
22
use Seboettg\CiteProc\Rendering\Observer\RenderingObserver;
23
use Seboettg\CiteProc\Rendering\Observer\StateChangedEvent;
24
use Seboettg\CiteProc\Root\Info;
25
use Seboettg\CiteProc\Style\Bibliography;
26
use Seboettg\CiteProc\Style\Citation;
27
use Seboettg\CiteProc\Style\Macro;
28
use Seboettg\CiteProc\Style\Options\BibliographyOptions;
29
use Seboettg\CiteProc\Style\Options\CitationOptions;
30
use Seboettg\CiteProc\Style\Options\GlobalOptions;
31
use Seboettg\CiteProc\Style\Options\NameOptions;
32
use Seboettg\CiteProc\Style\Sort\Sort;
33
use Seboettg\CiteProc\Root\Root;
34
use Seboettg\CiteProc\Styles\Css\CssStyle;
35
use Seboettg\Collection\ArrayList;
36
use Seboettg\Collection\ArrayList\ArrayListInterface;
37
38
/**
39
 * Class Context
40
 * @package Seboettg\CiteProc
41
 *
42
 * @author Sebastian Böttger <[email protected]>
43
 */
44
class Context implements RenderingObservable
45
{
46
    /** @var ArrayListInterface */
47
    private $macros;
48
49
    /** @var Locale */
50
    private $locale;
51
52
    /** @var Bibliography */
53
    private $bibliography;
54
55
    /** @var Citation */
56
    private $citation;
57
58
    /** @var Sort */
59
    private $sorting;
60
61
    /** @var RenderingMode */
62
    private $mode;
63
64
    /** @var DataList */
65
    private $citationData;
66
67
    /** @var ArrayListInterface */
68
    private $citationItems;
69
70
    /** @var ArrayListInterface */
71
    private $results;
72
73
    /** @var Root */
74
    private $root;
75
76
    /** @var GlobalOptions */
77
    private $globalOptions;
78
79
    /** @var BibliographyOptions */
80
    private $bibliographySpecificOptions;
81
82
    /** @var CitationOptions */
83
    private $citationSpecificOptions;
84
85
    /** @var RenderingState */
86
    private $renderingState;
87
88
    /** @var CssStyle */
89
    private $cssStyle;
90
91
    /** @var Info */
92
    private $info;
93
94
    /** @var array */
95
    protected $markupExtension = [];
96
97
    /** @var bool */
98
    private $citationsAsArray = false;
99
100
    /** @var ArrayListInterface */
101
    private $citedItems;
102
103
    /** @var ArrayListInterface */
104
    private $observers;
105
106
    /** @var NameOptions */
107
    private $nameOptions;
108
109 182
    public function __construct($locale = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$locale" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$locale"; expected 0 but found 1
Loading history...
110
    {
111 182
        if (!empty($locale)) {
112
            $this->locale = $locale;
113
        }
114
115 182
        $this->macros = new ArrayList();
116 182
        $this->citationData = new DataList();
117 182
        $this->results = new ArrayList();
118 182
        $this->renderingState = RenderingState::RENDERING();
119 182
        $this->mode = RenderingMode::BIBLIOGRAPHY();
120 182
        $this->citedItems = new ArrayList();
121 182
        $this->citationItems = new ArrayList();
122 182
        $this->observers = new ArrayList();
123 182
        $this->nameOptions = new NameOptions();
124 182
    }
125
126 120
    public function addObserver(RenderingObserver $observer): void
127
    {
128 120
        $this->observers->append($observer);
129 120
    }
130
131 172
    private function notifyObservers(RenderingEvent $event)
132
    {
133
        /** @var RenderingObserver $observer */
134 172
        foreach ($this->observers as $observer) {
135 112
            $observer->notify($event);
136
        }
137 172
    }
138
139
    public function getNameOptions(): NameOptions
140
    {
141
        return $this->nameOptions;
142
    }
143
144
    public function setNameOptions(NameOptions $nameOptions): void
145
    {
146
        $this->nameOptions = $nameOptions;
147
    }
148
149 70
    public function addMacro($key, $macro)
150
    {
151 70
        $this->macros->add($key, $macro);
152 70
    }
153
154
    /**
155
     * @param $key
156
     * @return Macro
157
     */
158 14
    public function getMacro($key)
159
    {
160 14
        return $this->macros->get($key);
161
    }
162
163
    /**
164
     * @param Locale $locale
165
     */
166 182
    public function setLocale(Locale $locale)
167
    {
168 182
        $this->locale = $locale;
169 182
    }
170
171
    /**
172
     * @return Locale
173
     */
174 182
    public function getLocale()
175
    {
176 182
        return $this->locale;
177
    }
178
179
    /**
180
     * @return Bibliography
181
     */
182 125
    public function getBibliography()
183
    {
184 125
        return $this->bibliography;
185
    }
186
187
    /**
188
     * @param Bibliography $bibliography
189
     */
190 86
    public function setBibliography(Bibliography $bibliography)
191
    {
192 86
        $this->bibliography = $bibliography;
193 86
    }
194
195
    /**
196
     * @return Citation
197
     */
198 104
    public function getCitation()
199
    {
200 104
        return $this->citation;
201
    }
202
203
    /**
204
     * @param Citation $citation
205
     */
206 159
    public function setCitation(Citation $citation)
207
    {
208 159
        $this->citation = $citation;
209 159
    }
210
211
    /**
212
     * @param $citationsAsArray
213
     */
214 172
    public function setCitationsAsArray($citationsAsArray = true)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$citationsAsArray" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$citationsAsArray"; expected 0 but found 1
Loading history...
215
    {
216 172
        $this->citationsAsArray = $citationsAsArray;
217 172
    }
218
219 1
    public function isCitationsAsArray()
220
    {
221 1
        return $this->citationsAsArray;
222
    }
223
224 60
    public function setSorting($sorting)
225
    {
226 60
        $this->sorting = $sorting;
227 60
    }
228
229 168
    public function getSorting()
230
    {
231 168
        return $this->sorting;
232
    }
233
234
    /**
235
     * return the render mode (citation|bibliography)
236
     * @return string
237
     */
238 176
    public function getMode()
239
    {
240 176
        return $this->mode;
241
    }
242
243
    /**
244
     * @param RenderingMode $mode
245
     */
246 172
    public function setMode(RenderingMode $mode)
247
    {
248 172
        $this->mode = $mode;
249 172
        $this->notifyObservers(new ModeChangedEvent($mode));
250 172
    }
251
252
    /**
253
     * returns true if the render mode is set to citation
254
     * @return bool
255
     */
256 132
    public function isModeCitation(): bool
257
    {
258 132
        return $this->mode->equals(RenderingMode::CITATION());
259
    }
260
261
    /**
262
     * returns true if the render mode is set to bibliography
263
     * @return bool
264
     */
265 168
    public function isModeBibliography(): bool
266
    {
267 168
        return $this->mode->equals(RenderingMode::BIBLIOGRAPHY());
268
    }
269
270
    /**
271
     * @return DataList
272
     */
273 104
    public function getCitationData(): DataList
274
    {
275 104
        return $this->citationData;
276
    }
277
278
    /**
279
     * @param ArrayListInterface|DataList $citationData
280
     */
281 80
    public function setCitationData($citationData)
282
    {
283 80
        $this->citationData = $citationData;
284 80
        $this->notifyObservers(new CitationDataChangedEvent($citationData));
285 80
    }
286
287
    /**
288
     * @return ArrayListInterface
289
     */
290
    public function getCitationItems(): ?ArrayListInterface
291
    {
292
        return $this->citationItems;
293
    }
294
295
    /**
296
     * @param ArrayListInterface $citationItems
297
     */
298 104
    public function setCitationItems(ArrayListInterface $citationItems): void
299
    {
300 104
        $this->citationItems = $citationItems;
301 104
        $this->notifyObservers(new CitationItemsChangedEvent($citationItems));
302 104
    }
303
304
    public function hasCitationItems()
305
    {
306
        return ($this->citationData->count() > 0);
307
    }
308
309
    /**
310
     * @return ArrayList
311
     */
312 120
    public function getMacros()
313
    {
314 120
        return $this->macros;
315
    }
316
317
    /**
318
     * @return ArrayListInterface
319
     */
320 168
    public function getResults(): ArrayListInterface
321
    {
322 168
        return $this->results;
323
    }
324
325
    /**
326
     * @return Root
327
     */
328 172
    public function getRoot()
329
    {
330 172
        return $this->root;
331
    }
332
333
    /**
334
     * @param Root $root
335
     */
336 172
    public function setRoot(Root $root)
337
    {
338 172
        $this->root = $root;
339 172
    }
340
341
    /**
342
     * @return GlobalOptions
343
     */
344 158
    public function getGlobalOptions(): ?GlobalOptions
345
    {
346 158
        return $this->globalOptions;
347
    }
348
349
    /**
350
     * @param GlobalOptions $globalOptions
351
     */
352 172
    public function setGlobalOptions(GlobalOptions $globalOptions): void
353
    {
354 172
        $this->globalOptions = $globalOptions;
355 172
    }
356
357
    /**
358
     * @return RenderingState
359
     */
360 110
    public function getRenderingState()
361
    {
362 110
        return $this->renderingState;
363
    }
364
365
    /**
366
     * @param RenderingState|string $renderingState
367
     */
368 63
    public function setRenderingState(RenderingState $renderingState)
369
    {
370 63
        $this->renderingState = $renderingState;
371 63
        $this->notifyObservers(new StateChangedEvent($renderingState));
372 63
    }
373
374
    /**
375
     * @return BibliographyOptions
376
     */
377 171
    public function getBibliographySpecificOptions(): ?BibliographyOptions
378
    {
379 171
        return $this->bibliographySpecificOptions;
380
    }
381
382
    /**
383
     * @param BibliographyOptions $bibliographySpecificOptions
384
     */
385 86
    public function setBibliographySpecificOptions(BibliographyOptions $bibliographySpecificOptions)
386
    {
387 86
        $this->bibliographySpecificOptions = $bibliographySpecificOptions;
388 86
    }
389
390
    /**
391
     * @return CitationOptions
392
     */
393
    public function getCitationSpecificOptions(): ?CitationOptions
394
    {
395
        return $this->citationSpecificOptions;
396
    }
397
398
    /**
399
     * @param CitationOptions $citationSpecificOptions
400
     */
401 159
    public function setCitationSpecificOptions(CitationOptions $citationSpecificOptions)
402
    {
403 159
        $this->citationSpecificOptions = $citationSpecificOptions;
404 159
    }
405
406
    /**
407
     * @param CssStyle $cssStyle
408
     */
409 3
    public function setCssStyle(CssStyle $cssStyle)
410
    {
411 3
        $this->cssStyle = $cssStyle;
412 3
    }
413
414
    /**
415
     * @return CssStyle
416
     */
417 3
    public function getCssStyle()
418
    {
419 3
        return $this->cssStyle;
420
    }
421
422 162
    public function setInfo(Info $info)
423
    {
424 162
        $this->info = $info;
425 162
    }
426
427 1
    public function getInfo()
428
    {
429 1
        return $this->info;
430
    }
431
432
    /**
433
     * @return array
434
     */
435 172
    public function getMarkupExtension()
436
    {
437 172
        return $this->markupExtension;
438
    }
439
440
    /**
441
     * @param callable[] $markupExtension
442
     */
443 172
    public function setMarkupExtension(array $markupExtension)
444
    {
445 172
        $this->markupExtension = $markupExtension;
446 172
    }
447
448 11
    public function getCitationItemById($id)
449
    {
450
        return $this->citationItems->filter(function ($item) use ($id) {
451 6
            return $item->id === $id;
452 11
        })->current();
453
    }
454
455
    /**
456
     * @return ArrayListInterface
457
     */
458 1
    public function getCitedItems(): ArrayListInterface
459
    {
460 1
        return $this->citedItems;
461
    }
462
463
    /**
464
     * @param ArrayListInterface $citedItems
465
     */
466
    public function setCitedItems(ArrayListInterface $citedItems): void
467
    {
468
        $this->citedItems = $citedItems;
469
        $this->notifyObservers(new CitedItemsChanged($citedItems));
470
    }
471
472 104
    public function appendCitedItem($citedItem)
473
    {
474 104
        $this->citedItems->append($citedItem);
475 104
        $this->notifyObservers(new CitedItemsChanged($this->citedItems));
476 104
    }
477
}
478