Completed
Pull Request — master (#9)
by Tomáš
07:14 queued 04:10
created

ProxySourceItem   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 29
lcom 2
cbo 3
dl 0
loc 139
ccs 0
cts 56
cp 0
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 4 1
A meta() 0 4 1
A url() 0 4 1
A blocks() 0 4 1
A setBlocks() 0 4 2
A previousItem() 0 4 1
B setPreviousItem() 0 17 5
A nextItem() 0 4 1
B setNextItem() 0 17 5
A reprocess() 0 4 1
A offsetSet() 0 17 4
A offsetExists() 0 4 2
A offsetUnset() 0 6 2
A offsetGet() 0 8 2
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Contrib\ProxySourceCollection;
13
14
use Symplify\PHP7_Sculpin\Core\Source\ProxySource;
15
16
class ProxySourceItem extends ProxySource implements \ArrayAccess
17
{
18
    /**
19
     * @var
20
     */
21
    private $previousItem;
22
23
    /**
24
     * @var
25
     */
26
    private $nextItem;
27
28
    public function id()
29
    {
30
        return $this->sourceId();
31
    }
32
33
    public function meta()
34
    {
35
        return $this->data()->export();
36
    }
37
38
    public function url()
39
    {
40
        return $this->permalink()->relativeUrlPath();
41
    }
42
43
//    public function date()
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
//    {
45
//        $calculatedDate = $this->data()->get('calculated_date');
46
47
//        return $calculatedDate ?: $this->data()->get('date');
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
//    }
49
50
//    public function title()
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
//    {
52
//        return $this->data()->get('title');
53
//    }
54
55
    public function blocks()
56
    {
57
        return $this->data()->get('blocks');
58
    }
59
60
    public function setBlocks(array $blocks = null)
61
    {
62
        $this->data()->set('blocks', $blocks ?: []);
63
    }
64
65
    public function previousItem()
66
    {
67
        return $this->previousItem;
68
    }
69
70
    public function setPreviousItem(ProxySourceItem $item = null)
71
    {
72
        $lastPreviousItem = $this->previousItem;
73
        $this->previousItem = $item;
74
        if ($lastPreviousItem) {
75
            // We did have a item before...
76
            if (!$item || $item->id() !== $lastPreviousItem->id()) {
77
                // But we no longer have a item or the item we
78
                // were given does not have the same ID as the
79
                // last one we had...
80
                $this->reprocess();
81
            }
82
        } elseif ($item) {
83
            // We didn't have a item before but we do now...
84
            $this->reprocess();
85
        }
86
    }
87
88
    public function nextItem()
89
    {
90
        return $this->nextItem;
91
    }
92
93
    public function setNextItem(ProxySourceItem $item = null)
94
    {
95
        $lastNextItem = $this->nextItem;
96
        $this->nextItem = $item;
97
        if ($lastNextItem) {
98
            // We did have a item before...
99
            if (!$item || $item->id() !== $lastNextItem->id()) {
100
                // But we no longer have a item or the item we
101
                // were given does not have the same ID as the
102
                // last one we had...
103
                $this->reprocess();
104
            }
105
        } elseif ($item) {
106
            // We didn't have a item before but we do now...
107
            $this->reprocess();
108
        }
109
    }
110
111
    public function reprocess()
112
    {
113
        $this->setHasChanged();
114
    }
115
116
    public function offsetSet($offset, $value)
117
    {
118
        if (is_null($offset)) {
119
            throw new \InvalidArgumentException('Proxy source items cannot have values pushed onto them');
120
        } else {
121
            if (method_exists($this, $offset)) {
122
                return call_user_func([$this, $offset, $value]);
123
            }
124
125
            $setMethod = 'set'.ucfirst($offset);
126
            if (method_exists($this, $setMethod)) {
127
                return call_user_func([$this, $setMethod, $value]);
128
            }
129
130
            $this->data()->set($offset, $value);
131
        }
132
    }
133
134
    public function offsetExists($offset)
135
    {
136
        return !method_exists($this, $offset) && null !== $this->data()->get($offset);
137
    }
138
139
    public function offsetUnset($offset)
140
    {
141
        if (!method_exists($this, $offset)) {
142
            $this->data()->remove($offset);
0 ignored issues
show
Bug introduced by
The method remove() does not seem to exist on object<Symplify\PHP7_Scu...guration\Configuration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
143
        }
144
    }
145
146
    public function offsetGet($offset)
147
    {
148
        if (method_exists($this, $offset)) {
149
            return call_user_func([$this, $offset]);
150
        }
151
152
        return $this->data()->get($offset);
153
    }
154
}
155