Completed
Pull Request — master (#230)
by thomas
27:32 queued 03:04
created

ScriptFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 89.29%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 17
c 5
b 0
f 2
lcom 1
cbo 11
dl 0
loc 103
ccs 25
cts 28
cp 0.8929
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A fromHex() 0 4 2
A create() 0 4 3
A scriptSig() 0 4 1
A scriptPubKey() 0 4 1
A p2sh() 0 4 1
A info() 0 4 1
A defaultFlags() 0 9 1
A getNativeConsensus() 0 4 3
A getBitcoinConsensus() 0 4 2
A consensus() 0 8 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Script;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
7
use BitWasp\Bitcoin\Flags;
8
use BitWasp\Bitcoin\Math\Math;
9
use BitWasp\Bitcoin\Script\Consensus\BitcoinConsensus;
10
use BitWasp\Bitcoin\Script\Consensus\NativeConsensus;
11
use BitWasp\Bitcoin\Script\Factory\InputScriptFactory;
12
use BitWasp\Bitcoin\Script\Factory\OutputScriptFactory;
13
use BitWasp\Bitcoin\Script\Factory\P2shScriptFactory;
14
use BitWasp\Bitcoin\Script\Factory\ScriptCreator;
15
use BitWasp\Bitcoin\Script\Factory\ScriptInfoFactory;
16
use BitWasp\Bitcoin\Script\Interpreter\InterpreterInterface;
17
use BitWasp\Buffertools\Buffer;
18
use BitWasp\Buffertools\BufferInterface;
19
20
class ScriptFactory
21
{
22
    /**
23
     * @param Buffer|string $string
24
     * @return Script
25
     */
26 708
    public static function fromHex($string)
27 6
    {
28 708
        return self::create($string instanceof BufferInterface ? $string : Buffer::hex($string))->getScript();
0 ignored issues
show
Bug introduced by
It seems like $string defined by parameter $string on line 26 can also be of type object<BitWasp\Buffertools\Buffer>; however, BitWasp\Buffertools\Buffer::hex() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Documentation introduced by
$string instanceof \BitW...ls\Buffer::hex($string) is of type object<BitWasp\Buffertools\Buffer>, but the function expects a null|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...
Bug introduced by
The class BitWasp\Buffertools\BufferInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
29
    }
30
31
    /**
32
     * @param BufferInterface|null $buffer
33
     * @param Opcodes|null $opcodes
34
     * @param Math|null $math
35
     * @return ScriptCreator
36
     */
37 1137
    public static function create(BufferInterface $buffer = null, Opcodes $opcodes = null, Math $math = null)
38
    {
39 1137
        return new ScriptCreator($math ?: Bitcoin::getMath(), $opcodes ?: new Opcodes(), $buffer);
40
    }
41
42
    /**
43
     * @return InputScriptFactory
44
     */
45 93
    public static function scriptSig()
46
    {
47 93
        return new InputScriptFactory();
48
    }
49
50
    /**
51
     * @return OutputScriptFactory
52
     */
53 345
    public static function scriptPubKey()
54
    {
55 345
        return new OutputScriptFactory();
56
    }
57
58
    /**
59
     * @return P2shScriptFactory
60
     */
61 42
    public static function p2sh()
62
    {
63 42
        return new P2shScriptFactory(self::scriptPubKey());
64
    }
65
66
    /**
67
     * @param ScriptInterface $script
68
     * @param ScriptInterface|null $redeemScript
69
     * @return ScriptInfo\ScriptInfoInterface
70
     */
71 135
    public static function info(ScriptInterface $script, ScriptInterface $redeemScript = null)
72
    {
73 135
        return (new ScriptInfoFactory())->load($script, $redeemScript);
74
    }
75
76
77
    /**
78
     * @return Flags
79
     */
80 24
    public static function defaultFlags()
81
    {
82 24
        return new Flags(
83 24
            InterpreterInterface::VERIFY_P2SH | InterpreterInterface::VERIFY_STRICTENC | InterpreterInterface::VERIFY_DERSIG |
84 24
            InterpreterInterface::VERIFY_LOW_S | InterpreterInterface::VERIFY_NULL_DUMMY | InterpreterInterface::VERIFY_SIGPUSHONLY |
85 24
            InterpreterInterface::VERIFY_DISCOURAGE_UPGRADABLE_NOPS | InterpreterInterface::VERIFY_CLEAN_STACK |
86 24
            InterpreterInterface::VERIFY_CHECKLOCKTIMEVERIFY | InterpreterInterface::VERIFY_CHECKSEQUENCEVERIFY
87 24
        );
88
    }
89
90
    /**
91
     * @param Flags|null $flags
92
     * @param EcAdapterInterface|null $ecAdapter
93
     * @return NativeConsensus
94
     */
95 12
    public static function getNativeConsensus(Flags $flags = null, EcAdapterInterface $ecAdapter = null)
96
    {
97 12
        return new NativeConsensus($ecAdapter ?: Bitcoin::getEcAdapter(), $flags ?: self::defaultFlags());
98
    }
99
100
    /**
101
     * @param Flags|null $flags
102
     * @return BitcoinConsensus
103
     */
104
    public static function getBitcoinConsensus(Flags $flags = null)
105
    {
106
        return new BitcoinConsensus($flags ?: self::defaultFlags());
107
    }
108
109
    /**
110
     * @param Flags|null $flags
111
     * @param EcAdapterInterface|null $ecAdapter
112
     * @return \BitWasp\Bitcoin\Script\Consensus\ConsensusInterface
113
     */
114 6
    public static function consensus(Flags $flags = null, EcAdapterInterface $ecAdapter = null)
115
    {
116 6
        if (extension_loaded('bitcoinconsensus')) {
117
            return self::getBitcoinConsensus($flags);
118
        } else {
119 6
            return self::getNativeConsensus($flags, $ecAdapter);
120
        }
121
    }
122
}
123