Complex classes like OutputClassifier often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OutputClassifier, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class OutputClassifier |
||
| 12 | { |
||
| 13 | const PAYTOPUBKEY = 'pubkey'; |
||
| 14 | const PAYTOPUBKEYHASH = 'pubkeyhash'; |
||
| 15 | const PAYTOSCRIPTHASH = 'scripthash'; |
||
| 16 | const WITNESS_V0_KEYHASH = 'witness_v0_keyhash'; |
||
| 17 | const WITNESS_V0_SCRIPTHASH = 'witness_v0_scripthash'; |
||
| 18 | const MULTISIG = 'multisig'; |
||
| 19 | const UNKNOWN = 'unknown'; |
||
| 20 | const NONSTANDARD = 'nonstandard'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var \BitWasp\Bitcoin\Script\Parser\Operation[] |
||
| 24 | */ |
||
| 25 | private $decoded; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var ScriptInterface |
||
| 29 | */ |
||
| 30 | private $script; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param ScriptInterface $script |
||
| 34 | */ |
||
| 35 | 198 | public function __construct(ScriptInterface $script) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @param BufferInterface|null $publicKey |
||
| 43 | * @return bool |
||
| 44 | */ |
||
| 45 | 144 | public function isPayToPublicKey(& $publicKey = null) |
|
| 46 | { |
||
| 47 | 144 | if (count($this->decoded) < 1 || !$this->decoded[0]->isPush()) { |
|
| 48 | 120 | return false; |
|
| 49 | } |
||
| 50 | |||
| 51 | 42 | $size = $this->decoded[0]->getDataSize(); |
|
| 52 | 42 | if ($size === 33 || $size === 65) { |
|
| 53 | 30 | $op = $this->decoded[1]; |
|
| 54 | 30 | if (!$op->isPush() && $op->getOp() === Opcodes::OP_CHECKSIG) { |
|
| 55 | 30 | $publicKey = $this->decoded[0]->getData(); |
|
| 56 | 30 | return true; |
|
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | 12 | return false; |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param BufferInterface|null $pubKeyHash |
||
| 65 | * @return bool |
||
| 66 | */ |
||
| 67 | 114 | public function isPayToPublicKeyHash(& $pubKeyHash = null) |
|
| 68 | { |
||
| 69 | 114 | if (count($this->decoded) !== 5) { |
|
| 70 | 54 | return false; |
|
| 71 | } |
||
| 72 | |||
| 73 | 66 | $dup = $this->decoded[0]; |
|
| 74 | 66 | $hash = $this->decoded[1]; |
|
| 75 | 66 | $buf = $this->decoded[2]; |
|
| 76 | 66 | $eq = $this->decoded[3]; |
|
| 77 | 66 | $checksig = $this->decoded[4]; |
|
| 78 | |||
| 79 | 66 | foreach ([$dup, $hash, $eq, $checksig] as $op) { |
|
| 80 | /** @var Operation $op */ |
||
| 81 | 66 | if ($op->isPush()) { |
|
| 82 | return false; |
||
| 83 | } |
||
| 84 | 66 | } |
|
| 85 | |||
| 86 | 66 | if ($dup->getOp() === Opcodes::OP_DUP |
|
| 87 | 66 | && $hash->getOp() === Opcodes::OP_HASH160 |
|
| 88 | 66 | && $buf->isPush() && $buf->getDataSize() === 20 |
|
| 89 | 66 | && $eq->getOp() === Opcodes::OP_EQUALVERIFY |
|
| 90 | 66 | && $checksig->getOp() === Opcodes::OP_CHECKSIG) { |
|
| 91 | 66 | $pubKeyHash = $this->decoded[2]->getData(); |
|
| 92 | 66 | return true; |
|
| 93 | } |
||
| 94 | |||
| 95 | return false; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param BufferInterface|null $scriptHash |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | 168 | public function isPayToScriptHash(& $scriptHash = null) |
|
| 103 | { |
||
| 104 | 168 | if (count($this->decoded) !== 3) { |
|
| 105 | 138 | return false; |
|
| 106 | } |
||
| 107 | |||
| 108 | 72 | $hash = $this->decoded[0]; |
|
| 109 | 72 | if ($hash->isPush() || !$hash->getOp() === Opcodes::OP_HASH160) { |
|
| 110 | return false; |
||
| 111 | } |
||
| 112 | |||
| 113 | 72 | $buffer = $this->decoded[1]; |
|
| 114 | 72 | if (!$buffer->isPush() || $buffer->getDataSize() !== 20) { |
|
| 115 | return false; |
||
| 116 | } |
||
| 117 | |||
| 118 | |||
| 119 | 72 | $eq = $this->decoded[2]; |
|
| 120 | 72 | if (!$eq->isPush() && $eq->getOp() === Opcodes::OP_EQUAL) { |
|
| 121 | 72 | $scriptHash = $this->decoded[1]->getData(); |
|
| 122 | 72 | return true; |
|
| 123 | } |
||
| 124 | |||
| 125 | return false; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param BufferInterface[] $keys |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | 78 | public function isMultisig(& $keys = []) |
|
| 133 | { |
||
| 134 | 78 | $count = count($this->decoded); |
|
| 135 | 78 | if ($count <= 3) { |
|
| 136 | 30 | return false; |
|
| 137 | } |
||
| 138 | |||
| 139 | 48 | $mOp = $this->decoded[0]; |
|
| 140 | 48 | $nOp = $this->decoded[$count - 2]; |
|
| 141 | 48 | $checksig = $this->decoded[$count - 1]; |
|
| 142 | 48 | if ($mOp->isPush() || $nOp->isPush() || $checksig->isPush()) { |
|
| 143 | return false; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** @var Operation[] $vKeys */ |
||
| 147 | 48 | $vKeys = array_slice($this->decoded, 1, -2); |
|
| 148 | 48 | $solutions = []; |
|
| 149 | 48 | foreach ($vKeys as $key) { |
|
| 150 | 48 | if (!$key->isPush() || !PublicKey::isCompressedOrUncompressed($key->getData())) { |
|
| 151 | return false; |
||
| 152 | } |
||
| 153 | 48 | $solutions[] = $key->getData(); |
|
| 154 | 48 | } |
|
| 155 | |||
| 156 | 48 | if ($mOp->getOp() >= Opcodes::OP_0 |
|
| 157 | 48 | && $nOp->getOp() <= Opcodes::OP_16 |
|
| 158 | 48 | && $checksig->getOp() === Opcodes::OP_CHECKMULTISIG) { |
|
| 159 | 48 | $keys = $solutions; |
|
| 160 | 48 | return true; |
|
| 161 | } |
||
| 162 | |||
| 163 | return false; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param BufferInterface $programHash |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | 198 | public function isWitness(& $programHash = null) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param BufferInterface|BufferInterface[] $solutions |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | 138 | public function classify(&$solutions = null) |
|
| 232 | } |
||
| 233 |
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: