Completed
Push — master ( ac7d8e...ec16eb )
by Stephanie
05:53 queued 02:59
created

FrmFieldUrl   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 14.1 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A field_settings_for_type() 8 8 1
A extra_field_opts() 0 5 1
A get_field_name() 0 3 1
A fill_default_atts() 0 11 2
A validate() 3 16 4
A prepare_display_value() 0 17 4

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
 * @since 3.0
5
 */
6
class FrmFieldUrl extends FrmFieldType {
7
8
	/**
9
	 * @var string
10
	 * @since 3.0
11
	 */
12
	protected $type = 'url';
13
	protected $display_type = 'text';
14
15 View Code Duplication
	protected function field_settings_for_type() {
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...
16
		return array(
17
			'size'           => true,
18
			'clear_on_focus' => true,
19
			'invalid'        => true,
20
			'show_image'     => true,
21
		);
22
	}
23
24
	/**
25
	 * @return array
26
	 */
27
	protected function extra_field_opts() {
28
		return array(
29
			'show_image' => 0,
30
		);
31
	}
32
33
	protected function get_field_name() {
34
		return __( 'Website', 'formidable' );
35
	}
36
37
	protected function fill_default_atts( &$atts ) {
38
		$defaults = array(
39
			'sep' => ', ',
40
			'html' => false,
41
		);
42
		$atts = wp_parse_args( $atts, $defaults );
43
44
		if ( $atts['html'] ) {
45
			$atts['sep'] = ' ';
46
		}
47
	}
48
49
	public function validate( $args ) {
50
		$value = $args['value'];
51
		if ( trim( $value ) == 'http://' ) {
52
			$value = '';
53
		} else {
54
			$value = esc_url_raw( $value );
55
			$value = preg_match( '/^(https?|ftps?|mailto|news|feed|telnet):/is', $value ) ? $value : 'http://' . $value;
56
		}
57
58
		FrmEntriesHelper::set_posted_value( $this->field, $value, $args );
59
60
		// validate the url format
61 View Code Duplication
		if ( ! preg_match( '/^http(s)?:\/\/(?:localhost|(?:[\da-z\.-]+\.[\da-z\.-]+))/i', $value ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62
			$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'invalid' );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$errors was never initialized. Although not strictly required by PHP, it is generally a good practice to add $errors = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
63
		}
64
	}
65
66
	protected function prepare_display_value( $value, $atts ) {
67
		if ( $atts['html'] ) {
68
			$images = '';
69
			foreach ( (array) $value as $url ) {
70
				$image_regex = '/(\.(?i)(jpg|jpeg|png|gif))$/';
71
				$is_image = preg_match( $image_regex, $url );
72
				if ( $is_image ) {
73
					$images .= '<img src="' . esc_attr( $url ) . '" class="frm_image_from_url" alt="" /> ';
74
				} else {
75
					$images .= strip_tags( $url );
76
				}
77
			}
78
			$value = $images;
79
		}
80
81
		return $value;
82
	}
83
}
84