Completed
Pull Request — master (#5)
by Anton
03:00
created

OrderByProcessor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 47
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 4 1
A field() 0 15 3
A createAlias() 0 4 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\OrderBy;
16
17
/**
18
 * Class OrderByProcessor
19
 */
20
class OrderByProcessor extends CriterionProcessor
21
{
22
    /**
23
     * @var int
24
     */
25
    private static $relationId = 0;
26
27
    /**
28
     * @param QueryBuilder $builder
29
     * @param CriterionInterface|OrderBy $orderBy
30
     * @return QueryBuilder
31
     */
32
    public function apply(QueryBuilder $builder, CriterionInterface $orderBy): QueryBuilder
33
    {
34
        return $builder->orderBy($this->field($orderBy->getField()), $orderBy->getDirection());
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 getField() does only exist in the following implementations of said interface: RDS\Hydrogen\Criteria\GroupBy, RDS\Hydrogen\Criteria\OrderBy, RDS\Hydrogen\Criteria\Selection, RDS\Hydrogen\Criteria\Where.

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...
Bug introduced by
It seems like you code against a concrete implementation and not the interface RDS\Hydrogen\Criteria\CriterionInterface as the method getDirection() does only exist in the following implementations of said interface: RDS\Hydrogen\Criteria\OrderBy.

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...
35
    }
36
37
    /**
38
     * @param Field $field
39
     * @return string
40
     */
41
    protected function field(Field $field): string
42
    {
43
        $relations = explode('.', $field->getName());
44
        $fieldValue = array_pop($relations);
45
46
        foreach ($relations as $relation) {
47
            $alias = $this->createAlias($relation);
48
        }
49
50
        if (isset($alias)) {
51
            return $alias.'.'.$fieldValue;
52
        }
53
54
        return parent::field($field);
55
    }
56
57
58
    /**
59
     * @param string $class
60
     * @return string
61
     */
62
    private function createAlias(string $class): string
63
    {
64
        return 'ref_' . \snake_case(\class_basename($class)) . '_' . ++self::$relationId;
65
    }
66
}
67