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

PodsField_Slug   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 143
Duplicated Lines 18.88 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 27
loc 143
rs 10
c 0
b 0
f 0
wmc 17
lcom 2
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 17 1
A schema() 0 6 1
A display() 0 4 1
C input() 27 27 7
A regex() 0 4 1
A validate() 0 4 1
A pre_save() 0 12 3
A ui() 0 4 1
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_Slug extends PodsField {
7
8
	/**
9
	 * {@inheritdoc}
10
	 */
11
	public static $type = 'slug';
12
13
	/**
14
	 * {@inheritdoc}
15
	 */
16
	public static $label = 'Permalink (url-friendly)';
17
18
	/**
19
	 * {@inheritdoc}
20
	 */
21
	public static $prepare = '%s';
22
23
	/**
24
	 * {@inheritdoc}
25
	 */
26
	public static $pod_types = array(
27
		'pod',
28
		'table',
29
	);
30
31
	/**
32
	 * {@inheritdoc}
33
	 */
34
	public function setup() {
35
36
		self::$label = __( 'Permalink (url-friendly)', 'pods' );
37
	}
38
39
	/**
40
	 * {@inheritdoc}
41
	 */
42
	public function options() {
43
44
		$options = array(
45
			static::$type . '_placeholder' => array(
46
				'label'   => __( 'HTML Placeholder', 'pods' ),
47
				'default' => '',
48
				'type'    => 'text',
49
				'help'    => array(
50
					__( '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' ),
51
					'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
52
				),
53
			),
54
		);
55
56
		return $options;
57
58
	}
59
60
	/**
61
	 * {@inheritdoc}
62
	 */
63
	public function schema( $options = null ) {
64
65
		$schema = 'VARCHAR(200)';
66
67
		return $schema;
68
	}
69
70
	/**
71
	 * {@inheritdoc}
72
	 */
73
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
74
75
		return $value;
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
		$field_type = 'slug';
91
92
		if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) {
93
			if ( pods_v( 'read_only', $options, false ) ) {
94
				$options['readonly'] = true;
95
96
				$field_type = 'text';
97
			} else {
98
				return;
99
			}
100
		} elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
101
			$options['readonly'] = true;
102
103
			$field_type = 'text';
104
		}
105
106
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
107
	}
108
109
	/**
110
	 * {@inheritdoc}
111
	 */
112
	public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
113
114
		return false;
115
	}
116
117
	/**
118
	 * {@inheritdoc}
119
	 */
120
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
121
122
		return true;
123
	}
124
125
	/**
126
	 * {@inheritdoc}
127
	 */
128
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
129
130
		$index = pods_v( 'pod_index', pods_v( 'options', $pod, $pod, true ), 'id', true );
131
132
		if ( empty( $value ) && isset( $fields[ $index ] ) ) {
133
			$value = $fields[ $index ]['value'];
134
		}
135
136
		$value = pods_unique_slug( $value, $name, $pod, 0, $params->id, null, false );
137
138
		return $value;
139
	}
140
141
	/**
142
	 * {@inheritdoc}
143
	 */
144
	public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
145
146
		return $this->display( $value, $name, $options, $pod, $id );
147
	}
148
}
149