Passed
Push — master ( 289469...3cf8a9 )
by Gerrit
03:42
created

EntityArgument   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 57.89%

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 11
cts 19
cp 0.5789
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A resolve() 0 21 3
A repository() 0 8 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;
14
15
use Addiks\SymfonyGenerics\Arguments\Argument;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Doctrine\Common\Persistence\ObjectRepository;
18
19
final class EntityArgument implements Argument
20
{
21
22
    /**
23
     * @var string
24
     */
25
    private $entityClass;
26
27
    /**
28
     * @var Argument
29
     */
30
    private $id;
31
32
    /**
33
     * @var ObjectManager
34
     */
35
    private $objectManager;
36
37
    /**
38
     * @var ObjectRepository|null
39
     */
40
    private $repository;
41
42
    /** @var array<string, mixed> */
43
    private static $constantMap = array(
44
        'true' => true,
45
        'false' => false,
46
        'null' => null,
47
    );
48
49 3
    public function __construct(
50
        ObjectManager $objectManager,
51
        string $entityClass,
52
        Argument $id
53
    ) {
54 3
        $this->entityClass = $entityClass;
55 3
        $this->id = $id;
56 3
        $this->objectManager = $objectManager;
57 3
    }
58
59 1
    public function resolve()
60
    {
61
        /** @var string $entityId */
62 1
        $entityId = $this->id->resolve();
63
64 1
        if (preg_match("/^\[([a-zA-Z0-9_-]+)\=(.*)\]$/is", $entityId, $matches)) {
65
            [, $column, $value] = $matches;
0 ignored issues
show
Bug introduced by
The variable $column 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 $value 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...
66
67
            if (isset(self::$constantMap[strtolower($value)])) {
0 ignored issues
show
Bug introduced by
The variable $value 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...
68
                $value = self::$constantMap[strtolower($value)];
0 ignored issues
show
Bug introduced by
The variable $value 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...
69
            }
70
71
            return $this->repository()->findOneBy([$column => $value]);
0 ignored issues
show
Bug introduced by
The variable $value 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...
72
73
        } else {
74 1
            return $this->objectManager->find(
75 1
                $this->entityClass,
76 1
                $entityId
77
            );
78
        }
79
    }
80
81
    private function repository(): ObjectRepository
82
    {
83
        if (is_null($this->repository)) {
84
            $this->repository = $this->objectManager->getRepository($this->entityClass);
85
        }
86
87
        return $this->repository;
88
    }
89
90
}
91