Passed
Push — master ( c80e12...1c5165 )
by Kirill
04:32
created

JoinBuilder::join()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.024

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 9
cts 15
cp 0.6
rs 9.504
c 0
b 0
f 0
cc 4
nc 4
nop 4
crap 5.024
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\Join;
16
17
/**
18
 * Class JoinBuilder
19
 */
20
class JoinBuilder extends WhereBuilder
21
{
22
    /**
23
     * @var array
24
     */
25
    private $relations = [];
26
27
    /**
28
     * @param QueryBuilder $builder
29
     * @param CriterionInterface|Join $relation
30
     * @return \Generator
31
     */
32 2
    public function apply($builder, CriterionInterface $relation): \Generator
33
    {
34 2
        $field = $relation->getField();
35 2
        $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\Join, 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 2
        foreach ($field->getChunks() as $chunk) {
38 2
            if (isset($this->relations[$chunk])) {
39
                $alias = $this->relations[$chunk];
40
                continue;
41
            }
42
43 2
            $alias = $this->join($relation->getType(), $builder, $chunk, $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 getType() does only exist in the following implementations of said interface: RDS\Hydrogen\Criteria\Join.

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...
44
45 2
            $this->relations[$chunk] = $alias;
46
        }
47
48 2
        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\Join, 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 2
    }
50
51
    /**
52
     * @param int $type
53
     * @param QueryBuilder $builder
54
     * @param string $child
55
     * @param string $parent
56
     * @return string
57
     */
58 2
    private function join(int $type, QueryBuilder $builder, string $child, string $parent): string
59
    {
60 2
        $relation = Field::new($child)->withAlias($parent);
61 2
        $alias = \str_replace('.', '_', $relation->toString());
62
63
        switch ($type) {
64 2
            case Join::TYPE_JOIN:
65
                $builder->join($relation->toString(), $alias);
66
                break;
67
68 2
            case Join::TYPE_LEFT_JOIN:
69 2
                $builder->leftJoin($relation->toString(), $alias);
70 2
                break;
71
72
            case Join::TYPE_INNER_JOIN:
73
                $builder->innerJoin($relation->toString(), $alias);
74
                break;
75
76
            default:
77
                throw new \InvalidArgumentException('Unexpected join type');
78
        }
79
80 2
        $builder->addSelect($alias);
81
82 2
        return $alias;
83
    }
84
}
85