Completed
Push — master ( 411d94...8fae9a )
by John
02:59
created

ParserGeneric   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 37
rs 10
c 0
b 0
f 0
ccs 0
cts 17
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 7 1
A factory() 0 6 1
1
<?php
2
/**
3
 * This file is part of graze/unicontroller-client.
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/unicontroller-client/blob/master/LICENSE.md
11
 * @link https://github.com/graze/unicontroller-client
12
 */
13
namespace Graze\UnicontrollerClient\Parser\Parser;
14
15
use Graze\UnicontrollerClient\Parser\Parser\ParserInterface;
16
use Graze\UnicontrollerClient\Entity\EntityHydrator;
17
use Graze\UnicontrollerClient\Entity\Entity\EntityGeneric;
18
19
class ParserGeneric implements ParserInterface
20
{
21
    /**
22
     * @var EntityHydrator
23
     */
24
    private $entityHydrator;
25
26
    /**
27
     * @param EntityHydrator $entityHydrator
28
     */
29
    public function __construct(EntityHydrator $entityHydrator)
30
    {
31
        $this->entityHydrator = $entityHydrator;
32
    }
33
34
    /**
35
     * @param string $string
36
     * @return Graze\UnicontrollerClient\Entity\Entity\EntityInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be \Graze\UnicontrollerClie...\Entity\EntityInterface?

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.

Loading history...
37
     */
38
    public function parse($string)
39
    {
40
        return $this->entityHydrator->hydrate(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->entityHydr...' => $string == ' ')); (Graze\UnicontrollerClien...\Entity\EntityInterface) is incompatible with the return type declared by the interface Graze\UnicontrollerClien...\ParserInterface::parse of type Graze\UnicontrollerClien...\Entity\EntityInterface.

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...
41
            new EntityGeneric(),
42
            ['success' => $string == "\r\n"]
43
        );
44
    }
45
46
    /**
47
     * @return ParserGeneric
48
     */
49
    public static function factory()
50
    {
51
        return new static(
52
            new EntityHydrator()
53
        );
54
    }
55
}
56