Test Failed
Push — dev ( ccede5...b27119 )
by Herberto
13:46
created

VisitorViewModel::constructFromEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Presentation\Api\GraphQl\Node\User\Visitor;
16
17
use Acme\App\Core\Component\User\Domain\User\User;
18
use Acme\App\Presentation\Api\GraphQl\Node\User\AbstractUserViewModel;
19
20 View Code Duplication
final class VisitorViewModel extends AbstractUserViewModel
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
{
22
    /**
23
     * @var string
24
     */
25
    private $visitorViewModelClass = __CLASS__;
26
27
    public static function constructFromEntity(User $user): self
28
    {
29
        return new static(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new static($user-...Password(), 'Visitor'); (Acme\App\Presentation\Ap...isitor\VisitorViewModel) is incompatible with the return type of the parent method Acme\App\Presentation\Ap...el::constructFromEntity of type Acme\App\Presentation\Ap...\Editor\EditorViewModel.

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...
30
            $user->getId()->toScalar(),
31
            $user->getFullName(),
32
            $user->getUsername(),
33
            $user->getEmail(),
34
            $user->getMobile(),
35
            $user->getPassword(),
36
            'Visitor'
37
        );
38
    }
39
40
    public function getVisitorViewModelClass(): string
41
    {
42
        return $this->visitorViewModelClass;
43
    }
44
}
45