MethodGenerator::buildDefinition()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 8
nop 0
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 03.09.16 at 11:30
5
 */
6
namespace samsonphp\generator;
7
8
/**
9
 * Class method generation class.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
class MethodGenerator extends FunctionGenerator
14
{
15
    use VisibilityTrait;
16
    use AbstractFinalTrait;
17
18
    /**
19
     * {@inheritdoc}
20
     * @throws \InvalidArgumentException
21
     */
22
    public function defLine(string $code)
23
    {
24
        if ($this->isAbstract === true) {
25
            throw new \InvalidArgumentException('Abstract class cannot have code');
26
        }
27
28
        return parent::defLine($code);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::defLine($code); (samsonphp\generator\MethodGenerator) is incompatible with the return type of the parent method samsonphp\generator\FunctionGenerator::defLine of type samsonphp\generator\CodeTrait.

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...
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function code(int $indentation = 0) : string
35
    {
36
        if ($this->isAbstract === true) {
37
            return $this->buildDefinition() . '(' . $this->buildArguments($this->arguments) . ');';
38
        } else {
39
            return parent::code($indentation);
40
        }
41
    }
42
43
    /**
44
     * Build function definition.
45
     *
46
     * @return string Function definition
47
     */
48
    protected function buildDefinition()
49
    {
50
        return $this->indentation($this->indentation) .
51
        ($this->isFinal ? 'final ' : '') .
52
        ($this->isAbstract ? 'abstract ' : '') .
53
        $this->visibility . ' ' .
54
        ($this->isStatic ? 'static ' : '') .
55
        'function ' . $this->name;
56
    }
57
}
58