Completed
Push — master ( 483143...6b44c3 )
by Guillaume
05:17
created

UrlProvider::getUrls()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 28
rs 8.8571
cc 2
eloc 19
nc 2
nop 0
1
<?php
2
3
namespace Hogosha\Monitor\Model;
4
5
use Hogosha\Monitor\Configuration\ConfigurationLoader;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
/**
9
 * @author Guillaume Cavana <[email protected]>
10
 */
11
class UrlProvider
12
{
13
    /**
14
     * $configurationLoader.
15
     *
16
     * @var ConfigurationLoader
17
     */
18
    protected $configurationLoader;
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param ConfigurationLoader $configurationLoader
24
     */
25
    public function __construct(ConfigurationLoader $configurationLoader)
26
    {
27
        $this->configurationLoader = $configurationLoader;
28
    }
29
30
    /**
31
     * getUrls.
32
     *
33
     * @return array
34
     */
35
    public function getUrls()
36
    {
37
        $resolver = new OptionsResolver();
38
        $resolver->setRequired(
39
            [
40
                'url',
41
                'method',
42
                'headers',
43
                'timeout',
44
                'status_code',
45
            ]
46
        );
47
48
        $config = $this->configurationLoader->loadConfiguration();
49
        foreach ($config['urls'] as $name => $attribute) {
50
            $attribute = $resolver->resolve($attribute);
51
            $urls[$name] = new UrlInfo(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$urls was never initialized. Although not strictly required by PHP, it is generally a good practice to add $urls = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
52
                $name,
53
                $attribute['url'],
54
                $attribute['method'],
55
                $attribute['headers'],
56
                $attribute['timeout'],
57
                $attribute['status_code']
58
            );
59
        }
60
61
        return $urls;
0 ignored issues
show
Bug introduced by
The variable $urls 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...
62
    }
63
}
64