Test Failed
Push — master ( e630f8...594d62 )
by Kirill
06:52
created

RelationProcessor::fetchAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
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\CriterionInterface;
14
use RDS\Hydrogen\Criteria\Relation;
15
16
/**
17
 * Class RelationProcessor
18
 */
19
class RelationProcessor extends CriterionProcessor
20
{
21
    /**
22
     * @var int
23
     */
24
    private static $relationId = 0;
25
26
    /**
27
     * @var array|string[]
28
     */
29
    private $relations = [];
30
31
    /**
32
     * @param QueryBuilder $builder
33
     * @param CriterionInterface|Relation $with
34
     * @return QueryBuilder
35
     */
36
    public function apply(QueryBuilder $builder, CriterionInterface $with): QueryBuilder
37
    {
38
        $alias = $this->alias;
39
40
        foreach ($with->getRelation()->split() as $relation) {
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 getRelation() does only exist in the following implementations of said interface: 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...
41
            $parent     = $relation->withAlias($alias);
42
            $exists     = $this->hasAlias($parent);
43
            $childAlias = $this->fetchAlias($parent, $relation->getName());
44
45
            if (! $exists) {
46
                $builder->leftJoin($parent, $childAlias);
47
                $builder->addSelect($childAlias);
48
            }
49
50
            $alias = $childAlias;
51
        }
52
53
        $this->processor->apply($builder, $with->getQuery(), $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...
54
55
        return $builder;
56
    }
57
58
    /**
59
     * @param string $parent
60
     * @return bool
61
     */
62
    private function hasAlias(string $parent): bool
63
    {
64
        return \array_key_exists($parent, $this->relations);
65
    }
66
67
    /**
68
     * @param string $parent
69
     * @param string $class
70
     * @return string
71
     */
72
    private function fetchAlias(string $parent, string $class): string
73
    {
74
        if (! $this->hasAlias($parent)) {
75
            $this->relations[$parent] = $this->createAlias($class);
76
        }
77
78
        return $this->relations[$parent];
79
    }
80
81
    /**
82
     * @param string $class
83
     * @return string
84
     */
85
    private function createAlias(string $class): string
86
    {
87
        return 'ref_' . \snake_case(\class_basename($class)) . '_' . ++self::$relationId;
88
    }
89
}
90