Passed
Push — feature/locators-issue-82 ( cc09c7...259e0f )
by Sebastian
04:29
created

Position   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 58
ccs 9
cts 12
cp 0.75
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 14 6
A isOnLastPosition() 0 4 2
A __construct() 0 4 1
A getPosition() 0 8 4
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\Constraint;
11
12
use Seboettg\CiteProc\CiteProc;
13
use Seboettg\Collection\ArrayList;
14
use stdClass;
15
16
/**
17
 * Class Position
18
 *
19
 * Tests whether the cite position matches the given positions (terminology: citations consist of one or more cites to
20
 * individual items). When called within the scope of cs:bibliography, position tests “false”. The positions that can
21
 * be tested are:
22
 *   - “first”: position of cites that are the first to reference an item
23
 *   - “ibid”/”ibid-with-locator”/”subsequent”: cites referencing previously cited items have the “subsequent” position.
24
 *     Such cites may also have the “ibid” or “ibid-with-locator” position when:
25
 *     a) the current cite immediately follows on another cite, within the same citation, that references the
26
 *        same item, or
27
 *     b) the current cite is the first cite in the citation, and the previous citation consists of a single cite
28
 *        referencing the same item
29
 *     If either requirement is met, the presence of locators determines which position is assigned:
30
 *     -  Preceding cite does not have a locator: if the current cite has a locator, the position of the current cite is
31
 *        “ibid-with-locator”. Otherwise the position is “ibid”.
32
 *     -  Preceding cite does have a locator: if the current cite has the same locator, the position of the current cite
33
 *        is “ibid”. If the locator differs the position is “ibid-with-locator”. If the current cite lacks a locator
34
 *        its only position is “subsequent”.
35
 *   - “near-note”: position of a cite following another cite referencing the same item. Both cites have to be located
36
 *     in foot or endnotes, and the distance between both cites may not exceed the maximum distance (measured in number
37
 *     of foot or endnotes) set with the near-note-distance option.
38
 *
39
 * Whenever position=”ibid-with-locator” tests true, position=”ibid” also tests true. And whenever position=”ibid” or
40
 * position=”near-note” test true, position=”subsequent” also tests true.
41
 *
42
 * @package Seboettg\CiteProc\Constraint
43
 *
44
 * @author Sebastian Böttger <[email protected]>
45
 */
46
class Position implements Constraint
47
{
48
    const FIRST = "first";
49
    const IBID = "ibid";
50
    const IBID_WITH_LOCATOR = "ibid-with-locator";
51
    const SUBSEQUENT = "subsequent";
52
    const NEAR_NOTE = "near-note";
53
54
    private $value;
55
56
    private $match;
57
58 3
    public function __construct($value, $match = "all")
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$match" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$match"; expected 0 but found 1
Loading history...
59
    {
60 3
        $this->value = $value;
61 3
        $this->match = $match;
62 3
    }
63
64
    /**
65
     * @codeCoverageIgnore
66
     * @param stdClass $object
67
     * @param int|null $citationNumber
68
     * @return bool
69
     */
70
    public function validate($object, $citationNumber = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$citationNumber" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$citationNumber"; expected 0 but found 1
Loading history...
71
    {
72
        if (CiteProc::getContext()->isModeBibliography()) {
73
            return false;
74
        }
75
        switch ($this->value) {
76
            case self::FIRST:
77
                return $this->getPosition($object) === null;
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getPosition($object) targeting Seboettg\CiteProc\Constr...Position::getPosition() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
78
            case self::IBID:
79
            case self::IBID_WITH_LOCATOR:
80
            case self::SUBSEQUENT:
81
                return $this->isOnLastPosition($object);
82
        }
83
        return true;
84
    }
85
86 1
    private function getPosition($object)
87
    {
88 1
        foreach (CiteProc::getContext()->getCitedItems() as $key => $value) {
89 1
            if (!empty($value->{'id'}) && $value->{'id'} === $object->{'id'}) {
90 1
                return $key;
91
            }
92
        }
93 1
        return null;
94
    }
95
96
    /**
97
     * @param stdClass $object
98
     * @return bool
99
     */
100
    private function isOnLastPosition($object): bool
101
    {
102
        $lastCitedItem = CiteProc::getContext()->getCitedItems()->last();
103
        return !empty($lastCitedItem) ? $lastCitedItem->{'id'} === $object->{'id'} : false;
104
    }
105
}
106