Completed
Push — master ( a51be7...c74983 )
by Antony
02:19
created

KnockoutConfirmedPasswordField::__construct()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
rs 8.8571
cc 3
eloc 22
nc 3
nop 6
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
require_once('Common.php');
3
require_once('CommonComposite.php');
4
5
/**
6
 * KnockoutConfirmedPasswordField
7
 *
8
 * Creates a {@link ConfirmedPasswordField} with an additional data-bind attribute that links to a Knockout obervable
9
 * @uses 'confirmedPassword' as the default observable
10
 */
11
class KnockoutConfirmedPasswordField extends ConfirmedPasswordField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    use \Knockout\Common;
14
    use \Knockout\CommonComposite;
15
    
16
    /**
17
     * bindingType
18
     *
19
     * KnockoutConfirmedPasswordField needs either 'value' or 'textInput' as a key for the 'data-bind' HTML attribute
20
     *
21
     * @var string data-bind attribute key
22
     * @example  data-bind="input: name, valueUpdate: 'input'" - the binding type is: input.
23
     */
24
    protected $bindingType = "textInput";
25
26
    /**
27
     * casting of variables for security purposes
28
     *
29
     * @see http://docs.silverstripe.org/en/3.1/developer_guides/security/secure_coding/
30
     */
31
    protected $casting = array(
32
        "Observable" => "Varchar",
33
        "Observables" => "Array",
34
        "BindingType" => "Varchar",
35
        "OtherBindings" => "Varchar",
36
        "HasFocus" => "Boolean"
37
    );
38
39
    /**
40
     * Constructor
41
     *
42
     * @param string $name
43
     * @param null|string $title
44
     * @param string $value
45
     */
46
    public function __construct($name, $title = null, $value = "", $form = null, $showOnClick = false,
47
            $titleConfirmField = null)
48
    {
49
        parent::__construct($name, $title, $value, $form, $showOnClick, $titleConfirmField);
50
        
51
        // Variables
52
        
53
        $fields = $this->children;
54
        $password_field = $fields->fieldByName('{$name}[_Password]');
0 ignored issues
show
Unused Code introduced by
$password_field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
        $confirmed_password_field = $fields->fieldByName('{$name}[_ConfirmPassword]');
0 ignored issues
show
Unused Code introduced by
$confirmed_password_field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
57
        // swap fields for the knockout ones
58
        foreach($fields as $key=>$field) {
59
            $name = $field->getName();
60
            $title = $field->Title();
61
            $value = $field->Value();
62
            $knockout_field = KnockoutPasswordField::create(
63
                $name,
64
                $title,
65
                $value
66
            );
67
68
            $fields->replaceField(
69
                $name,
70
                $knockout_field
71
            );
72
            
73
            if($key == 1) {
74
                $knockout_field->setObservable('confirmedPassword');
75
            }
76
        }
77
78
        Requirements::block(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
79
        Requirements::block(FRAMEWORK_DIR . '/javascript/ConfirmedPasswordField.js');
80
        Requirements::block(FRAMEWORK_DIR . '/css/ConfirmedPasswordField.css');
81
    }
82
}
83
84