Completed
Push — master ( d5c9e6...77b81f )
by Konstantin
02:19
created

FullNameHelper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 77
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 5 1
A isCyrillic() 0 7 2
A isLatin() 0 7 2
A isText() 0 7 2
A explodeName() 0 19 3
A getFirstName() 0 4 1
A getSecondName() 0 4 1
A getMiddleName() 0 4 1
A get() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: root
5
 * Date: 07.07.17
6
 * Time: 14:59
7
 */
8
9
namespace FormHelper;
10
11
12
class FullNameHelper extends AbstractFormHelper
13
{
14
    private $full_name;
15
    private $first;
16
    private $second;
17
    private $middle;
18
19 6
    public function set($full_name)
20
    {
21 6
        $this->full_name = $full_name;
22 6
        return $this;
23
    }
24
25 2
    public function isCyrillic($word = null)
26
    {
27 2
        if (!parent::isCyrillic($this->full_name)) {
28 1
            $this->full_name = false;
29
        }
30 2
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (FormHelper\FullNameHelper) is incompatible with the return type of the parent method FormHelper\AbstractFormHelper::isCyrillic of type boolean.

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...
31
    }
32
33 2
    public function isLatin($word = null)
34
    {
35 2
        if (!parent::isLatin($this->full_name)) {
36 1
            $this->full_name = false;
37
        }
38 2
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (FormHelper\FullNameHelper) is incompatible with the return type of the parent method FormHelper\AbstractFormHelper::isLatin of type boolean.

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...
39
    }
40
41 2
    public function isText($word = null)
42
    {
43 2
        if (!parent::isText($this->full_name)) {
44 1
            $this->full_name = false;
45
        }
46 2
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (FormHelper\FullNameHelper) is incompatible with the return type of the parent method FormHelper\AbstractFormHelper::isText of type boolean.

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...
47
    }
48
49 6
    public function explodeName()
50
    {
51 6
        $name = explode(' ', $this->full_name);
52 6
        $count_name = count($name);
53
54 6
        if ($count_name == 3) {
55 2
            $this->first = $name[0];
56 2
            $this->second = $name[1];
57 2
            $this->middle = $name[2];
58 4
        } elseif ($count_name == 2) {
59 1
            $this->first = $name[0];
60 1
            $this->second = $name[1];
61 1
            $this->middle = false;
62
        } else {
63 3
            $this->first = $this->second = $this->middle = false;
64
        }
65
66 6
        return $this;
67
    }
68
69 6
    public function getFirstName()
70
    {
71 6
        return $this->first;
72
    }
73
74 3
    public function getSecondName()
75
    {
76 3
        return $this->second;
77
    }
78
79 3
    public function getMiddleName()
80
    {
81 3
        return $this->middle;
82
    }
83
84 2
    public function get()
85
    {
86 2
        return ['first' => $this->first, 'second' => $this->second, 'middle' => $this->middle];
87
    }
88
}