Completed
Push — master ( 31165a...a6c946 )
by Dmytro
04:37
created

CriteriaHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 43
loc 43
ccs 8
cts 11
cp 0.7272
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A supports() 4 4 2
A handleSorting() 4 4 1
A handleFiltering() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types=1);
3
4
namespace Paymaxi\Component\Query\Handler;
5
6
use Doctrine\Common\Collections\Criteria;
7
use Paymaxi\Component\Query\Filter\CriteriaFilterInterface;
8
use Paymaxi\Component\Query\Filter\FilterInterface;
9
use Paymaxi\Component\Query\Sort\CriteriaSortInterface;
10
use Paymaxi\Component\Query\Sort\SortInterface;
11
12
/**
13
 * Class CriteriaHandler
14
 *
15
 * @package Paymaxi\Component\Query\Handler
16
 */
17 View Code Duplication
final class CriteriaHandler extends AbstractHandler
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
{
19
    /** @var Criteria */
20
    private $criteria;
21
22
    /**
23
     * CriteriaHandler constructor.
24
     *
25
     * @param Criteria $criteria
26
     */
27 4
    public function __construct(Criteria $criteria)
28
    {
29 4
        $this->criteria = $criteria;
30 4
    }
31
32
    /**
33
     * @param object $object
34
     *
35
     * @return bool
36
     */
37 4
    public function supports($object): bool
38
    {
39 4
        return $object instanceof CriteriaSortInterface || $object instanceof CriteriaFilterInterface;
40
    }
41
42
    /**
43
     * @param CriteriaSortInterface|SortInterface $sort
44
     * @param string $order
45
     */
46
    protected function handleSorting(SortInterface $sort, string $order):void
47
    {
48
        $sort->apply($this->criteria, $order);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Paymaxi\Component\Query\Sort\SortInterface as the method apply() does only exist in the following implementations of said interface: Paymaxi\Component\Query\Sort\DynamicSorting, Paymaxi\Component\Query\Sort\Sorting, Paymaxi\Component\Query\Sort\StaticSorting.

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
    }
50
51
    /**
52
     * @param FilterInterface|CriteriaFilterInterface $filter
53
     * @param mixed $value
54
     */
55 3
    protected function handleFiltering(FilterInterface $filter, $value):void
56
    {
57 3
        $filter->apply($this->criteria, $value);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Paymaxi\Component\Query\Filter\FilterInterface as the method apply() does only exist in the following implementations of said interface: Paymaxi\Component\Query\Filter\BooleanFilter, Paymaxi\Component\Query\Filter\DefaultFilter, Paymaxi\Component\Query\...ynamicEnumerationFilter, Paymaxi\Component\Query\Filter\DynamicFilter, Paymaxi\Component\Query\Filter\EnumerationFilter, Paymaxi\Component\Query\Filter\OperatorFilter, Paymaxi\Component\Query\Filter\ScalarFilter.

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...
58 3
    }
59
}
60