Completed
Push — master ( ce605d...b98633 )
by thomas
24:11 queued 20:21
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
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
crap 2.032
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 870
    public function __construct()
10
    {
11 870
        $this->setIteratorMode(\SplDoublyLinkedList::IT_MODE_FIFO | \SplDoublyLinkedList::IT_MODE_KEEP);
12 870
    }
13
14 48
    public function bottom()
15
    {
16 48
        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 mixed $value
21
     * @throws \InvalidArgumentException
22
     */
23 42
    private function typeCheck($value)
24
    {
25 42
        if (!$value instanceof BufferInterface) {
26
            throw new \InvalidArgumentException('Value was not of type Buffer');
27
        }
28 42
    }
29
30
    /**
31
     * @see \ArrayAccess::offsetGet()
32
     * @param int $offset
33
     * @return \BitWasp\Buffertools\BufferInterface
34
     */
35 552
    public function offsetGet($offset)
36
    {
37 552
        $offset = count($this) + $offset;
38 552
        return parent::offsetGet($offset);
39
    }
40
41
    /**
42
     * @see \ArrayAccess::offsetSet()
43
     * @param int $offset
44
     * @param BufferInterface $value
45
     * @throws \InvalidArgumentException
46
     */
47 24
    public function offsetSet($offset, $value)
48
    {
49 24
        $this->typeCheck($value);
50 24
        $offset = count($this) + $offset;
51 24
        parent::offsetSet($offset, $value);
52 24
    }
53
54
    /**
55
     * @see \ArrayAccess::offsetExists()
56
     * @param int $offset
57
     * @return bool
58
     */
59 6
    public function offsetExists($offset)
60
    {
61 6
        $offset = count($this) + $offset;
62 6
        return parent::offsetExists($offset);
63
    }
64
65
    /**
66
     * @see \ArrayAccess::offsetUnset()
67
     * @param int $offset
68
     */
69 36
    public function offsetUnset($offset)
70
    {
71 36
        $offset = count($this) + $offset;
72 36
        parent::offsetUnset($offset);
73 30
    }
74
75
    /**
76
     * @param int $first
77
     * @param int $second
78
     */
79 24
    public function swap($first, $second)
80
    {
81 24
        $val1 = $this->offsetGet($first);
82 24
        $val2 = $this->offsetGet($second);
83 24
        $this->offsetSet($second, $val1);
84 24
        $this->offsetSet($first, $val2);
85 24
    }
86
87
    /**
88
     * @param int $index
89
     * @param BufferInterface $value
90
     */
91 18
    public function add($index, $value)
92
    {
93 18
        $this->typeCheck($value);
94
95 18
        if (getenv('HHVM_VERSION') || version_compare(phpversion(), '5.5.0', 'lt')) {
96 6
            if ($index == $this->count()) {
97 2
                $this->push($value);
98 2
            } else {
99 4
                $size = count($this);
100 4
                $temp = [];
101 4
                for ($i = $size; $i > $index; $i--) {
102 4
                    array_unshift($temp, $this->pop());
103 4
                }
104
105 2
                $this->push($value);
106 2
                foreach ($temp as $value) {
107 2
                    $this->push($value);
108 2
                }
109
            }
110
111 4
        } else {
112 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...
113
        }
114 12
    }
115
116
    /**
117
     * @return int
118
     */
119 810
    public function end()
120
    {
121 810
        $count = count($this);
122 810
        if ($count === 0) {
123 810
            return 0;
124
        }
125
126
        return $count - 1;
127
    }
128
129
    /**
130
     * @param $length
131
     * @return $this
132
     */
133 30
    public function resize($length)
134
    {
135 30
        if ($length > count($this)) {
136
            throw new \RuntimeException('Invalid start or length');
137
        }
138
139 30
        while (count($this) > $length) {
140 30
            $this->pop();
141 30
        }
142
143 30
        return $this;
144
    }
145
}
146