Completed
Push — feature/EVO-5751-text-index-mo... ( 0ab3ac...8fecb1 )
by
unknown
62:16 queued 57:01
created

RangeConstraintBuilder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 9.23 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 12
c 2
b 0
f 1
lcom 1
cbo 1
dl 6
loc 65
rs 10
ccs 0
cts 21
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsConstraint() 0 9 2
B buildConstraint() 6 19 10

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
 * RangeConstraintBuilder class file
4
 */
5
6
namespace Graviton\SchemaBundle\Constraint\Builder;
7
8
use Graviton\RestBundle\Model\DocumentModel;
9
use Graviton\SchemaBundle\Document\Schema;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class RangeConstraintBuilder implements ConstraintBuilderInterface
17
{
18
19
    /**
20
     * @var array
21
     */
22
    private $types = [
23
        'Range',
24
        'GreaterThanOrEqual',
25
        'LessThanOrEqual'
26
    ];
27
28
    /**
29
     * @var string
30
     */
31
    private $type;
32
    
33
    /**
34
     * if this builder supports a given constraint
35
     *
36
     * @param string $type    Field type
37
     * @param array  $options Options
38
     *
39
     * @return bool
40
     */
41
    public function supportsConstraint($type, array $options = [])
42
    {
43
        if (in_array($type, $this->types)) {
44
            $this->type = $type;
45
            return true;
46
        }
47
48
        return false;
49
    }
50
51
    /**
52
     * Adds constraints to the property
53
     *
54
     * @param string        $fieldName field name
55
     * @param Schema        $property  property
56
     * @param DocumentModel $model     parent model
57
     * @param array         $options   the constraint options
58
     *
59
     * @return Schema the modified property
60
     */
61
    public function buildConstraint($fieldName, Schema $property, DocumentModel $model, array $options)
62
    {
63
        foreach ($options as $option) {
64
            if ($option->name == 'min' && $this->type == 'Range') {
65
                $property->setNumericMinimum(floatval($option->value));
66
            }
67 View Code Duplication
            if ($option->name == 'max' && $this->type == 'Range') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
68
                $property->setNumericMaximum(floatval($option->value));
69
            }
70
            if ($option->name == 'value' && $this->type == 'GreaterThanOrEqual') {
71
                $property->setNumericMinimum(floatval($option->value));
72
            }
73 View Code Duplication
            if ($option->name == 'value' && $this->type == 'LessThanOrEqual') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
74
                $property->setNumericMaximum(floatval($option->value));
75
            }
76
        }
77
78
        return $property;
79
    }
80
}
81