GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 7f5f8b...23b9d8 )
by Bruno
04:20
created

NotesManager::getNotesStrings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: bheron
5
 * Date: 25/07/2016
6
 * Time: 01:06
7
 */
8
namespace Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher;
9
10
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
11
12
class NotesManager implements NotesManagerInterface
13
{
14
    /**
15
     * @var ClassStoreInterface
16
     */
17
    private $classStore;
18
19
    /**
20
     * @var StringGeneratorInterface
21
     */
22
    private $stringGenerator;
23
24
    /**
25
     * @var array
26
     */
27
    private $str = array();
28
29 5
    public function __construct(
30
        ClassStoreInterface $classStore,
31
        StringGeneratorInterface $stringGenerator
32
    ) {
33 5
        $this->classStore       = $classStore;
34 5
        $this->stringGenerator  = $stringGenerator;
35 5
    }
36
37
    /**
38
     * @param array $notes
39
     * @return string
40
     */
41 4
    public function getNotesStrings(array $notes)
42
    {
43 4
        foreach ($notes as $className => $note) {
44 4
            $this->setNoteString($className, $note);
45 2
        }
46
47 2
        return $this->str;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->str; (array) is incompatible with the return type declared by the interface Onurb\Doctrine\ORMMetada...erface::getNotesStrings of type string.

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...
48
    }
49
50 4
    private function setNoteString($className, $note)
51
    {
52 4
        if ($class = $this->classStore->getClassByName($className)) {
53 4
            $this->str[] = $this->makeNoteLink($class, $note);
54 2
        }
55 2
    }
56
57
    /**
58
     * @param ClassMetadata $class
59
     * @param array $note
60
     * @return string
61
     * @throws \Exception
62
     */
63 4
    private function makeNoteLink(ClassMetadata $class, $note)
64
    {
65 4
        if (!$this->isValid($note)) {
66 2
            throw new \Exception('Invalid note. It must be an array, with  \'value\' key'
67 2
                .'and with optional \'color\' keys. And of course, description must not be empty ;) ^^');
68
        }
69
70 2
        return $this->stringGenerator->getClassString($class) . "-" . $this->makeNoteString($note);
71
    }
72
73
    /**
74
     * @param array $note
75
     * @return bool
76
     */
77 4
    private function isValid($note)
78
    {
79 4
        return isset($note['value']) && $note['value'] != '';
80
    }
81
82
    /**
83
     * @param array $note
84
     * @return string
85
     */
86 2
    private function makeNoteString($note)
87
    {
88 2
        return '[note:' . $note['value'] . $this->getNoteColor($note) . ']';
89
    }
90
91
    /**
92
     * @param $note
93
     * @return string
94
     */
95 2
    private function getNoteColor($note)
96
    {
97 2
        return isset($note['color']) ? '{bg:' . $note['color'] . '}' : '{bg:yellow}';
98
    }
99
}
100