These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Phpml\SupportVectorMachine; |
||
6 | |||
7 | class DataTransformer |
||
8 | { |
||
9 | public static function trainingSet(array $samples, array $labels, bool $targets = false): string |
||
10 | { |
||
11 | $set = ''; |
||
12 | if (!$targets) { |
||
13 | $numericLabels = self::numericLabels($labels); |
||
14 | } |
||
15 | |||
16 | foreach ($labels as $index => $label) { |
||
17 | $set .= sprintf('%s %s %s', ($targets ? $label : $numericLabels[$label]), self::sampleRow($samples[$index]), PHP_EOL); |
||
0 ignored issues
–
show
|
|||
18 | } |
||
19 | |||
20 | return $set; |
||
21 | } |
||
22 | |||
23 | public static function testSet(array $samples): string |
||
24 | { |
||
25 | if (!is_array($samples[0])) { |
||
26 | $samples = [$samples]; |
||
27 | } |
||
28 | |||
29 | $set = ''; |
||
30 | foreach ($samples as $sample) { |
||
31 | $set .= sprintf('0 %s %s', self::sampleRow($sample), PHP_EOL); |
||
32 | } |
||
33 | |||
34 | return $set; |
||
35 | } |
||
36 | |||
37 | public static function predictions(string $rawPredictions, array $labels): array |
||
38 | { |
||
39 | $numericLabels = self::numericLabels($labels); |
||
40 | $results = []; |
||
41 | foreach (explode(PHP_EOL, $rawPredictions) as $result) { |
||
42 | if (isset($result[0])) { |
||
43 | $results[] = array_search($result, $numericLabels); |
||
44 | } |
||
45 | } |
||
46 | |||
47 | return $results; |
||
48 | } |
||
49 | |||
50 | public static function numericLabels(array $labels): array |
||
51 | { |
||
52 | $numericLabels = []; |
||
53 | foreach ($labels as $label) { |
||
54 | if (isset($numericLabels[$label])) { |
||
55 | continue; |
||
56 | } |
||
57 | |||
58 | $numericLabels[$label] = count($numericLabels); |
||
59 | } |
||
60 | |||
61 | return $numericLabels; |
||
62 | } |
||
63 | |||
64 | private static function sampleRow(array $sample): string |
||
65 | { |
||
66 | $row = []; |
||
67 | foreach ($sample as $index => $feature) { |
||
68 | $row[] = sprintf('%s:%s', $index + 1, $feature); |
||
69 | } |
||
70 | |||
71 | return implode(' ', $row); |
||
72 | } |
||
73 | } |
||
74 |
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: