| Conditions | 17 |
| Paths | 84 |
| Total Lines | 52 |
| Code Lines | 31 |
| 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 |
||
| 80 | public function normalize(array $outputs, NetworkInterface $network = null) { |
||
| 81 | $network = $network ?: Bitcoin::getNetwork(); |
||
| 82 | if (empty($outputs)) { |
||
| 83 | return []; |
||
| 84 | } |
||
| 85 | |||
| 86 | $keys = array_keys($outputs); |
||
| 87 | $newOutputs = []; |
||
| 88 | if (is_int($keys[0])) { |
||
| 89 | foreach ($outputs as $i => $output) { |
||
| 90 | if (!is_int($i)) { |
||
| 91 | throw new BlocktrailSDKException("Encountered invalid index while traversing numerically indexed list"); |
||
| 92 | } |
||
| 93 | if (!is_array($output)) { |
||
| 94 | throw new BlocktrailSDKException("Encountered invalid output while traversing numerically indexed list"); |
||
| 95 | } |
||
| 96 | $newOutputs[] = $this->readArrayFormat($output, $network); |
||
|
|
|||
| 97 | } |
||
| 98 | } else if (is_string($keys[0])) { |
||
| 99 | foreach ($outputs as $address => $value) { |
||
| 100 | if (!is_string($address)) { |
||
| 101 | throw new BlocktrailSDKException("Encountered invalid address while traversing address keyed list.."); |
||
| 102 | } |
||
| 103 | |||
| 104 | $newOutputs[] = $this->parseAddressOutput($address, $value, $network); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | foreach ($newOutputs as &$newOutput) { |
||
| 109 | if (fmod($newOutput['value'], 1)) { |
||
| 110 | throw new BlocktrailSDKException("Value should be in Satoshis"); |
||
| 111 | } |
||
| 112 | |||
| 113 | if (is_string($newOutput['scriptPubKey'])) { |
||
| 114 | $newOutput['scriptPubKey'] = ScriptFactory::fromHex($newOutput['scriptPubKey']); |
||
| 115 | } |
||
| 116 | |||
| 117 | if (strlen($newOutput['scriptPubKey']->getBinary()) < 1) { |
||
| 118 | throw new BlocktrailSDKException("Script cannot be empty"); |
||
| 119 | } |
||
| 120 | |||
| 121 | if ($newOutput['scriptPubKey']->getBinary()[0] !== "\x6a") { |
||
| 122 | if (!$newOutput['value']) { |
||
| 123 | throw new BlocktrailSDKException("Values should be non zero"); |
||
| 124 | } else if ($newOutput['value'] < Blocktrail::DUST) { |
||
| 125 | throw new BlocktrailSDKException("Values should be more than dust (" . Blocktrail::DUST . ")"); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | return $newOutputs; |
||
| 131 | } |
||
| 132 | } |
||
| 133 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.