Completed
Push — master ( 12981b...2f1b03 )
by Robbie
14s
created

EditableTextField   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 40.38%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 0
loc 151
ccs 21
cts 52
cp 0.4038
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldValidationOptions() 0 17 1
B getCMSFields() 0 30 1
A getFormField() 0 17 2
B updateFormField() 0 20 7
1
<?php
2
/**
3
 * EditableTextField
4
 *
5
 * This control represents a user-defined text field in a user defined form
6
 *
7
 * @package userforms
8
 */
9
10
class EditableTextField extends EditableFormField
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...
11
{
12
13
    private static $singular_name = 'Text Field';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
15
    private static $plural_name = 'Text Fields';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $plural_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
17
    private static $has_placeholder = true;
0 ignored issues
show
Unused Code introduced by
The property $has_placeholder is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
18
19
    private static $autocomplete_options = array(
0 ignored issues
show
Unused Code introduced by
The property $autocomplete_options is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
        'off' => 'Off',
21
        'on' => 'On',
22
        'name' => 'Full name',
23
        'honorific-prefix' => 'Prefix or title',
24
        'given-name' => 'First name',
25
        'additional-name' => 'Additional name',
26
        'family-name' => 'Family name',
27
        'honorific-suffix' => 'Suffix (e.g Jr.)',
28
        'nickname' => 'Nickname',
29
        'email' => 'Email',
30
        'organization-title' => 'Job title',
31
        'organization' => 'Organization',
32
        'street-address' => 'Street address',
33
        'address-line1' => 'Address line 1',
34
        'address-line2' => 'Address line 2',
35
        'address-line3' => 'Address line 3',
36
        'address-level1' => 'Address level 1',
37
        'address-level2' => 'Address level 2',
38
        'address-level3' => 'Address level 3',
39
        'address-level4' => 'Address level 4',
40
        'country' => 'Country',
41
        'country-name' => 'Country name',
42
        'postal-code' => 'Postal code',
43
        'bday' => 'Birthday',
44
        'sex' => 'Gender identity',
45
        'tel' => 'Telephone number',
46
        'url' => 'Home page'
47
      );
48
49
    protected $jsEventHandler = 'keyup';
50
51
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
52
        'MinLength' => 'Int',
53
        'MaxLength' => 'Int',
54
        'Rows' => 'Int(1)',
55
        'Autocomplete' => 'Varchar(255)'
56
    );
57
58
    private static $defaults = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $defaults is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
59
        'Rows' => 1
60
    );
61
62
    public function getCMSFields()
63
    {
64
        $this->beforeUpdateCMSFields(function ($fields) {
65
            $fields->addFieldToTab(
66
                'Root.Main',
67
                NumericField::create(
68
                    'Rows',
69
                    _t('EditableTextField.NUMBERROWS', 'Number of rows')
70
                )->setDescription(_t(
71
                    'EditableTextField.NUMBERROWS_DESCRIPTION',
72
                    'Fields with more than one row will be generated as a textarea'
73
                ))
74
            );
75
76
            $fields->addFieldToTab(
77
                'Root.Main',
78
                DropdownField::create(
79
                    'Autocomplete',
80
                    _t('EditableTextField.AUTOCOMPLETE', 'Autocomplete'),
81
                    $this->config()->get('autocomplete_options')
82
                  )->setDescription(_t(
83
                      'EditableTextField.AUTOCOMPLETE_DESCRIPTION',
84
                      'Supported browsers will attempt to populate this field automatically with the users information, use to set the value populated'
85
                  ))
86
            );
87
88
        });
89
90
        return parent::getCMSFields();
91
    }
92
93
    /**
94
     * @return FieldList
95
     */
96
    public function getFieldValidationOptions()
97
    {
98
        $fields = parent::getFieldValidationOptions();
99
100
        $fields->merge(array(
101
            FieldGroup::create(
102
                _t('EditableTextField.TEXTLENGTH', 'Allowed text length'),
103
                array(
104
                    NumericField::create('MinLength', false),
105
                    LiteralField::create('RangeLength', _t("EditableTextField.RANGE_TO", "to")),
106
                    NumericField::create('MaxLength', false)
107
                )
108
            )
109
        ));
110
111
        return $fields;
112
    }
113
114
    /**
115
     * @return TextareaField|TextField
116
     */
117 10
    public function getFormField()
118
    {
119 10
        if ($this->Rows > 1) {
0 ignored issues
show
Documentation introduced by
The property Rows does not exist on object<EditableTextField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
120 1
            $field = TextareaField::create($this->Name, $this->EscapedTitle, $this->Default)
0 ignored issues
show
Documentation introduced by
The property EscapedTitle does not exist on object<EditableTextField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
121 1
                ->setFieldHolderTemplate('UserFormsField_holder')
122 1
                ->setTemplate('UserFormsTextareaField')
123 1
                ->setRows($this->Rows);
0 ignored issues
show
Documentation introduced by
The property Rows does not exist on object<EditableTextField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
124
        } else {
125 10
            $field = TextField::create($this->Name, $this->EscapedTitle, $this->Default)
0 ignored issues
show
Documentation introduced by
The property EscapedTitle does not exist on object<EditableTextField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
126 10
                ->setFieldHolderTemplate('UserFormsField_holder')
127 10
                ->setTemplate('UserFormsField');
128
        }
129
130 10
        $this->doUpdateFormField($field);
131
132 10
        return $field;
133
    }
134
135
    /**
136
     * Updates a formfield with the additional metadata specified by this field
137
     *
138
     * @param FormField $field
139
     */
140 10
    protected function updateFormField($field)
141
    {
142 10
        parent::updateFormField($field);
143
144 10
        if (is_numeric($this->MinLength) && $this->MinLength > 0) {
0 ignored issues
show
Documentation introduced by
The property MinLength does not exist on object<EditableTextField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
145 1
            $field->setAttribute('data-rule-minlength', intval($this->MinLength));
0 ignored issues
show
Documentation introduced by
The property MinLength does not exist on object<EditableTextField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
146
        }
147
148 10
        if (is_numeric($this->MaxLength) && $this->MaxLength > 0) {
0 ignored issues
show
Documentation introduced by
The property MaxLength does not exist on object<EditableTextField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
149 1
            if ($field instanceof TextField) {
150 1
                $field->setMaxLength(intval($this->MaxLength));
0 ignored issues
show
Documentation introduced by
The property MaxLength does not exist on object<EditableTextField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
151
            }
152 1
            $field->setAttribute('data-rule-maxlength', intval($this->MaxLength));
0 ignored issues
show
Documentation introduced by
The property MaxLength does not exist on object<EditableTextField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
153
        }
154
155 10
        if ($this->Autocomplete) {
0 ignored issues
show
Bug introduced by
The property Autocomplete does not seem to exist. Did you mean autocomplete_options?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
156
            $field->setAttribute('autocomplete', $this->Autocomplete);
0 ignored issues
show
Bug introduced by
The property Autocomplete does not seem to exist. Did you mean autocomplete_options?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
157
        }
158
159 10
    }
160
}
161