Completed
Branch FET-10304-welcome-to-vue (644299)
by
unknown
94:54 queued 83:07
created

EE_Boolean_Field::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
defined( 'EVENT_ESPRESSO_VERSION') || exit;
3
4
class EE_Boolean_Field extends EE_Integer_Field
5
{
6
    /**
7
     * @param string $table_column
8
     * @param string $nicename
9
     * @param bool   $nullable
10
     * @param null   $default_value
11
     */
12
    public function __construct($table_column, $nicename, $nullable, $default_value = null)
13
    {
14
        parent::__construct($table_column, $nicename, $nullable, $default_value);
15
        $this->setSchemaType('boolean');
16
    }
17
18
    public function prepare_for_set($value_inputted_for_field_on_model_object)
19
    {
20
        if ($value_inputted_for_field_on_model_object) {
21
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type of the parent method EE_Integer_Field::prepare_for_set of type integer.

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...
22
        } else {
23
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method EE_Integer_Field::prepare_for_set of type integer.

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...
24
        }
25
    }
26
27
    /**
28
     * Make sure we're returning booleans
29
     *
30
     * @param string $value_inputted_for_field_on_model_object
31
     * @return boolean
32
     */
33
    public function prepare_for_set_from_db($value_inputted_for_field_on_model_object)
34
    {
35
        return intval($value_inputted_for_field_on_model_object) ? true : false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return intval($value_inp...object) ? true : false; (boolean) is incompatible with the return type of the parent method EE_Integer_Field::prepare_for_set_from_db of type integer.

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...
36
    }
37
38
    /**
39
     * Gets a nice Yes/No value for this field
40
     *
41
     * @param boolean $value_on_field_to_be_outputted
42
     * @return string Yes or No
43
     */
44
    public function prepare_for_pretty_echoing($value_on_field_to_be_outputted)
45
    {
46
        return apply_filters(
47
            'FHEE__EE_Boolean_Field__prepare_for_pretty_echoing__return',
48
            $value_on_field_to_be_outputted ? __('Yes', 'event_espresso') : __('No', 'event_espresso'),
49
            $value_on_field_to_be_outputted
50
        );
51
    }
52
}
53