Conditions | 10 |
Paths | 12 |
Total Lines | 59 |
Code Lines | 41 |
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 |
||
61 | public function fromParser(Parser $parser) |
||
62 | { |
||
63 | $math = Bitcoin::getMath(); |
||
64 | $int32le = new Int32($math, ByteOrder::LE); |
||
65 | $uint32le = new Uint32($math, ByteOrder::LE); |
||
66 | $varint = new VarInt($math, ByteOrder::LE); |
||
67 | $vinParser = $this->vinParser(); |
||
68 | |||
69 | $version = $int32le->read($parser); |
||
70 | list ($vin) = $vinParser->parse($parser); |
||
71 | |||
72 | $vout = []; |
||
73 | $flags = 0; |
||
74 | if (count($vin) == 0) { |
||
75 | $flags = (int) $varint->read($parser); |
||
76 | if ($flags != 0) { |
||
77 | $vinCount = $varint->read($parser); |
||
78 | for ($i = 0; $i < $vinCount; $i++) { |
||
79 | $vin[] = $this->inputSerializer->fromParser($parser); |
||
80 | } |
||
81 | |||
82 | $voutCount = $varint->read($parser); |
||
83 | for ($i = 0; $i < $voutCount; $i++) { |
||
84 | $vout[] = $this->outputSerializer->fromParser($parser); |
||
85 | } |
||
86 | } |
||
87 | } else { |
||
88 | $voutCount = $varint->read($parser); |
||
89 | for ($i = 0; $i < $voutCount; $i++) { |
||
90 | $vout[] = $this->outputSerializer->fromParser($parser); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | $vwit = []; |
||
95 | if (($flags & 1)) { |
||
96 | echo "Check flags for witness: " . ($flags & 1 ? 'yes' : 'no') . PHP_EOL; |
||
97 | $flags ^= 1; |
||
98 | $witCount = count($vin); |
||
99 | for ($i = 0; $i < $witCount; $i++) { |
||
100 | echo "parse a witness structure\n"; |
||
101 | $vectorCount = $varint->read($parser); |
||
102 | $vwit[] = $this->witnessSerializer->fromParser($parser, $vectorCount); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | if ($flags) { |
||
107 | throw new \RuntimeException('Flags byte was 0'); |
||
108 | } |
||
109 | |||
110 | $lockTime = $uint32le->read($parser); |
||
111 | |||
112 | return new Transaction( |
||
113 | $version, |
||
114 | new TransactionInputCollection($vin), |
||
115 | new TransactionOutputCollection($vout), |
||
116 | new TransactionWitnessCollection($vwit), |
||
117 | $lockTime |
||
118 | ); |
||
119 | } |
||
120 | |||
160 |
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: