1 | <?php |
||
17 | class Keys |
||
18 | { |
||
19 | /** |
||
20 | * Build 44 digits keys to NFe, NFCe, CTe and MDFe |
||
21 | * @param string $cUF UF number |
||
22 | * @param string $ano year |
||
23 | * @param string $mes month |
||
24 | * @param string $cnpj or CPF |
||
25 | * @param string $mod model of document 55, 65, 57 etc |
||
26 | * @param string $serie |
||
27 | * @param string $numero document number |
||
28 | * @param string $tpEmis emission type |
||
29 | * @param string $codigo random number or document number |
||
30 | * @return string |
||
31 | */ |
||
32 | 2 | public static function build( |
|
33 | $cUF, |
||
34 | $ano, |
||
35 | $mes, |
||
36 | $cnpj, |
||
37 | $mod, |
||
38 | $serie, |
||
39 | $numero, |
||
40 | $tpEmis, |
||
41 | $codigo = null |
||
42 | ) { |
||
43 | 2 | if (empty($codigo)) { |
|
44 | $codigo = self::random(); |
||
45 | } |
||
46 | //if a cpf with 11 digits is passed then complete with leading zeros |
||
47 | //up to the 14 digits |
||
48 | 2 | if (strlen($cnpj) < 14) { |
|
49 | 1 | $cnpj = str_pad($cnpj, 14, '0', STR_PAD_LEFT); |
|
50 | } |
||
51 | 2 | $format = "%02d%02d%02d%s%02d%03d%09d%01d%08d"; |
|
52 | 2 | $key = sprintf( |
|
53 | 2 | $format, |
|
54 | 2 | $cUF, |
|
55 | 2 | $ano, |
|
56 | 2 | $mes, |
|
57 | 2 | $cnpj, |
|
58 | 2 | $mod, |
|
59 | 2 | $serie, |
|
60 | 2 | $numero, |
|
61 | 2 | $tpEmis, |
|
62 | 2 | $codigo |
|
63 | ); |
||
64 | 2 | return $key . self::verifyingDigit($key); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Verifies that the key provided is valid |
||
69 | * @param string $key |
||
70 | * @return boolean |
||
71 | */ |
||
72 | 2 | public static function isValid($key) |
|
84 | |||
85 | /** |
||
86 | * This method calculates verifying digit |
||
87 | * @param string $key |
||
88 | * @return string |
||
89 | */ |
||
90 | 6 | public static function verifyingDigit($key) |
|
110 | |||
111 | /** |
||
112 | * Generate and return a 8 digits random number |
||
113 | * for cNF tag |
||
114 | * @param string|null $nnf |
||
115 | * @return string |
||
116 | */ |
||
117 | public static function random($nnf = null) |
||
131 | |||
132 | /** |
||
133 | * Verify if cNF number is valid NT2019.001 |
||
134 | * @param string $cnf |
||
135 | */ |
||
136 | public static function cNFIsValid($cnf) |
||
146 | } |
||
147 |
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: