1 | <?php |
||
17 | abstract class AbstractFormatterProvider implements FormatterProvider |
||
18 | { |
||
19 | protected $defaultFormatter; |
||
20 | /** |
||
21 | * method prefix |
||
22 | */ |
||
23 | const METHOD_PATTERN_MATCH = '/^as([A-Z]\w+)$/'; |
||
24 | |||
25 | public $nullValue = '<span>Not Set</span>'; |
||
26 | |||
27 | /** |
||
28 | * Provide a list of formatters this that are available from this provider |
||
29 | * |
||
30 | * @return array |
||
31 | */ |
||
32 | public function formats() |
||
33 | { |
||
34 | // get a list of all public non-static methods that start with |
||
35 | // METHOD_PREFIX |
||
36 | $reflect = new ReflectionClass($this); |
||
37 | $formats = []; |
||
38 | foreach ($reflect->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
||
39 | $isFormatter = |
||
40 | !$method->isStatic() && |
||
41 | preg_match(self::METHOD_PATTERN_MATCH, $method->getName(), $match); |
||
42 | if (!$isFormatter || !$match) { |
||
43 | continue; |
||
44 | } |
||
45 | $formats[] = strtolower($match[1]); |
||
46 | } |
||
47 | |||
48 | return $formats; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Combine the value & params |
||
53 | * @param mixed $value the value (first argument of the format method) |
||
54 | * @param string|array $format the name of the formatter, or an array of |
||
55 | * the formatter and its addtional params |
||
56 | * @return array of two elements, format and the params for the formatter |
||
57 | * method |
||
58 | * @throws InvalidArgumentException if the format is incorrect |
||
59 | */ |
||
60 | protected function extractFormatAndParams($value, $format) |
||
77 | |||
78 | /** |
||
79 | * Format the supplied value, based on the desired format + configuration |
||
80 | * |
||
81 | * @param mixed $value The value to format |
||
82 | * @param string|array|null $format either the formatter name, or the formatter |
||
83 | * config as an array. If it's an array, the |
||
84 | * first item must be the same of the formatter |
||
85 | * @return mixed |
||
86 | * @throws InvalidArgumentException if the format is incorrect |
||
87 | */ |
||
88 | public function format($value, $format = null) |
||
106 | |||
107 | /** |
||
108 | * Check if the requested format exists in this provider |
||
109 | * |
||
110 | * @param string $format The formatter name you want to check for |
||
111 | * @return boolean |
||
112 | */ |
||
113 | public function hasFormat($format) |
||
124 | } |
||
125 |
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: