Completed
Push — master ( 02db1d...96e327 )
by Daniel
02:23 queued 15s
created

DateFilter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 56
loc 56
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 6 6 1
A getExpression() 10 10 2
A isApplicable() 4 4 1
A configureOptions() 4 4 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\ObjectAgent\Query\Comparison;
8
use Psi\Component\ObjectAgent\Query\Expression;
9
use Psi\Component\ObjectAgent\Query\Query;
10
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
11
use Symfony\Component\Form\Extension\Core\Type\DateType;
12
use Symfony\Component\Form\FormBuilderInterface;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15 View Code Duplication
class DateFilter extends AbstractComparatorFilter
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...
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function buildForm(FormBuilderInterface $builder, array $options)
21
    {
22
        $builder->add('apply', CheckboxType::class, []);
23
        $this->addComparatorChoice($builder, $options);
24
        $builder->add('value', DateType::class, []);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getExpression(string $fieldName, array $data): Expression
31
    {
32
        $comparator = $data['comparator'] ?: Comparison::EQUALS;
33
34
        return Query::comparison(
35
            $comparator,
36
            $fieldName,
37
            $data['value']
38
        );
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function isApplicable(array $filterData): bool
45
    {
46
        return (bool) $filterData['apply'];
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function configureOptions(OptionsResolver $options)
53
    {
54
        $options->setDefault('comparators', $this->getComparatorMap());
55
    }
56
57
    protected function getComparatorMap(): array
58
    {
59
        $supported = [
60
            Comparison::EQUALS,
61
            Comparison::NOT_EQUALS,
62
            Comparison::GREATER_THAN,
63
            Comparison::GREATER_THAN_EQUAL,
64
            Comparison::LESS_THAN_EQUAL,
65
            Comparison::LESS_THAN,
66
        ];
67
68
        return array_combine($supported, $supported);
69
    }
70
}
71