Bit-Wasp /
bitcoin-php
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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
|
|||
| 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
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 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 |
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..