1 | <?php |
||
13 | class HierarchicalKeySequence |
||
14 | { |
||
15 | /** |
||
16 | * @var Math |
||
17 | */ |
||
18 | private $math; |
||
19 | |||
20 | const START_HARDENED = 2147483648; // 2^31 |
||
21 | |||
22 | /** |
||
23 | * @param Math $math |
||
24 | */ |
||
25 | 78 | public function __construct(Math $math) |
|
29 | |||
30 | /** |
||
31 | * @return \BitWasp\Bitcoin\Math\BinaryMath |
||
32 | */ |
||
33 | 57 | private function binaryMath() |
|
37 | |||
38 | /** |
||
39 | * @param $sequence |
||
40 | * @return bool |
||
41 | */ |
||
42 | 57 | public function isHardened($sequence) |
|
46 | |||
47 | /** |
||
48 | * @param $sequence |
||
49 | * @return int|string |
||
50 | */ |
||
51 | 42 | public function getHardened($sequence) |
|
52 | { |
||
53 | 42 | if ($this->isHardened($sequence)) { |
|
54 | 3 | throw new \LogicException('Sequence is already for a hardened key'); |
|
55 | } |
||
56 | |||
57 | 39 | $prime = $this->binaryMath()->makeNegative(gmp_init($sequence, 10), 32); |
|
58 | 39 | return gmp_strval($prime, 10); |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Convert a human readable path node (eg, "0", "0'", or "0h") into the correct sequence (0, 0x80000000, 0x80000000) |
||
63 | * |
||
64 | * @param $node |
||
65 | * @return int|string |
||
66 | */ |
||
67 | 48 | public function fromNode($node) |
|
82 | |||
83 | /** |
||
84 | * Given a sequence, get the human readable node. Ie, 0 -> 0, 0x80000000 -> 0h |
||
85 | * |
||
86 | * @param $sequence |
||
87 | * @return string |
||
88 | */ |
||
89 | 30 | public function getNode($sequence) |
|
98 | |||
99 | /** |
||
100 | * Decodes a human-readable path, into an array of integers (sequences) |
||
101 | * |
||
102 | * @param string $path |
||
103 | * @return array |
||
104 | */ |
||
105 | 36 | public function decodePath($path) |
|
120 | |||
121 | /** |
||
122 | * Encodes a list of sequences to the human-readable path. |
||
123 | * |
||
124 | * @param array|\stdClass|\Traversable $list |
||
125 | * @return string |
||
126 | */ |
||
127 | 9 | public function encodePath($list) |
|
138 | |||
139 | /** |
||
140 | * Check the list, mainly that it works for foreach() |
||
141 | * |
||
142 | * @param \stdClass|array|\Traversable $list |
||
143 | */ |
||
144 | 15 | public static function validateListType($list) |
|
151 | } |
||
152 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.