Entity::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php namespace C4tech\RayEmitter\Domain;
2
3
use C4tech\RayEmitter\Contracts\Domain\Entity as EntityInterface;
4
use C4tech\RayEmitter\Contracts\Domain\ValueObject as ValueObjectInterface;
5
use C4tech\RayEmitter\Exceptions\UnknownProperty;
6
7
abstract class Entity implements EntityInterface
8
{
9
    /**
10
     * The Entity's identity.
11
     * @var ValueObjectInterface
12
     */
13
    protected $identity;
14
15
    /**
16
     * @inheritDoc
17
     */
18 13
    public function __construct(ValueObjectInterface $identifier)
19
    {
20 13
        if (method_exists($this, 'setId')) {
21 5
            $this->setId($identifier);
0 ignored issues
show
Bug introduced by
The method setId() does not seem to exist on object<C4tech\RayEmitter\Domain\Entity>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
22 5
        } else {
23 9
            $this->identity = $identifier;
24
        }
25 13
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30 2
    public function getId()
31
    {
32 2
        return $this->identity;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->identity; (C4tech\RayEmitter\Contracts\Domain\ValueObject) is incompatible with the return type declared by the interface C4tech\RayEmitter\Contracts\Domain\Entity::getId of type C4tech\RayEmitter\Contra...in\ValueObjectInterface.

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...
33
    }
34
35
    /**
36
     * Set
37
     *
38
     * Protected method to set property value.
39
     * @param  string $property Requested "property"
40
     * @param  mixed  $value    Requested value
41
     * @return mixed
42
     */
43 4
    protected function set($property, $value)
44
    {
45 4
        $method = 'set' . studly_case($property);
46
47 4
        if ($property !== 'id' && method_exists($this, $method)) {
48 2
            $this->$method($value);
49 4
        } elseif ($property !== 'identity' && property_exists($this, $property)) {
50 1
            $this->$property = $value;
51 1
        } else {
52 1
            throw new UnknownProperty('There is no way to set ' . $property, 504);
53
        }
54
55 3
    }
56
57
    /**
58
     * Magic Getter
59
     *
60
     * Expose getter methods as properties.
61
     * @param  string $property Requested "property"
62
     * @return mixed
63
     */
64 4
    public function __get($property)
65
    {
66 4
        $method = 'get' . studly_case($property);
67
68 4
        if (method_exists($this, $method)) {
69 1
            return $this->$method();
70 3
        } elseif (isset($this->$property)) {
71 2
            return $this->$property;
72
        }
73
74 1
        throw new UnknownProperty('Cannot find the property ' . $property . ' on ' . get_class($this), 504);
75
    }
76
}
77