Completed
Pull Request — master (#114)
by
unknown
02:50
created

FrmFieldNumber::sanitize_value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @since 3.0
5
 */
6
class FrmFieldNumber extends FrmFieldType {
7
8
	/**
9
	 * @var string
10
	 * @since 3.0
11
	 */
12
	protected $type = 'number';
13
	protected $display_type = 'text';
14
15
	protected function field_settings_for_type() {
16
		$settings = array(
17
			'size'           => true,
18
			'clear_on_focus' => true,
19
			'invalid'        => true,
20
			'range'          => true,
21
		);
22
23
		$frm_settings = FrmAppHelper::get_settings();
24
		if ( $frm_settings->use_html ) {
25
			$settings['max'] = false;
26
		}
27
28
		return $settings;
29
	}
30
31
	protected function extra_field_opts() {
32
		return array(
33
			'minnum' => 0,
34
			'maxnum' => 9999999,
35
			'step'   => 'any',
36
		);
37
	}
38
39
	/**
40
	 * @since 3.01.03
41
	 */
42
	protected function add_extra_html_atts( $args, &$input_html ) {
43
		$this->add_min_max( $args, $input_html );
44
	}
45
46
	public function validate( $args ) {
47
		$errors = array();
48
49
		$this->remove_commas_from_number( $args );
50
51
		//validate the number format
52 View Code Duplication
		if ( ! is_numeric( $args['value'] ) && '' !== $args['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...
53
			$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $this->field, 'invalid' );
54
		}
55
56
		// validate number settings
57
		if ( $args['value'] != '' ) {
58
			$frm_settings = FrmAppHelper::get_settings();
59
			// only check if options are available in settings
60
			$minnum = FrmField::get_option( $this->field, 'minnum' );
61
			$maxnum = FrmField::get_option( $this->field, 'maxnum' );
62
			if ( $frm_settings->use_html && $maxnum !== '' && $minnum !== '' ) {
63
				$value = (float) $args['value'];
64 View Code Duplication
				if ( $value < $minnum ) {
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...
65
					$errors[ 'field' . $args['id'] ] = __( 'Please select a higher number', 'formidable' );
66
				} elseif ( $value > $maxnum ) {
67
					$errors[ 'field' . $args['id'] ] = __( 'Please select a lower number', 'formidable' );
68
				}
69
			}
70
		}
71
72
		return $errors;
73
	}
74
75
	/**
76
	 * IE fallback for number fields
77
	 * Remove the comma when HTML5 isn't supported
78
	 *
79
	 * @since 3.0
80
	 */
81
	private function remove_commas_from_number( &$args ) {
82
		if ( strpos( $args['value'], ',' ) ) {
83
			$args['value'] = str_replace( ',', '', $args['value'] );
84
			FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args );
85
		}
86
	}
87
88
	/**
89
	 * Force the value to be numeric before it's saved in the DB
90
	 */
91
	public function set_value_before_save( $value ) {
92
		if ( ! is_numeric( $value ) ) {
93
			$value = (float) $value;
94
		}
95
96
		return $value;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $value; (integer|double|string) is incompatible with the return type of the parent method FrmFieldType::set_value_before_save of type string|array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
97
	}
98
99
	/**
100
	 * @since 4.0
101
	 */
102
	public function sanitize_value( &$value ) {
103
		FrmAppHelper::sanitize_value( 'sanitize_text_field', $value );
104
	}
105
}
106