Completed
Push — master ( b2abd4...891c12 )
by thomas
22:03
created

Stack::end()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 2.032
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Interpreter;
4
5
use BitWasp\Buffertools\BufferInterface;
6
7
class Stack extends \SplDoublyLinkedList implements StackInterface
8
{
9 580
    public function __construct()
10
    {
11 580
        $this->setIteratorMode(\SplDoublyLinkedList::IT_MODE_FIFO | \SplDoublyLinkedList::IT_MODE_KEEP);
12 580
    }
13
14 32
    public function bottom()
15
    {
16 32
        return parent::offsetGet(count($this) - 1);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (offsetGet() instead of bottom()). Are you sure this is correct? If so, you might want to change this to $this->offsetGet().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
17
    }
18
19
    /**
20
     * @param BufferInterface $value
21
     * @throws \InvalidArgumentException
22
     */
23 28
    private function typeCheck($value)
24
    {
25 28
        if (!$value instanceof BufferInterface) {
26
            throw new \InvalidArgumentException('Value was not of type Buffer');
27
        }
28 28
    }
29
30
    /**
31
     * @see \ArrayAccess::offsetGet()
32
     * @param int $offset
33
     * @return \BitWasp\Buffertools\BufferInterface
34
     */
35 368
    public function offsetGet($offset)
36
    {
37 368
        $offset = count($this) + $offset;
38 368
        return parent::offsetGet($offset);
39
    }
40
41
    /**
42
     * @see \ArrayAccess::offsetSet()
43
     * @param int $offset
44
     * @param BufferInterface $value
45
     * @throws \InvalidArgumentException
46
     */
47 16
    public function offsetSet($offset, $value)
48
    {
49 16
        $this->typeCheck($value);
50 16
        $offset = count($this) + $offset;
51 16
        parent::offsetSet($offset, $value);
52 16
    }
53
54
    /**
55
     * @see \ArrayAccess::offsetExists()
56
     * @param int $offset
57
     * @return bool
58
     */
59 4
    public function offsetExists($offset)
60
    {
61 4
        $offset = count($this) + $offset;
62 4
        return parent::offsetExists($offset);
63
    }
64
65
    /**
66
     * @see \ArrayAccess::offsetUnset()
67
     * @param int $offset
68
     */
69 24
    public function offsetUnset($offset)
70
    {
71 24
        $offset = count($this) + $offset;
72 24
        parent::offsetUnset($offset);
73 20
    }
74
75
    /**
76
     * @param int $first
77
     * @param int $second
78
     */
79 16
    public function swap($first, $second)
80
    {
81 16
        $val1 = $this->offsetGet($first);
82 16
        $val2 = $this->offsetGet($second);
83 16
        $this->offsetSet($second, $val1);
84 16
        $this->offsetSet($first, $val2);
85 16
    }
86
87
    /**
88
     * @param int $index
89
     * @param BufferInterface $value
90
     */
91 12
    public function add($index, $value)
92
    {
93 12
        $this->typeCheck($value);
94
95 12
        if (getenv('HHVM_VERSION') || version_compare(phpversion(), '5.5.0', 'lt')) {
96
            if ($index == $this->count()) {
97
                $this->push($value);
98
            } else {
99
                $size = count($this);
100
                $temp = [];
101
                for ($i = $size; $i > $index; $i--) {
102
                    array_unshift($temp, $this->pop());
103
                }
104
105
                $this->push($value);
106
                foreach ($temp as $value) {
107
                    $this->push($value);
108
                }
109
            }
110
        } else {
111 12
            parent::add($index, $value);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SplDoublyLinkedList as the method add() does only exist in the following sub-classes of SplDoublyLinkedList: BitWasp\Bitcoin\Script\Interpreter\Stack. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
112
        }
113 8
    }
114
115
    /**
116
     * @return int
117
     */
118 540
    public function end()
119
    {
120 540
        $count = count($this);
121 540
        if ($count === 0) {
122 540
            return 0;
123
        }
124
125
        return $count - 1;
126
    }
127
128
    /**
129
     * @param int $length
130
     * @return $this
131
     */
132 20
    public function resize($length)
133
    {
134 20
        if ($length > count($this)) {
135
            throw new \RuntimeException('Invalid start or length');
136
        }
137
138 20
        while (count($this) > $length) {
139 20
            $this->pop();
140 15
        }
141
142 20
        return $this;
143
    }
144
}
145