GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Pull Request — 2.2 (#165)
by Simone
03:19 queued 30s
created

Join::join()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 58
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 58
rs 8.4266
c 0
b 0
f 0
cc 7
nc 12
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Mado\QueryBundle\Queries;
4
5
use Doctrine\ORM\EntityManager;
6
use Mado\QueryBundle\Services\StringParser;
7
8
class Join
9
{
10
    private const AND_OPERATOR_LOGIC = 'AND';
11
12
    private const OR_OPERATOR_LOGIC = 'OR';
13
14
    private $parser;
15
16
    private $entityName;
17
18
    private $entityAlias;
19
20
    private $manager;
21
22
    private $innerJoin;
23
24
    private $leftJoin;
25
26
    private $joins;
27
28
    private $relationEntityAlias;
29
30
    public function __construct(string $entityName, string $entityAlias, EntityManager $manager)
31
    {
32
        $this->parser  = new StringParser();
33
        $this->entityName = $entityName;
34
        $this->entityAlias = $entityAlias;
35
        $this->manager = $manager;
36
        $this->innerJoin = [];
37
        $this->leftJoin = [];
38
    }
39
40
    /**
41
     * @param String $relation Nome della relazione semplice (groups.name) o con embedded (_embedded.groups.name)
42
     * @return $this
43
     */
44
    public function join(String $relation, $logicOperator = self::AND_OPERATOR_LOGIC)
45
    {
46
        $relation = explode('|', $relation)[0];
47
        $relations = [$relation];
48
49
        if (strstr($relation, '_embedded.')) {
50
            $embeddedFields = explode('.', $relation);
51
            $this->parser->camelize($embeddedFields[1]);
52
53
            // elimino l'ultimo elemento che dovrebbe essere il nome del campo
54
            unset($embeddedFields[count($embeddedFields) - 1]);
55
56
            // elimino il primo elemento _embedded
57
            unset($embeddedFields[0]);
58
59
            $relations = $embeddedFields;
60
        }
61
62
        $entityName = $this->entityName;
63
        $entityAlias = $this->entityAlias;
64
65
        foreach ($relations as $relation) {
66
            $relation = $this->parser->camelize($relation);
67
            $relationEntityAlias = 'table_' . $relation;
68
69
            $metadata = $this->manager->getClassMetadata($entityName);
70
71
            if ($metadata->hasAssociation($relation)) {
72
                $association = $metadata->getAssociationMapping($relation);
73
74
                $fieldName = $this->parser->camelize($association['fieldName']);
75
76
                if ($this->noExistsJoin($relationEntityAlias, $relation)) {
77
                    if ($logicOperator === self::AND_OPERATOR_LOGIC) {
78
                        $param = [];
79
                        $param['field'] = $entityAlias . "." . $fieldName;
80
                        $param['relation'] = $relationEntityAlias;
81
                        $this->innerJoin[] = $param;
82
                    } elseif ($logicOperator === self::OR_OPERATOR_LOGIC) {
83
                        $param = [];
84
                        $param['field'] = $entityAlias . "." . $fieldName;
85
                        $param['relation'] = $relationEntityAlias;
86
                        $this->leftJoin[] = $param;
87
                    } else {
88
                        throw new \Exception('Missing Logic operator');
89
                    }
90
91
                    $this->storeJoin($relationEntityAlias, $relation);
92
                }
93
94
                $entityName = $association['targetEntity'];
95
                $entityAlias = $relationEntityAlias;
96
            }
97
98
            $this->setRelationEntityAlias($relationEntityAlias);
99
        }
100
101
        return $this;
102
    }
103
104
    private function storeJoin($prevEntityAlias, $currentEntityAlias)
105
    {
106
        $needle = $prevEntityAlias . '_' . $currentEntityAlias;
107
        $this->joins[$needle] = $needle;
108
    }
109
110
    private function noExistsJoin($prevEntityAlias, $currentEntityAlias)
111
    {
112
        if (null === $this->joins) {
113
            $this->joins = [];
114
        }
115
116
        $needle = $prevEntityAlias . '_' . $currentEntityAlias;
117
118
        return !in_array($needle, $this->joins);
119
    }
120
121
    public function getInnerJoin() :array
122
    {
123
        return $this->innerJoin;
124
    }
125
126
    public function getLeftJoin() :array
127
    {
128
        return $this->leftJoin;
129
    }
130
131
    private function setRelationEntityAlias(string $relationEntityAlias)
132
    {
133
        $this->relationEntityAlias = $relationEntityAlias;
134
    }
135
136
    public function getRelationEntityAlias()
137
    {
138
        return $this->relationEntityAlias;
139
    }
140
}