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

WitnessScriptFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A parse() 0 15 1
A multisig() 0 4 1
A payToPubKey() 0 4 1
A payToPubKeyHash() 0 4 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Factory;
4
5
use BitWasp\Bitcoin\Script\Opcodes;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
7
use BitWasp\Bitcoin\Script\Parser\Operation;
8
use BitWasp\Bitcoin\Script\Script;
9
use BitWasp\Bitcoin\Script\ScriptInterface;
10
use BitWasp\Bitcoin\Script\WitnessProgram;
11
12
class WitnessScriptFactory
13
{
14
    /**
15
     * @var OutputScriptFactory
16
     */
17
    private $scriptPubKey;
18
19
    /**
20
     * @var Opcodes
21
     */
22
    private $opcodes;
23
24
    /**
25
     * WitnessScriptFactory constructor.
26
     * @param OutputScriptFactory $scriptPubKey
27
     * @param Opcodes $opcodes
28
     */
29
    public function __construct(OutputScriptFactory $scriptPubKey, Opcodes $opcodes)
30
    {
31
        $this->scriptPubKey = $scriptPubKey;
32
        $this->opcodes = $opcodes;
33
    }
34
35
    /**
36
     * Parse a ScriptInterface into a WitnessProgram
37
     *
38
     * @param ScriptInterface $script
39
     * @return WitnessProgram
40
     */
41
    public function parse(ScriptInterface $script)
42
    {
43
        $parser = $script->getScriptParser();
44
        $decoded = $parser->decode();
45
46
        /**
47
         * @var Operation $versionPush
48
         * @var Operation $witnessPush
49
         */
50
        list ($versionPush, $witnessPush) = $decoded;
51
        $version = $versionPush->getData()->getInt();
52
        $program = new Script($witnessPush->getData());
53
54
        return new WitnessProgram($version, $program, $this->opcodes);
0 ignored issues
show
Documentation introduced by
$program is of type object<BitWasp\Bitcoin\Script\Script>, but the function expects a object<BitWasp\Buffertools\BufferInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to WitnessProgram::__construct() has too many arguments starting with $this->opcodes.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
55
    }
56
57
    /**
58
     * Create a multisig witness program
59
     *
60
     * @param int $version
61
     * @param int $m
62
     * @param array $keys
63
     * @param bool|true $sort
64
     * @return WitnessProgram
65
     */
66
    public function multisig($version, $m, array $keys, $sort = true)
67
    {
68
        return new WitnessProgram($version, $this->scriptPubKey->multisig($m, $keys, $sort), $this->opcodes);
0 ignored issues
show
Documentation introduced by
$this->scriptPubKey->multisig($m, $keys, $sort) is of type object<BitWasp\Bitcoin\Script\Script>, but the function expects a object<BitWasp\Buffertools\BufferInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to WitnessProgram::__construct() has too many arguments starting with $this->opcodes.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
69
    }
70
71
    /**
72
     * Create a pay-to-pubkey witness program
73
     *
74
     * @param int $version
75
     * @param PublicKeyInterface  $publicKey
76
     * @return WitnessProgram
77
     */
78
    public function payToPubKey($version, PublicKeyInterface $publicKey)
79
    {
80
        return new WitnessProgram($version, $this->scriptPubKey->payToPubKey($publicKey), $this->opcodes);
0 ignored issues
show
Documentation introduced by
$this->scriptPubKey->payToPubKey($publicKey) is of type object<BitWasp\Bitcoin\Script\Script>, but the function expects a object<BitWasp\Buffertools\BufferInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to WitnessProgram::__construct() has too many arguments starting with $this->opcodes.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
81
    }
82
83
    /**
84
     * Create a pay-to-pubkey-hash witness program
85
     *
86
     * @param int $version
87
     * @param PublicKeyInterface  $publicKey
88
     * @return WitnessProgram
89
     */
90
    public function payToPubKeyHash($version, PublicKeyInterface $publicKey)
91
    {
92
        return new WitnessProgram($version, $this->scriptPubKey->payToPubKeyHash($publicKey), $this->opcodes);
0 ignored issues
show
Documentation introduced by
$this->scriptPubKey->payToPubKeyHash($publicKey) is of type object<BitWasp\Bitcoin\Script\Script>, but the function expects a object<BitWasp\Buffertools\BufferInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to WitnessProgram::__construct() has too many arguments starting with $this->opcodes.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
93
    }
94
}
95