Completed
Push — master ( 72a246...6ab249 )
by Helmut
05:19
created

NewPassword   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 8
loc 76
c 0
b 0
f 0
wmc 13
lcom 1
cbo 3
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A hash() 8 8 2
A getValue() 0 7 1
A getButtonName() 0 4 1
A renderWith() 0 9 1
A setValueFromDefault() 0 4 1
A setValueFromModel() 0 4 1
A setValueFromRequest() 0 5 1
A fillModelWithValue() 0 6 2
A validate() 0 4 1
A validateMatches() 0 4 1
A validateRequired() 0 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
3
namespace Helmut\Forms\Fields\NewPassword;
4
5
use Helmut\Forms\Field;
6
use Helmut\Forms\Utility\Validate;
7
8
class NewPassword extends Field {
9
10
    protected $value = '';
11
    protected $value_confirmation = '';
12
13 View Code Duplication
    public function hash($value) 
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...
14
    {
15
        if (strlen($value) === 0) {
16
            return '';
17
        }
18
19
        return password_hash($value, PASSWORD_BCRYPT, ['cost' => 10]);
20
    }
21
22
    public function getValue()
23
    {
24
        $values = [];
25
        $values[$this->name] = $this->value;
26
        $values[$this->name.'_confirmation'] = $this->value_confirmation;
27
        return $values;
28
    }
29
30
    public function getButtonName()
31
    {
32
        //
33
    }
34
35
    public function renderWith()
36
    {
37
        return [    
38
            'name' => $this->name, 
39
            'name_confirmation' => $this->name.'_confirmation', 
40
            'value' => $this->value,
41
            'value_confirmation' => $this->value_confirmation,
42
        ];
43
    }
44
45
    public function setValueFromDefault()
46
    {
47
        //
48
    }
49
50
    public function setValueFromModel($model)
51
    {
52
        //
53
    }
54
55
    public function setValueFromRequest($request)
56
    {
57
        $this->value = $request->get($this->name);
58
        $this->value_confirmation = $request->get($this->name.'_confirmation');
59
    }
60
61
    public function fillModelWithValue($model)
62
    {
63
        if (property_exists($model, $this->name)) {
64
            $model->{$this->name} = $this->hash($this->value);
65
        }
66
    }
67
68
    public function validate()
69
    {
70
        $this->matches();
0 ignored issues
show
Bug introduced by
The method matches() does not exist on Helmut\Forms\Fields\NewPassword\NewPassword. Did you maybe mean validateMatches()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
71
    }
72
73
    public function validateMatches()
74
    {
75
        return Validate::matches($this->value, $this->value_confirmation);
76
    }    
77
    
78
    public function validateRequired()
79
    {
80
        return Validate::required($this->value);
81
    }
82
83
}