Conditions | 15 |
Paths | 780 |
Total Lines | 68 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
123 | public function testCases($fixture) |
||
124 | { |
||
125 | $defaultPolicy = Interpreter::VERIFY_NONE | Interpreter::VERIFY_P2SH | Interpreter::VERIFY_WITNESS | Interpreter::VERIFY_CHECKLOCKTIMEVERIFY | Interpreter::VERIFY_CHECKSEQUENCEVERIFY;; |
||
126 | $txBuilder = new TxBuilder(); |
||
127 | if (array_key_exists('version', $fixture['raw'])) { |
||
128 | $txBuilder->version((int) $fixture['raw']['version']); |
||
129 | } |
||
130 | |||
131 | $totalOut = 12345; |
||
132 | foreach ($fixture['raw']['outs'] as $output) { |
||
133 | $txBuilder->output($output['value'], ScriptFactory::fromHex($output['script'])); |
||
134 | $totalOut += $output['value']; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @var SignData[] $signDatas |
||
139 | * @var Utxo[] $utxos |
||
140 | */ |
||
141 | $signDatas = []; |
||
142 | $utxos = []; |
||
143 | foreach ($fixture['raw']['ins'] as $input) { |
||
144 | $scriptPubKey = ScriptFactory::fromHex($input['scriptPubKey']); |
||
145 | if (array_key_exists('value', $input)) { |
||
146 | echo "needs value: {$input['value']}\n"; |
||
147 | } |
||
148 | |||
149 | $value = array_key_exists('value', $input) ? (int) $input['value'] : $totalOut; |
||
150 | $utxo = $this->fundOutput($scriptPubKey, $value); |
||
151 | |||
152 | $sequence = array_key_exists('sequence', $input) ? (int) $input['sequence'] : 0xffffffff; |
||
153 | $txBuilder->spendOutPoint($utxo->getOutPoint(), null, $sequence); |
||
154 | |||
155 | $signData = new SignData(); |
||
156 | if (array_key_exists('redeemScript', $input) && "" !== $input['redeemScript']) { |
||
157 | $signData->p2sh(ScriptFactory::fromHex($input['redeemScript'])); |
||
158 | } |
||
159 | if (array_key_exists('witnessScript', $input) && "" !== $input['witnessScript']) { |
||
160 | $signData->p2wsh(ScriptFactory::fromHex($input['witnessScript'])); |
||
161 | } |
||
162 | |||
163 | $policy = array_key_exists('signaturePolicy', $fixture) ? $this->getScriptFlagsFromString($fixture['signaturePolicy']) : $defaultPolicy; |
||
164 | $signData->signaturePolicy($policy); |
||
165 | $signDatas[] = $signData; |
||
166 | |||
167 | $utxos[] = $utxo; |
||
168 | } |
||
169 | |||
170 | $txBuilder->locktime(isset($fixture['raw']['locktime']) ? $fixture['raw']['locktime'] : 0); |
||
171 | |||
172 | $signer = new Signer($txBuilder->get()); |
||
173 | foreach ($fixture['raw']['ins'] as $i => $input) { |
||
174 | $iSigner = $signer->input($i, $utxos[$i]->getOutput(), $signDatas[$i]); |
||
175 | foreach ($input['keys'] as $key) { |
||
176 | $priv = PrivateKeyFactory::fromWif($key['key'], null, NetworkFactory::bitcoinTestnet()); |
||
177 | $sigHashType = $key['sigHashType']; |
||
178 | $iSigner->sign($priv, $sigHashType); |
||
179 | } |
||
180 | |||
181 | $this->assertTrue($iSigner->isFullySigned()); |
||
182 | } |
||
183 | |||
184 | $tx = $signer->get(); |
||
185 | $result = $this->makeRpcRequest('sendrawtransaction', [$tx->getHex(), true]); |
||
186 | $this->assertEquals(null, $result['error']); |
||
187 | |||
188 | $txid = $result['result']; |
||
189 | $this->assertEquals(64, strlen($txid)); |
||
190 | } |
||
191 | } |
||
192 |