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

initInheritableNameAttributes()   D

Complexity

Conditions 43
Paths 176

Size

Total Lines 110
Code Lines 88

Duplication

Lines 30
Ratio 27.27 %

Importance

Changes 0
Metric Value
dl 30
loc 110
rs 4
c 0
b 0
f 0
cc 43
eloc 88
nc 176
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2017 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Style;
11
12
use Seboettg\CiteProc\CiteProc;
13
use Seboettg\CiteProc\Rendering\Name\Name;
14
15
/**
16
 * Class InheritableNameAttributesTrait
17
 *
18
 * Attributes for the cs:names and cs:name elements may also be set on cs:style, cs:citation and cs:bibliography. This
19
 * eliminates the need to repeat the same attributes and attribute values for every occurrence of the cs:names and
20
 * cs:name elements.
21
 *
22
 * The available inheritable attributes for cs:name are and, delimiter-precedes-et-al, delimiter-precedes-last,
23
 * et-al-min, et-al-use-first, et-al-use-last, et-al-subsequent-min, et-al-subsequent-use-first, initialize,
24
 * initialize-with, name-as-sort-order and sort-separator. The attributes name-form and name-delimiter correspond to the
25
 * form and delimiter attributes on cs:name. Similarly, names-delimiter corresponds to the delimiter attribute on
26
 * cs:names.
27
 *
28
 *
29
 * @package Seboettg\CiteProc\Style
30
 * @author Sebastian Böttger <[email protected]>
31
 */
