MethodCollection::getIterator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the ClassGeneration package.
5
 *
6
 * (c) Antonio Spinelli <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ClassGeneration\Composition;
13
14
use ClassGeneration\Collection\ArrayCollection;
15
use ClassGeneration\Element\Tabbable;
16
17
/**
18
 * Method Collection ClassGeneration
19
 * @author Antonio Spinelli <[email protected]>
20
 */
21
class MethodCollection extends ArrayCollection implements Tabbable
22
{
23
    /**
24
     * Tabulation Identity.
25
     * @var int
26
     */
27
    protected $tabulation = 4;
28
29
    /**
30
     * @return MethodInterface[]
31
     */
32 5
    public function getIterator()
33
    {
34 5
        return parent::getIterator();
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 5
    public function getTabulation()
41
    {
42 5
        return $this->tabulation;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 5
    public function getTabulationFormatted()
49
    {
50 5
        return str_repeat(' ', $this->getTabulation());
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function setTabulation($tabulation)
57
    {
58
        $this->tabulation = (int)$tabulation;
59
60
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (ClassGeneration\Composition\MethodCollection) is incompatible with the return type declared by the interface ClassGeneration\Element\Tabbable::setTabulation of type ClassGeneration\Element\ElementAbstract.

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...
61
    }
62
63
    /**
64
     * @param MethodInterface $method
65
     * @return bool
66
     */
67 7
    public function add($method)
68
    {
69 7
        if (!$method instanceof MethodInterface) {
70 1
            throw new \InvalidArgumentException(
71
                'This Method must be a instance of \ClassGeneration\Composition\MethodInterface'
72 1
            );
73
        }
74 7
        parent::set($method->getTraitName() . '.' . $method->getName(), $method);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (set() instead of add()). Are you sure this is correct? If so, you might want to change this to $this->set().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
75 7
        return true;
76
    }
77
78
    /**
79
     * Parse the Composition Collection to string.
80
     * @return string
81
     */
82 5
    public function toString()
83
    {
84 5
        if ($this->isEmpty()) {
85
            return '';
86
        }
87
88 5
        $tabulationFormatted = $this->getTabulationFormatted();
89
90 5
        $string = '{' . PHP_EOL;
91 5
        $string .= $this->toStringMethods();
92 5
        $string .= $tabulationFormatted . '}';
93 5
        return $string . PHP_EOL;
94
    }
95
96 5
    protected function toStringMethods()
97
    {
98 5
        $useTraits = $this->getIterator();
99 5
        $tabulationFormatted = $this->getTabulationFormatted();
100
101 5
        $string = '';
102 5
        foreach ($useTraits as $useTrait) {
103 5
            $string .= $tabulationFormatted . $tabulationFormatted . $useTrait->toString();
104 5
        }
105
106 5
        return $string;
107
    }
108
}
109