Test Failed
Push — master ( b17eba...69b436 )
by Kirill
06:37
created

RelationBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 48
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 18 3
A join() 0 10 1
1
<?php
2
/**
3
 * This file is part of Hydrogen package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace RDS\Hydrogen\Processor\DatabaseProcessor;
11
12
use Doctrine\ORM\QueryBuilder;
13
use RDS\Hydrogen\Criteria\Common\Field;
14
use RDS\Hydrogen\Criteria\CriterionInterface;
15
use RDS\Hydrogen\Criteria\Relation;
16
17
/**
18
 * Class RelationBuilder
19
 */
20
class RelationBuilder extends WhereBuilder
21
{
22
    /**
23
     * @var array
24
     */
25
    private $relations = [];
26
27
    /**
28
     * @param QueryBuilder $builder
29
     * @param CriterionInterface|Relation $relation
30
     * @return \Generator
31
     */
32
    public function apply($builder, CriterionInterface $relation): \Generator
33
    {
34
        $field = $relation->getField();
35
        $alias = $relation->getAlias();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface RDS\Hydrogen\Criteria\CriterionInterface as the method getAlias() does only exist in the following implementations of said interface: RDS\Hydrogen\Criteria\Relation, RDS\Hydrogen\Criteria\Selection.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
36
37
        foreach ($field->getChunks() as $chunk) {
38
            if (isset($this->relations[$chunk])) {
39
                $alias = $this->relations[$chunk];
40
                continue;
41
            }
42
43
            $alias = $this->join($builder, $chunk, $alias);
44
45
            $this->relations[$chunk] = $alias;
46
        }
47
48
        yield $relation->getQuery()->withAlias($alias);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface RDS\Hydrogen\Criteria\CriterionInterface as the method getQuery() does only exist in the following implementations of said interface: RDS\Hydrogen\Criteria\Group, RDS\Hydrogen\Criteria\Relation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
49
    }
50
51
    /**
52
     * @param QueryBuilder $builder
53
     * @param string $child
54
     * @param string $parent
55
     * @return string
56
     */
57
    private function join(QueryBuilder $builder, string $child, string $parent): string
58
    {
59
        $relation = Field::new($child)->withAlias($parent);
60
        $alias = \str_replace('.', '_', $relation->toString());
61
62
        $builder->leftJoin($relation->toString(), $alias);
63
        $builder->addSelect($alias);
64
65
        return $alias;
66
    }
67
}
68