Completed
Push — master ( c4984f...4e54fe )
by Vitaliy
02:58
created

FilterProcessor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 0
cbo 1
dl 0
loc 30
ccs 6
cts 6
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 22 4
1
<?php
2
3
namespace ViewComponents\Eloquent\Processor;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use ViewComponents\ViewComponents\Data\Operation\FilterOperation;
7
use ViewComponents\ViewComponents\Data\Operation\OperationInterface;
8
use ViewComponents\ViewComponents\Data\Processor\ProcessorInterface;
9
10
class FilterProcessor implements ProcessorInterface
11
{
12
    /**
13
     * @param Builder $src
14
     * @param OperationInterface|FilterOperation $operation
15
     * @return mixed
16
     */
17 13
    public function process($src, OperationInterface $operation)
18
    {
19 13
        $value = $operation->getValue();
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 getValue() does only exist in the following implementations of said interface: ViewComponents\ViewCompo...eration\FilterOperation.

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...
20 13
        $operator = $operation->getOperator();
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 getOperator() does only exist in the following implementations of said interface: ViewComponents\ViewCompo...eration\FilterOperation.

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...
21 13
        $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...
22 13
        switch ($operator) {
23 13
            case FilterOperation::OPERATOR_STR_STARTS_WITH:
24
                $operator = FilterOperation::OPERATOR_LIKE;
25
                $value .= '%';
26
                break;
27
            case FilterOperation::OPERATOR_STR_ENDS_WITH:
28
                $operator = FilterOperation::OPERATOR_LIKE;
29
                $value = '%' . $value;
30
                break;
31
            case FilterOperation::OPERATOR_STR_CONTAINS:
32
                $operator = FilterOperation::OPERATOR_LIKE;
33
                $value = '%' . $value . '%';
34
                break;
35
        }
36
        $src->where($field, $operator, $value);
37
        return $src;
38
    }
39
}
40