1 | <?php |
||
12 | class RangesFromBoundaryCalculator |
||
13 | { |
||
14 | /** |
||
15 | * The BinaryMath instance to be used to perform bitwise poerations. |
||
16 | * |
||
17 | * @var \IPLib\Service\BinaryMath |
||
18 | */ |
||
19 | private $math; |
||
20 | |||
21 | /** |
||
22 | * The number of bits used to represent addresses. |
||
23 | * |
||
24 | * @var int |
||
25 | * |
||
26 | * @example 32 for IPv4, 128 for IPv6 |
||
27 | */ |
||
28 | private $numBits; |
||
29 | |||
30 | /** |
||
31 | * The bit masks for every bit index. |
||
32 | * |
||
33 | * @var string[] |
||
34 | */ |
||
35 | private $masks; |
||
36 | |||
37 | /** |
||
38 | * The bit unmasks for every bit index. |
||
39 | * |
||
40 | * @var string[] |
||
41 | */ |
||
42 | private $unmasks; |
||
43 | |||
44 | /** |
||
45 | * Initializes the instance. |
||
46 | * |
||
47 | * @param int $numBits the number of bits used to represent addresses (32 for IPv4, 128 for IPv6) |
||
48 | */ |
||
49 | 25 | public function __construct($numBits) |
|
54 | |||
55 | /** |
||
56 | * Calculate the subnets describing all (and only all) the addresses between two bouundaries. |
||
57 | * |
||
58 | * @param \IPLib\Address\AddressInterface $from |
||
59 | * @param \IPLib\Address\AddressInterface $to |
||
60 | * |
||
61 | * @return \IPLib\Range\Subnet[]|null return NULL if the two addresses have an invalid number of bits (that is, different from the one passed to the constructor of this class) |
||
62 | */ |
||
63 | 43 | public function getRanges(AddressInterface $from, AddressInterface $to) |
|
76 | |||
77 | /** |
||
78 | * Set the number of bits used to represent addresses (32 for IPv4, 128 for IPv6). |
||
79 | * |
||
80 | * @param int $numBits |
||
81 | */ |
||
82 | 25 | private function setNumBits($numBits) |
|
95 | |||
96 | /** |
||
97 | * Calculate the subnets. |
||
98 | * |
||
99 | * @param string $start the start address (represented in reduced bit form) |
||
100 | * @param string $end the end address (represented in reduced bit form) |
||
101 | * @param int $position the number of bits in the mask we are comparing at this cycle |
||
102 | * @param \IPLib\Range\Subnet[] $result found ranges will be added to this variable |
||
103 | */ |
||
104 | 37 | private function calculate($start, $end, $position, array &$result) |
|
128 | |||
129 | /** |
||
130 | * Create an address instance starting from its bits. |
||
131 | * |
||
132 | * @param string $bits the bits of the address (represented in reduced bit form) |
||
133 | * |
||
134 | * @return \IPLib\Address\AddressInterface |
||
135 | */ |
||
136 | 37 | private function addressFromBits($bits) |
|
146 | |||
147 | /** |
||
148 | * Create an range instance starting from the bits if the address and the length of the network prefix. |
||
149 | * |
||
150 | * @param string $bits the bits of the address (represented in reduced bit form) |
||
151 | * @param int $networkPrefix the length of the network prefix |
||
152 | * |
||
153 | * @return \IPLib\Range\Subnet |
||
154 | */ |
||
155 | 37 | private function subnetFromBits($bits, $networkPrefix) |
|
161 | } |
||
162 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: