| Conditions | 5 |
| Paths | 6 |
| Total Lines | 54 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 136 | public function fromParser(Parser $parser) |
||
| 137 | { |
||
| 138 | $math = Bitcoin::getMath(); |
||
| 139 | $int32le = new Int32($math, ByteOrder::LE); |
||
| 140 | $uint32le = new Uint32($math, ByteOrder::LE); |
||
| 141 | |||
| 142 | $varint = new VarInt($math, ByteOrder::LE); |
||
| 143 | $inputsSerializer = new Vector($varint, function (Parser $parser) { |
||
| 144 | return $this->inputSerializer->fromParser($parser); |
||
| 145 | }); |
||
| 146 | $flagsSerializer = new Uint8($math, ByteOrder::BE); |
||
| 147 | $outputsSerializer = new Vector($varint, function (Parser $parser) { |
||
| 148 | return $this->outputSerializer->fromParser($parser); |
||
| 149 | }); |
||
| 150 | |||
| 151 | $vcharSerializer = new VarString($varint); |
||
| 152 | $witnessSerializer = new Vector($varint, function (Parser $parser) use ($vcharSerializer) { |
||
| 153 | return $vcharSerializer->read($parser); |
||
| 154 | }); |
||
| 155 | |||
| 156 | $version = $int32le->read($parser); |
||
| 157 | $dummy = (int) $flagsSerializer->read($parser); |
||
| 158 | if ($dummy !== 0) { |
||
| 159 | throw new \RuntimeException('Non-zero dummy in witness-bearing transaction'); |
||
| 160 | } |
||
| 161 | |||
| 162 | $flags = (int) $flagsSerializer->read($parser); |
||
| 163 | if ($math->cmp($flags, 0) != 0) { |
||
| 164 | $vInputs = $inputsSerializer->read($parser); |
||
| 165 | $vOutputs = $outputsSerializer->read($parser); |
||
| 166 | } else { |
||
| 167 | throw new \RuntimeException('Unknown flag'); |
||
| 168 | } |
||
| 169 | |||
| 170 | $vWitness = []; |
||
| 171 | if ($flags & 1) { |
||
| 172 | $flags ^= 1; |
||
| 173 | $vWitness = $witnessSerializer->read($parser); |
||
| 174 | } |
||
| 175 | |||
| 176 | if ($flags) { |
||
| 177 | throw new \RuntimeException('Unknown optional data'); |
||
| 178 | } |
||
| 179 | |||
| 180 | $locktime = $uint32le->read($parser); |
||
| 181 | |||
| 182 | return new Transaction( |
||
| 183 | $version, |
||
| 184 | new TransactionInputCollection($vInputs), |
||
| 185 | new TransactionOutputCollection($vOutputs), |
||
| 186 | new TransactionWitnessCollection($vWitness), |
||
| 187 | $locktime |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 201 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: