Completed
Pull Request — master (#244)
by thomas
201:55 queued 131:14
created

Stack::bottom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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