Passed
Push — master ( fbbe5c...a34811 )
by Arkadiusz
07:00
created

src/Phpml/SupportVectorMachine/DataTransformer.php (1 issue)

Upgrade to new PHP Analysis Engine

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
The variable $numericLabels does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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