Completed
Push — develop ( 95bfc8...365a9b )
by Arkadiusz
02:42
created

DataTransformer::predictions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\SupportVectorMachine;
6
7
class DataTransformer
8
{
9
    /**
10
     * @param array $samples
11
     * @param array $labels
12
     * @param bool  $targets
13
     *
14
     * @return string
15
     */
16
    public static function trainingSet(array $samples, array $labels, bool $targets = false): string
17
    {
18
        $set = '';
19
        if (!$targets) {
20
            $numericLabels = self::numericLabels($labels);
21
        }
22
23
        foreach ($labels as $index => $label) {
24
            $set .= sprintf('%s %s %s', ($targets ? $label : $numericLabels[$label]), self::sampleRow($samples[$index]), PHP_EOL);
0 ignored issues
show
Bug introduced by
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...
25
        }
26
27
        return $set;
28
    }
29
30
    /**
31
     * @param array $samples
32
     *
33
     * @return string
34
     */
35
    public static function testSet(array $samples): string
36
    {
37
        if (!is_array($samples[0])) {
38
            $samples = [$samples];
39
        }
40
41
        $set = '';
42
        foreach ($samples as $sample) {
43
            $set .= sprintf('0 %s %s', self::sampleRow($sample), PHP_EOL);
44
        }
45
46
        return $set;
47
    }
48
49
    /**
50
     * @param string $rawPredictions
51
     * @param array  $labels
52
     *
53
     * @return array
54
     */
55
    public static function predictions(string $rawPredictions, array $labels): array
56
    {
57
        $numericLabels = self::numericLabels($labels);
58
        $results = [];
59
        foreach (explode(PHP_EOL, $rawPredictions) as $result) {
60
            if (strlen($result) > 0) {
61
                $results[] = array_search($result, $numericLabels);
62
            }
63
        }
64
65
        return $results;
66
    }
67
68
    /**
69
     * @param array $labels
70
     *
71
     * @return array
72
     */
73
    public static function numericLabels(array $labels): array
74
    {
75
        $numericLabels = [];
76
        foreach ($labels as $label) {
77
            if (isset($numericLabels[$label])) {
78
                continue;
79
            }
80
81
            $numericLabels[$label] = count($numericLabels);
82
        }
83
84
        return $numericLabels;
85
    }
86
87
    /**
88
     * @param array $sample
89
     *
90
     * @return string
91
     */
92
    private static function sampleRow(array $sample): string
93
    {
94
        $row = [];
95
        foreach ($sample as $index => $feature) {
96
            $row[] = sprintf('%s:%s', $index + 1, $feature);
97
        }
98
99
        return implode(' ', $row);
100
    }
101
}
102