Passed
Push — master ( 8e04a1...918acc )
by Gerrit
02:12
created

createArgumentFromString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\Arguments\ArgumentFactory;
14
15
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory;
16
use Addiks\SymfonyGenerics\Arguments\Argument;
17
use Addiks\SymfonyGenerics\Arguments\RequestFileArgument;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
use Webmozart\Assert\Assert;
20
21
final class RequestFileArgumentFactory implements ArgumentFactory
22
{
23
24
    /**
25
     * @var RequestStack
26
     */
27
    private $requestStack;
28
29 25
    public function __construct(RequestStack $requestStack)
30
    {
31 25
        $this->requestStack = $requestStack;
32 25
    }
33
34 6
    public function understandsString(string $source): bool
35
    {
36 6
        return strpos($source, '$files.') === 0 && strlen($source) > 7;
37
    }
38
39 7
    public function understandsArray(array $source): bool
40
    {
41 7
        return isset($source['key']) && isset($source['type']) && $source['type'] === 'request-file';
42
    }
43
44 6
    public function createArgumentFromString(string $source): Argument
45
    {
46 6
        Assert::startsWith($source, '$files.');
47
48 4
        if (substr_count($source, '.') > 1) {
49 2
            [, $key, $property] = explode(".", $source);
0 ignored issues
show
Bug introduced by
The variable $key does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $property seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
50
51
        } else {
52 2
            $property = 'content';
53 2
            [, $key] = explode(".", $source);
54
        }
55
56 4
        return new RequestFileArgument($this->requestStack, $key, $property);
0 ignored issues
show
Bug introduced by
The variable $property 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...
57
    }
58
59 6
    public function createArgumentFromArray(array $source): Argument
60
    {
61 6
        Assert::keyExists($source, 'key');
62
63
        /** @var string $property */
64 4
        $property = 'content';
65
66 4
        if (isset($source['property'])) {
67 2
            $property = $source['property'];
68
        }
69
70 4
        return new RequestFileArgument($this->requestStack, $source['key'], $property);
71
    }
72
73
}
74