32
trait InheritableNameAttributesTrait
33
{
34
    static public $attributes = [
35
        'and',
36
        'delimiter-precedes-et-al',
37
        'delimiter-precedes-last',
38
        'et-al-min',
39
        'et-al-use-first',
40
        'et-al-use-last',
41
        'et-al-subsequent-min',
42
        'et-al-subsequent-use-first',
43
        'initialize',
44
        'initialize-with',
45
        'name-as-sort-order',
46
        'sort-separator'
47
    ];
48
49
    /**
50
     * @var bool
51
     */
52
    protected $attributesInitialized = false;
53
54
    /**
55
     * Specifies the delimiter between the second to last and last name of the names in a name variable. Allowed values
56
     * are “text” (selects the “and” term, e.g. “Doe, Johnson and Smith”) and “symbol” (selects the ampersand,
57
     * e.g. “Doe, Johnson & Smith”).
58
     *
59
     * @var string
60
     */
61
    private $and;
62
63
    /**
64
     * Determines when the name delimiter or a space is used between a truncated name list and the “et-al”
65
     * (or “and others”) term in case of et-al abbreviation. Allowed values:
66
     * - “contextual” - (default), name delimiter is only used for name lists truncated to two or more names
67
     *   - 1 name: “J. Doe et al.”
68
     *   - 2 names: “J. Doe, S. Smith, et al.”
69
     * - “after-inverted-name” - name delimiter is only used if the preceding name is inverted as a result of the
70
     *   - name-as-sort-order attribute. E.g. with name-as-sort-order set to “first”:
71
     *   - “Doe, J., et al.”
72
     *   - “Doe, J., S. Smith et al.”
73
     * - “always” - name delimiter is always used
74
     *   - 1 name: “J. Doe, et al.”
75
     *   - 2 names: “J. Doe, S. Smith, et al.”
76
     * - “never” - name delimiter is never used
77
     *   - 1 name: “J. Doe et al.”
78
     *   - 2 names: “J. Doe, S. Smith et al.”
79
     *
80
     * @var string
81
     */
82
    private $delimiterPrecedesEtAl;
83
84
    /**
85
     * Determines when the name delimiter is used to separate the second to last and the last name in name lists (if
86
     * and is not set, the name delimiter is always used, regardless of the value of delimiter-precedes-last). Allowed
87
     * values:
88
     *
89
     * - “contextual” - (default), name delimiter is only used for name lists with three or more names
90
     *   - 2 names: “J. Doe and T. Williams”
91
     *   - 3 names: “J. Doe, S. Smith, and T. Williams”
92
     * - “after-inverted-name” - name delimiter is only used if the preceding name is inverted as a result of the
93
     *   name-as-sort-order attribute. E.g. with name-as-sort-order set to “first”:
94
     *   - “Doe, J., and T. Williams”
95
     *   - “Doe, J., S. Smith and T. Williams”
96
     * - “always” - name delimiter is always used
97
     *   - 2 names: “J. Doe, and T. Williams”
98
     *   - 3 names: “J. Doe, S. Smith, and T. Williams”
99
     * - “never” - name delimiter is never used
100
     *   - 2 names: “J. Doe and T. Williams”
101
     *   - 3 names: “J. Doe, S. Smith and T. Williams”
102
     *
103
     * @var string
104
     */
105
    private $delimiterPrecedesLast;
106
107
    /**
108
     * Use of etAlMin (et-al-min attribute) and etAlUseFirst (et-al-use-first attribute) enables et-al abbreviation. If
109
     * the number of names in a name variable matches or exceeds the number set on etAlMin, the rendered name list is
110
     * truncated after reaching the number of names set on etAlUseFirst.
111
     *
112
     * @var int
113
     */
114
    private $etAlMin;
115
116
    /**
117
     * Use of etAlMin (et-al-min attribute) and etAlUseFirst (et-al-use-first attribute) enables et-al abbreviation. If
118
     * the number of names in a name variable matches or exceeds the number set on etAlMin, the rendered name list is
119
     * truncated after reaching the number of names set on etAlUseFirst.
120
     *
121
     * @var int
122
     */
123
    private $etAlUseFirst;
124
125
    /**
126
     * When set to “true” (the default is “false”), name lists truncated by et-al abbreviation are followed by the name
127
     * delimiter, the ellipsis character, and the last name of the original name list. This is only possible when the
128
     * original name list has at least two more names than the truncated name list (for this the value of
129
     * et-al-use-first/et-al-subsequent-min must be at least 2 less than the value of
130
     * et-al-min/et-al-subsequent-use-first).
131
     *
132
     * @var bool
133
     */
134
    private $etAlUseLast = false;
135
136
    /**
137
     * If used, the values of these attributes (et-al-subsequent-min and et-al-subsequent-use-first) replace those of
138
     * respectively et-al-min and et-al-use-first for subsequent cites (cites referencing earlier cited items).
139
     *
140
     * @var int
141
     */
142
    private $etAlSubsequentMin;
143
144
    /**
145
     * If used, the values of these attributes (et-al-subsequent-min and et-al-subsequent-use-first) replace those of
146
     * respectively et-al-min and et-al-use-first for subsequent cites (cites referencing earlier cited items).
147
     *
148
     * @var int
149
     */
150
    private $etAlSubsequentUseFirst;
151
152
    /**
153
     * When set to “false” (the default is “true”), given names are no longer initialized when “initialize-with” is set.
154
     * However, the value of “initialize-with” is still added after initials present in the full name (e.g. with
155
     * initialize set to “false”, and initialize-with set to ”.”, “James T Kirk” becomes “James T. Kirk”).
156
     *
157
     * @var bool
158
     */
159
    private $initialize = true;
160
161
    /**
162
     * When set, given names are converted to initials. The attribute value is added after each initial (”.” results
163
     * in “J.J. Doe”). For compound given names (e.g. “Jean-Luc”), hyphenation of the initials can be controlled with
164
     * the global initialize-with-hyphen option
165
     *
166
     * @var string
167
     */
168
    private $initializeWith = "";
169
170
    /**
171
     * Specifies that names should be displayed with the given name following the family name (e.g. “John Doe” becomes
172
     * “Doe, John”). The attribute has two possible values:
173
     *   - “first” - attribute only has an effect on the first name of each name variable
174
     *   - “all” - attribute has an effect on all names
175
     * Note that even when name-as-sort-order changes the name-part order, the display order is not necessarily the same
176
     * as the sorting order for names containing particles and suffixes (see Name-part order). Also, name-as-sort-order
177
     * only affects names written in the latin or Cyrillic alphabets. Names written in other alphabets (e.g. Asian
178
     * scripts) are always displayed with the family name preceding the given name.
179
     *
180
     * @var string
181
     */
182
    private $nameAsSortOrder = "";
183
184
    /**
185
     * Sets the delimiter for name-parts that have switched positions as a result of name-as-sort-order. The default
186
     * value is ”, ” (“Doe, John”). As is the case for name-as-sort-order, this attribute only affects names written in
187
     * the latin or Cyrillic alphabets.
188
     *
189
     * @var string
190
     */
191
    private $sortSeparator = ", ";
192
193
194
    public function initInheritableNameAttributes(\SimpleXMLElement $node)
195
    {
196
        $context = CiteProc::getContext();
197
        $parentStyleElement = null;
198
        if ($this instanceof  Name) {
199
            $parentStyleElement = ($context->isModeBibliography() ? $context->getBibliography() : $context->getCitation());
200
        } else if ($this instanceof StyleElement){
201
            $parentStyleElement = $context->getRoot();
202
        }
203
204
        foreach (self::$attributes as $nameAttribute) {
205
            $attribute = $node[$nameAttribute];
206
            switch ($nameAttribute) {
207
                case 'and':
208
                    $and = "";
209
                    if (!empty($attribute)) {
210
                        $and = (string)$attribute;
211
212
                    } else if (!empty($parentStyleElement)) { //inherit from parent style
213
                        $and = $parentStyleElement->getAnd();
214
                    }
215
                    if ("text" === $and) {
216
                        $this->and = CiteProc::getContext()->getLocale()->filter('terms', 'and')->single;
217
                    } elseif ('symbol' === $and) {
218
                        $this->and = '&#38;';
219
                    }
220
                    break;
221
                case 'delimiter-precedes-et-al':
222
                    if (!empty($attribute)) {
223
                        $this->delimiterPrecedesEtAl = (string)$attribute;
224
                    } else if (!empty($parentStyleElement)) { //inherit from parent style
225
                        $this->delimiterPrecedesEtAl = $parentStyleElement->getDelimiterPrecedesEtAl();
226
                    }
227
                    break;
228
                case 'delimiter-precedes-last':
229
                    if (!empty($attribute)) {
230
                        $this->delimiterPrecedesLast = (string)$attribute;
231
                    } else if (!empty($parentStyleElement)) { //inherit from parent style
232
                        $this->delimiterPrecedesLast = $parentStyleElement->getDelimiterPrecedesLast();
233
                    }
234
                    break;
235
                case 'et-al-min':
236 View Code Duplication
                    if (!empty($attribute)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
                        $this->etAlMin = intval((string) $attribute);
238
                    } else if (!empty($parentStyleElement)) {
239
                        $this->etAlMin = $parentStyleElement->getEtAlMin();
240
                    }
241
                    break;
242
                case 'et-al-use-first':
243 View Code Duplication
                    if (!empty($attribute)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
                        $this->etAlUseFirst = intval((string)$attribute);
245
                    } else if (!empty($parentStyleElement)) {
246
                        $this->etAlUseFirst = $parentStyleElement->getEtAlUseFirst();
247
                    }
248
                    break;
249
                case 'et-al-subsequent-min':
250 View Code Duplication
                    if (!empty($attribute)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
251
                        $this->etAlSubsequentMin = intval((string)$attribute);
252
                    } else if (!empty($parentStyleElement)) {
253
                        $this->etAlSubsequentMin = $parentStyleElement->getEtAlSubsequentMin();
254
                    }
255
                    break;
256
                case 'et-al-subsequent-use-first':
257 View Code Duplication
                    if (!empty($attribute)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
                        $this->etAlSubsequentUseFirst = intval((string) $attribute);
259
                    } else if (!empty($parentStyleElement)) {
260
                        $this->etAlSubsequentUseFirst = $parentStyleElement->getEtAlSubsequentUseFirst();
261
                    }
262
                    break;
263
                case 'et-al-use-last':
264 View Code Duplication
                    if (!empty($attribute)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
265
                        $this->etAlUseLast = boolval((string) $attribute);
266
                    } else if (!empty($parentStyleElement)) {
267
                        $this->etAlUseLast = $parentStyleElement->getEtAlUseLast();
268
                    }
269
270
                    break;
271
                case 'initialize':
272 View Code Duplication
                    if (!empty($attribute)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
273
                        $this->initialize = boolval((string) $attribute);
274
                    } else if (!empty($parentStyleElement)) {
275
                        $this->initialize = $parentStyleElement->getInitialize();
276
                    }
277
278
                    break;
279
                case 'initialize-with':
280
                    if (!empty($attribute)) {
281
                        $this->initializeWith = (string)$attribute;
282
                    } else if (!empty($parentStyleElement)) {
283
                        $this->initializeWith = $parentStyleElement->getInitializeWith();
284
                    }
285
                    break;
286
                case 'name-as-sort-order':
287
                    if (!empty($attribute)) {
288
                        $this->nameAsSortOrder = (string) $attribute;
289
                    } else if (!empty($parentStyleElement)) {
290
                        $this->nameAsSortOrder = $parentStyleElement->getNameAsSortOrder();
291
                    }
292
                    break;
293
                case 'sort-separator':
294
                    if (!empty($attribute)) {
295
                        $this->sortSeparator = (string) $attribute;
296
                    } else if (!empty($parentStyleElement)) {
297
                        $this->sortSeparator = $parentStyleElement->getSortSeparator();
298
                    }
299
                    break;
300
            }
301
        }
302
        $this->attributesInitialized = true;
303
    }
304
305
    /**
306
     * @return string
307
     */
308
    public function getAnd()
309
    {
310
        return $this->and;
311
    }
312
313
    /**
314
     * @param string $and
315
     */
316
    public function setAnd($and)
317
    {
318
        $this->and = $and;
319
    }
320
321
    /**
322
     * @return string
323
     */
324
    public function getDelimiterPrecedesEtAl()
325
    {
326
        return $this->delimiterPrecedesEtAl;
327
    }
328
329
    /**
330
     * @param string $delimiterPrecedesEtAl
331
     */
332
    public function setDelimiterPrecedesEtAl($delimiterPrecedesEtAl)
333
    {
334
        $this->delimiterPrecedesEtAl = $delimiterPrecedesEtAl;
335
    }
336
337
    /**
338
     * @return string
339
     */
340
    public function getDelimiterPrecedesLast()
341
    {
342
        return $this->delimiterPrecedesLast;
343
    }
344
345
    /**
346
     * @param string $delimiterPrecedesLast
347
     */
348
    public function setDelimiterPrecedesLast($delimiterPrecedesLast)
349
    {
350
        $this->delimiterPrecedesLast = $delimiterPrecedesLast;
351
    }
352
353
    /**
354
     * @return int
355
     */
356
    public function getEtAlMin()
357
    {
358
        return $this->etAlMin;
359
    }
360
361
    /**
362
     * @param int $etAlMin
363
     */
364
    public function setEtAlMin($etAlMin)
365
    {
366
        $this->etAlMin = $etAlMin;
367
    }
368
369
    /**
370
     * @return int
371
     */
372
    public function getEtAlUseFirst()
373
    {
374
        return $this->etAlUseFirst;
375
    }
376
377
    /**
378
     * @param int $etAlUseFirst
379
     */
380
    public function setEtAlUseFirst($etAlUseFirst)
381
    {
382
        $this->etAlUseFirst = $etAlUseFirst;
383
    }
384
385
    /**
386
     * @return bool
387
     */
388
    public function getEtAlUseLast()
389
    {
390
        return $this->etAlUseLast;
391
    }
392
393
    /**
394
     * @param bool $etAlUseLast
395
     */
396
    public function setEtAlUseLast($etAlUseLast)
397
    {
398
        $this->etAlUseLast = $etAlUseLast;
399
    }
400
401
    /**
402
     * @return int
403
     */
404
    public function getEtAlSubsequentMin()
405
    {
406
        return $this->etAlSubsequentMin;
407
    }
408
409
    /**
410
     * @param int $etAlSubsequentMin
411
     */
412
    public function setEtAlSubsequentMin($etAlSubsequentMin)
413
    {
414
        $this->etAlSubsequentMin = $etAlSubsequentMin;
415
    }
416
417
    /**
418
     * @return int
419
     */
420
    public function getEtAlSubsequentUseFirst()
421
    {
422
        return $this->etAlSubsequentUseFirst;
423
    }
424
425
    /**
426
     * @param int $etAlSubsequentUseFirst
427
     */
428
    public function setEtAlSubsequentUseFirst($etAlSubsequentUseFirst)
429
    {
430
        $this->etAlSubsequentUseFirst = $etAlSubsequentUseFirst;
431
    }
432
433
    /**
434
     * @return bool
435
     */
436
    public function getInitialize()
437
    {
438
        return $this->initialize;
439
    }
440
441
    /**
442
     * @param bool $initialize
443
     */
444
    public function setInitialize($initialize)
445
    {
446
        $this->initialize = $initialize;
447
    }
448
449
    /**
450
     * @return string
451
     */
452
    public function getInitializeWith()
453
    {
454
        return $this->initializeWith;
455
    }
456
457
    /**
458
     * @param string $initializeWith
459
     */
460
    public function setInitializeWith($initializeWith)
461
    {
462
        $this->initializeWith = $initializeWith;
463
    }
464
465
    /**
466
     * @return string
467
     */
468
    public function getNameAsSortOrder()
469
    {
470
        return $this->nameAsSortOrder;
471
    }
472
473
    /**
474
     * @param string $nameAsSortOrder
475
     */
476
    public function setNameAsSortOrder($nameAsSortOrder)
477
    {
478
        $this->nameAsSortOrder = $nameAsSortOrder;
479
    }
480
481
    /**
482
     * @return string
483
     */
484
    public function getSortSeparator()
485
    {
486
        return $this->sortSeparator;
487
    }
488
489
    /**
490
     * @param string $sortSeparator
491
     */
492
    public function setSortSeparator($sortSeparator)
493
    {
494
        $this->sortSeparator = $sortSeparator;
495
    }
496
497
}