Completed
Push — master ( 97b66a...92b407 )
by thomas
29:00
created

ScriptFactory::getNativeConsensus()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 3
eloc 2
nc 1
nop 2
crap 3
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 BufferInterface|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
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...
Bug introduced by
It seems like $string instanceof \BitW...ls\Buffer::hex($string) can also be of type object<BitWasp\Buffertools\Buffer>; however, BitWasp\Bitcoin\Script\ScriptFactory::create() does only seem to accept null|object<BitWasp\Buffertools\BufferInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

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