Completed
Pull Request — MetadataImplementationStep (#116)
by Alex
01:56
created

AssociationPolymorphic::getArrayPayload()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 0
cts 34
cp 0
rs 8.9411
c 0
b 0
f 0
cc 2
eloc 35
nc 2
nop 0
crap 6
1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations;
4
5
use AlgoWeb\PODataLaravel\Providers\MetadataProvider;
6
7
class AssociationPolymorphic extends Association
8
{
9
    /**
10
     * @var AssociationStubBase[]
11
     */
12
    protected $last = [];
13
14
    /**
15
     * @return AssociationStubBase[]
16
     */
17
    public function getLast()
18
    {
19
        return $this->last;
20
    }
21
22
    public function setLast(array $stubs)
23
    {
24
        $this->last = $stubs;
25
    }
26
27
    /**
28
     * @return bool
29
     */
30
    public function isOk()
31
    {
32
        $first = $this->getFirst();
33
        $last = $this->getLast();
34
        if (!$this->isStubOk($first)) {
35
            return false;
36
        }
37
        if (0 == count($this->last)) {
38
            return false;
39
        }
40
        foreach ($last as $stub) {
41
            if (!$this->isStubOk($stub)) {
42
                return false;
43
            }
44
            if (!$first->isCompatible($stub)) {
45
                return false;
46
            }
47
        }
48
        return true;
49
    }
50
51
    /**
52
     * @return AssociationType|AssociationType[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
53
     */
54
    public function getAssociationType()
55
    {
56
        assert($this->isOk());
57
        $types = [];
58
        $first = $this->getFirst();
59
        $firstMult = $first->getMultiplicity()->getValue();
60
        $last = $this->getLast();
61
        foreach ($last as $stub) {
62
            $types[] = new AssociationType($firstMult | $stub->getMultiplicity()->getValue());
63
        }
64
65
        return $types;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $types; (array) is incompatible with the return type declared by the abstract method AlgoWeb\PODataLaravel\Mo...ion::getAssociationType of type AlgoWeb\PODataLaravel\Mo...tions\AssociationType[].

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...
66
    }
67
68
    private function isStubOk($stub)
69
    {
70
        return (null !== $stub && $stub instanceof AssociationStubPolymorphic && $stub->isOk());
71
    }
72
}
73