Completed
Pull Request — master (#68)
by Daniel
02:25
created

DateFilter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 40.35 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 7
dl 23
loc 57
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 0 7 1
A getExpression() 10 10 2
A configureOptions() 0 12 1
A getComparatorMap() 13 13 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
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Filter;
6
7
use Psi\Component\Grid\FilterDataInterface;
8
use Psi\Component\Grid\FilterInterface;
9
use Psi\Component\ObjectAgent\Query\Comparison;
10
use Psi\Component\ObjectAgent\Query\Expression;
11
use Psi\Component\ObjectAgent\Query\Query;
12
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
13
use Symfony\Component\Form\FormBuilderInterface;
14
use Symfony\Component\Form\FormInterface;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
17
use Psi\Component\Grid\Filter\BooleanFilterData;
18
use Symfony\Component\Form\Extension\Core\Type\DateType;
19
use Psi\Component\Grid\Filter\DateFilterData;
20
21
class DateFilter extends AbstractComparatorFilter
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function buildForm(FormBuilderInterface $builder, array $options)
27
    {
28
        $this->addComparatorChoice($builder, $options);
29
30
        $builder->add('apply', CheckboxType::class, []);
31
        $builder->add('value', DateType::class, []);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 View Code Duplication
    public function getExpression(string $fieldName, FilterDataInterface $data): Expression
0 ignored issues
show
Duplication introduced by
This method 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...
38
    {
39
        $comparator = $data->getComparator() ?: self::TYPE_EQUAL;
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psi\Component\Grid\FilterDataInterface as the method getComparator() does only exist in the following implementations of said interface: Psi\Component\Grid\Filter\DateFilterData, Psi\Component\Grid\Filter\NumberFilterData, Psi\Component\Grid\Filter\StringFilterData.

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...
40
41
        return Query::comparison(
42
            $comparator,
43
            $fieldName,
44
            $data->getValue()
45
        );
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function configureOptions(OptionsResolver $options)
52
    {
53
        $options->setDefault('comparators', $this->getComparatorMap());
54
        $options->setDefault('data_class', DateFilterData::class);
55
        $options->setDefault('empty_data', function (FormInterface $form) {
56
            return new DateFilterData(
57
                $form->get('apply')->getData(),
58
                $form->get('comparator')->getData(),
59
                $form->get('value')->getData()
60
            );
61
        });
62
    }
63
64 View Code Duplication
    protected function getComparatorMap(): array
0 ignored issues
show
Duplication introduced by
This method 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...
65
    {
66
        $supported = [
67
            Comparison::EQUALS,
68
            Comparison::NOT_EQUALS,
69
            Comparison::GREATER_THAN,
70
            Comparison::GREATER_THAN_EQUAL,
71
            Comparison::LESS_THAN_EQUAL,
72
            Comparison::LESS_THAN,
73
        ];
74
75
        return array_combine($supported, $supported);
76
    }
77
}
78
79