SortProcessor::process()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
3
namespace ViewComponents\Doctrine\Processor;
4
5
use Doctrine\DBAL\Query\QueryBuilder;
6
use ViewComponents\ViewComponents\Data\Operation\OperationInterface;
7
use ViewComponents\ViewComponents\Data\Operation\SortOperation;
8
use ViewComponents\ViewComponents\Data\Processor\ProcessorInterface;
9
10
/**
11
 * SortOperation processing for DoctrineDataProvider.
12
 *
13
 * @see SortOperation
14
 */
15
class SortProcessor implements ProcessorInterface
16
{
17
    /**
18
     * Applies operation to data source and returns modified data source.
19
     *
20
     * @param QueryBuilder $src
21
     * @param OperationInterface|SortOperation $operation
22
     * @return QueryBuilder
23
     */
24
    public function process($src, OperationInterface $operation)
25
    {
26
        $field = $operation->getField();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface ViewComponents\ViewCompo...tion\OperationInterface as the method getField() does only exist in the following implementations of said interface: ViewComponents\ViewCompo...eration\FilterOperation, ViewComponents\ViewCompo...Operation\SortOperation.

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...
27
        $order = $operation->getOrder();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface ViewComponents\ViewCompo...tion\OperationInterface as the method getOrder() does only exist in the following implementations of said interface: ViewComponents\ViewCompo...Operation\SortOperation.

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...
28
        $src->orderBy($field, $order);
29
        return $src;
30
    }
31
}
32