Passed
Push — master ( c2000a...f4524e )
by Marc
02:29
created

Loader::load()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5.0031

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
ccs 19
cts 20
cp 0.95
rs 8.439
cc 5
eloc 19
nc 5
nop 1
crap 5.0031
1
<?php
2
/*
3
 * This file is part of the phpflo\phpflo-fbp package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PhpFlo\Loader;
11
12
use PhpFlo\Common\DefinitionInterface;
13
use PhpFlo\Common\LoaderInterface;
14
use PhpFlo\Exception\LoaderException;
15
use PhpFlo\Fbp\FbpDefinition;
16
use PhpFlo\Fbp\FbpParser;
17
use Symfony\Component\Yaml\Yaml;
18
19
/**
20
 * Class Loader
21
 *
22
 * @package PhpFlo\Loader
23
 * @author Marc Aschmann <[email protected]>
24
 */
25
final class Loader implements LoaderInterface
26
{
27
    private static $types = [
28
        'yml' => 'yaml',
29
        'yaml' => 'yaml',
30
        'json' => 'json',
31
        'fbp' => 'fbp',
32
    ];
33
34
    /**
35
     * @param string $file name/path of file to load
36
     * @return DefinitionInterface
37
     * @throws LoaderException
38
     */
39 4
    public static function load($file)
40
    {
41 4
        $type = self::$types[self::checkType($file)];
42 4
        $content = self::loadFile($file);
43
44 3
        if (empty($content)) {
45
            throw new LoaderException("Loader::load(): no data found in file!");
46
        }
47
48
        switch ($type) {
49 3
            case 'fbp':
50 1
                $loader = new FbpParser($content);
51 1
                $definition = $loader->run();
52 1
                break;
53 2
            case 'yaml':
54 1
                $definition = new FbpDefinition(
55 1
                    Yaml::parse($content)
56 1
                );
57 1
                break;
58 1
            case 'json':
59 1
                $definition = new FbpDefinition(
60 1
                    json_decode($content, true)
61 1
                );
62 1
                break;
63
        }
64
65 3
        return $definition;
0 ignored issues
show
Bug introduced by
The variable $definition 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...
66
    }
67
68
    /**
69
     * Check file if extension matches a loader.
70
     *
71
     * @param string $file
72
     * @return string
73
     */
74 4
    private static function checkType($file)
75
    {
76 4
        $parts = explode('.', $file);
77 4
        $type = array_pop($parts);
78
79 4
        if (!in_array($type, array_keys(self::$types))) {
80
            throw new LoaderException("Loader::checkType(): Could not find parser for {$file}!");
81
        }
82
83 4
        return $type;
84
    }
85
86
    /**
87
     * @param string $file
88
     * @return string
89
     */
90 4
    private static function loadFile($file)
91
    {
92 4
        if (file_exists($file) && is_readable($file)) {
93 3
            $content = file_get_contents($file);
94 3
        } else {
95 1
            throw new LoaderException(
96 1
                "Loader::loadFile(): {$file} does not exist!"
97 1
            );
98
        }
99
100 3
        return $content;
101
    }
102
}
103