Completed
Pull Request — master (#239)
by thomas
49:01 queued 45:29
created

WitnessPubKeyHashAddress   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 70
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A getWitnessVersion() 0 4 1
A getHash() 0 4 1
A getPrefixByte() 0 4 1
A getAddress() 0 9 3
1
<?php
2
3
namespace BitWasp\Bitcoin\Address;
4
5
use BitWasp\Bitcoin\Base58;
6
use BitWasp\Bitcoin\Bitcoin;
7
use BitWasp\Bitcoin\Network\NetworkInterface;
8
use BitWasp\Buffertools\Buffer;
9
use BitWasp\Buffertools\BufferInterface;
10
11
class WitnessPubKeyHashAddress implements AddressInterface
12
{
13
    /**
14
     * @var int
15
     */
16
    private $witnessVersion;
17
18
    /**
19
     * @var BufferInterface
20
     */
21
    private $hash;
22
23
    /**
24
     * WitnessAddress constructor.
25
     * @param int $witnessVersion
26
     * @param BufferInterface $hash
27
     */
28
    public function __construct($witnessVersion, BufferInterface $hash)
29
    {
30
        if (!is_int($witnessVersion)) {
31
            throw new \RuntimeException('Witness version must be an integer');
32
        }
33
34
        if ($hash->getSize() !== 20) {
35
            throw new \RuntimeException('Hash for P2WPKH address must be 20 bytes');
36
        }
37
38
        $this->witnessVersion = $witnessVersion;
39
        $this->hash = $hash->getHex();
0 ignored issues
show
Documentation Bug introduced by
It seems like $hash->getHex() of type string is incompatible with the declared type object<BitWasp\Buffertools\BufferInterface> of property $hash.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
    }
41
42
    /**
43
     * @return int
44
     */
45
    public function getWitnessVersion()
46
    {
47
        return $this->witnessVersion;
48
    }
49
50
    /**
51
     * @return BufferInterface|string
52
     */
53
    public function getHash()
54
    {
55
        return $this->hash;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->hash; (BitWasp\Buffertools\BufferInterface) is incompatible with the return type declared by the interface BitWasp\Bitcoin\Address\AddressInterface::getHash of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
56
    }
57
58
    /**
59
     * @param NetworkInterface $network
60
     * @return string
61
     */
62
    public function getPrefixByte(NetworkInterface $network)
63
    {
64
        return $network->getP2WPKHByte();
65
    }
66
67
    /**
68
     * @param NetworkInterface|null $network
69
     * @return string
70
     */
71
    public function getAddress(NetworkInterface $network = null)
72
    {
73
        $network = $network ?: Bitcoin::getNetwork();
74
        $witnessByte = dechex($this->witnessVersion);
75
        $witnessByte = strlen($witnessByte) % 2 == 0 ? $witnessByte : '0' . $witnessByte;
76
77
        $payload = Buffer::hex($this->getPrefixByte($network) . $witnessByte . "00" . $this->getHash());
78
        return Base58::encodeCheck($payload);
79
    }
80
}
81