Completed
Pull Request — 2.x (#4569)
by Scott Kingsley
04:56
created

PodsField_Password   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 137
Duplicated Lines 47.45 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 65
loc 137
rs 10
c 0
b 0
f 0
wmc 21
lcom 2
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 22 1
A schema() 12 12 3
B input() 21 21 7
B validate() 22 22 6
A pre_save() 10 10 3
A setup() 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
/**
4
 * @package Pods\Fields
5
 */
6
class PodsField_Password extends PodsField {
7
8
	/**
9
	 * {@inheritdoc}
10
	 */
11
	public static $group = 'Text';
12
13
	/**
14
	 * {@inheritdoc}
15
	 */
16
	public static $type = 'password';
17
18
	/**
19
	 * {@inheritdoc}
20
	 */
21
	public static $label = 'Password';
22
23
	/**
24
	 * {@inheritdoc}
25
	 */
26
	public static $prepare = '%s';
27
28
	/**
29
	 * {@inheritdoc}
30
	 */
31
	public function setup() {
32
33
		self::$label = __( 'Password', 'pods' );
34
	}
35
36
	/**
37
	 * {@inheritdoc}
38
	 */
39
	public function options() {
40
41
		$options = array(
42
			static::$type . '_max_length'  => array(
43
				'label'   => __( 'Maximum Length', 'pods' ),
44
				'default' => 255,
45
				'type'    => 'number',
46
				'help'    => __( 'Set to -1 for no limit', 'pods' ),
47
			),
48
			static::$type . '_placeholder' => array(
49
				'label'   => __( 'HTML Placeholder', 'pods' ),
50
				'default' => '',
51
				'type'    => 'text',
52
				'help'    => array(
53
					__( 'Placeholders can provide instructions or an example of the required data format for a field. Please note: It is not a replacement for labels or description text, and it is less accessible for people using screen readers.', 'pods' ),
54
					'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
55
				),
56
			),
57
		);
58
59
		return $options;
60
	}
61
62
	/**
63
	 * {@inheritdoc}
64
	 */
65 View Code Duplication
	public function schema( $options = null ) {
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...
66
67
		$length = (int) pods_v( static::$type . '_max_length', $options, 255 );
68
69
		$schema = 'VARCHAR(' . $length . ')';
70
71
		if ( 255 < $length || $length < 1 ) {
72
			$schema = 'LONGTEXT';
73
		}
74
75
		return $schema;
76
	}
77
78
	/**
79
	 * {@inheritdoc}
80
	 */
81 View Code Duplication
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
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...
82
83
		$options         = (array) $options;
84
		$form_field_type = PodsForm::$field_type;
85
86
		if ( is_array( $value ) ) {
87
			$value = implode( ' ', $value );
88
		}
89
90
		if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) {
91
			if ( pods_v( 'read_only', $options, false ) ) {
92
				$options['readonly'] = true;
93
			} else {
94
				return;
95
			}
96
		} elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
97
			$options['readonly'] = true;
98
		}
99
100
		pods_view( PODS_DIR . 'ui/fields/password.php', compact( array_keys( get_defined_vars() ) ) );
101
	}
102
103
	/**
104
	 * {@inheritdoc}
105
	 */
106 View Code Duplication
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
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...
107
108
		$errors = array();
109
110
		$check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
111
112
		if ( is_array( $check ) ) {
113
			$errors = $check;
114
		} else {
115
			if ( 0 < strlen( $value ) && '' === $check ) {
116
				if ( 1 === (int) pods_v( 'required', $options ) ) {
117
					$errors[] = __( 'This field is required.', 'pods' );
118
				}
119
			}
120
		}
121
122
		if ( ! empty( $errors ) ) {
123
			return $errors;
124
		}
125
126
		return true;
127
	}
128
129
	/**
130
	 * {@inheritdoc}
131
	 */
132 View Code Duplication
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
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...
133
134
		$length = (int) pods_v( static::$type . '_max_length', $options, 255 );
135
136
		if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
137
			$value = pods_mb_substr( $value, 0, $length );
138
		}
139
140
		return $value;
141
	}
142
}
143