Completed
Pull Request — master (#241)
by thomas
133:23 queued 63:06
created

WitnessScriptFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 3
c 2
b 0
f 2
lcom 1
cbo 6
dl 0
loc 57
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 4 1
A parse() 0 15 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Factory;
4
5
use BitWasp\Bitcoin\Script\Opcodes;
6
use BitWasp\Bitcoin\Script\Parser\Operation;
7
use BitWasp\Bitcoin\Script\Script;
8
use BitWasp\Bitcoin\Script\ScriptInterface;
9
use BitWasp\Bitcoin\Script\WitnessProgram;
10
11
class WitnessScriptFactory
12
{
13
    /**
14
     * @var OutputScriptFactory
15
     */
16
    private $scriptPubKey;
17
18
    /**
19
     * @var Opcodes
20
     */
21
    private $opcodes;
22
23
    /**
24
     * WitnessScriptFactory constructor.
25
     * @param OutputScriptFactory $scriptPubKey
26
     * @param P2shScriptFactory $redeemScript
27
     * @param Opcodes $opcodes
28
     */
29
    public function __construct(OutputScriptFactory $scriptPubKey, P2shScriptFactory $redeemScript, Opcodes $opcodes)
30
    {
31
        $this->scriptPubKey = $scriptPubKey;
32
        $this->redeemScript = $redeemScript;
0 ignored issues
show
Bug introduced by
The property redeemScript does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
        $this->opcodes = $opcodes;
34
    }
35
36
    /**
37
     * @param int $version
38
     * @param ScriptInterface $script
39
     * @return WitnessProgram
40
     */
41
    public function create($version, ScriptInterface $script)
42
    {
43
        return new WitnessProgram($version, $script);
0 ignored issues
show
Documentation introduced by
$script is of type object<BitWasp\Bitcoin\Script\ScriptInterface>, 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...
44
    }
45
46
    /**
47
     * Parse a ScriptInterface into a WitnessProgram
48
     *
49
     * @param ScriptInterface $script
50
     * @return WitnessProgram
51
     */
52
    public function parse(ScriptInterface $script)
53
    {
54
        $parser = $script->getScriptParser();
55
        $decoded = $parser->decode();
56
57
        /**
58
         * @var Operation $versionPush
59
         * @var Operation $witnessPush
60
         */
61
        list ($versionPush, $witnessPush) = $decoded;
62
        $version = $versionPush->getData()->getInt();
63
        $program = new Script($witnessPush->getData());
64
65
        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...
66
    }
67
}
68