Completed
Push — master ( 3bf76e...9f9c26 )
by Joschi
04:22
created

AbstractCommonMarkObject::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
cc 1
eloc 8
nc 1
nop 4
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Application
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Application\Model\Object;
38
39
use Apparat\Object\Application\Contract\CommonMarkPayloadProcessorInterface;
40
use Apparat\Object\Domain\Model\Object\AbstractObject;
41
use Apparat\Object\Domain\Model\Object\ObjectInterface;
42
use Apparat\Object\Domain\Model\Path\RepositoryPathInterface;
43
44
/**
45
 * Abstract CommonMark object
46
 *
47
 * @package Apparat\Object
48
 * @subpackage Apparat\Object\Application
49
 */
50
abstract class AbstractCommonMarkObject extends AbstractObject
51
{
52
    /**
53
     * Payload processor
54
     *
55
     * @var CommonMarkPayloadProcessorInterface
56
     */
57
    protected $payloadProcessor;
58
59
    /**
60
     * Object constructor
61
     *
62
     * @param CommonMarkPayloadProcessorInterface $payloadProcessor Payload processor
63
     * @param RepositoryPathInterface $path Object repository path
64
     * @param string $payload Object payload
65
     * @param array $propertyData Property data
66
     */
67 1
    public function __construct(
68
        CommonMarkPayloadProcessorInterface $payloadProcessor,
69
        RepositoryPathInterface $path,
70
        $payload,
71
        array $propertyData
72
    ) {
73 1
        $this->payloadProcessor = $payloadProcessor;
74 1
        $this->payloadProcessor->setObject($this);
75
76 1
        parent::__construct($path, $payload, $propertyData);
77 1
    }
78
79
80
    /**
81
     * Set the payload
82
     *
83
     * @param string $payload Payload
84
     * @return ObjectInterface Self reference
85
     */
86 1
    public function setPayload($payload)
87
    {
88 1
        parent::setPayload($payload);
89
90
        // Process the payload
91 1
        return $this->payloadProcessor->processPayload();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->payloadProcessor->processPayload(); (Apparat\Object\Applicati...bstractCommonMarkObject) is incompatible with the return type of the parent method Apparat\Object\Domain\Mo...tractObject::setPayload of type Apparat\Object\Domain\Mo...ect\Traits\PayloadTrait.

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...
92
    }
93
}
94