Completed
Push — master ( c280e5...b534db )
by Tim
11s
created

AbstractResult::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * AppserverIo\Routlt\Results\AbstractResult
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author     Tim Wagner <[email protected]>
15
 * @copyright  2015 TechDivision GmbH <[email protected]>
16
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link       http://github.com/appserver-io/routlt
18
 * @link       http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Routlt\Results;
22
23
use AppserverIo\Routlt\ActionInterface;
24
use AppserverIo\Routlt\Util\ActionAware;
25
use AppserverIo\Routlt\Util\DescriptorAware;
26
use AppserverIo\Psr\Deployment\DescriptorInterface;
27
28
/**
29
 * Abstract result implementation that provides basic result functionality.
30
 *
31
 * @author     Tim Wagner <[email protected]>
32
 * @copyright  2015 TechDivision GmbH <[email protected]>
33
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link       http://github.com/appserver-io/routlt
35
 * @link       http://www.appserver.io
36
 */
37
abstract class AbstractResult implements ResultInterface, ActionAware, DescriptorAware
38
{
39
40
    /**
41
     * The descriptor instance.
42
     *
43
     * @var  \AppserverIo\Routlt\Description\ResultDescriptorInterface
44
     */
45
    protected $descriptor;
46
47
    /**
48
     * The action result name.
49
     *
50
     * @var string
51
     */
52
    protected $name;
53
54
    /**
55
     * The action result type.
56
     *
57
     * @var string
58
     */
59
    protected $type;
60
61
    /**
62
     * The action result value.
63
     *
64
     * @var array
65
     */
66
    protected $result;
67
68
    /**
69
     * The HTTP response code that has to be send.
70
     *
71
     * @var string
72
     */
73
    protected $code = 200;
74
75
    /**
76
     * Initializes the result from the result descriptor instance.
77
     *
78
     * @return void
79
     */
80
    public function init()
81
    {
82
83
        // load the injected descriptor instance
84
        /** @var \AppserverIo\Routlt\Description\ResultDescriptorInterface $resultDescriptor */
85
        $resultDescriptor = $this->getDescriptor();
86
87
        // initialize the properites
88
        $this->name = $resultDescriptor->getName();
89
        $this->type = $resultDescriptor->getType();
90
        $this->code = $resultDescriptor->getCode();
0 ignored issues
show
Documentation Bug introduced by
The property $code was declared of type string, but $resultDescriptor->getCode() is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
91
        $this->result = $resultDescriptor->getResult();
0 ignored issues
show
Documentation Bug introduced by
It seems like $resultDescriptor->getResult() of type string is incompatible with the declared type array of property $result.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
92
    }
93
94
    /**
95
     * Returns the action result name.
96
     *
97
     * @return string The action result name
98
     */
99
    public function getName()
100
    {
101
        return $this->name;
102
    }
103
104
    /**
105
     * Returns the action result type.
106
     *
107
     * @return string The action result type
108
     */
109
    public function getType()
110
    {
111
        return $this->type;
112
    }
113
114
    /**
115
     * Returns the action result value.
116
     *
117
     * @return string The action result value
118
     */
119
    public function getResult()
120
    {
121
        return $this->result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->result; (array) is incompatible with the return type declared by the interface AppserverIo\Routlt\Resul...ultInterface::getResult of type string.

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...
122
    }
123
124
    /**
125
     * Returns the HTTP response code that has to be send.
126
     *
127
     * @return integer The HTTP response code
128
     */
129
    public function getCode()
130
    {
131
        return $this->code;
132
    }
133
134
    /**
135
     * Sets the actual action instance.
136
     *
137
     * @param \AppserverIo\Routlt\ActionInterface $action The action instance
138
     *
139
     * @return void
140
     */
141
    public function setAction(ActionInterface $action)
142
    {
143
        $this->action = $action;
0 ignored issues
show
Bug introduced by
The property action does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
144
    }
145
146
    /**
147
     * Returns the action instance.
148
     *
149
     * @return \AppserverIo\Routlt\ActionInterface The action instance
150
     */
151
    public function getAction()
152
    {
153
        return $this->action;
154
    }
155
156
    /**
157
     * Sets the descriptor instance.
158
     *
159
     * @param \AppserverIo\Psr\Deployment\DescriptorInterface $descriptor The descriptor instance
160
     *
161
     * @return void
162
     */
163
    public function setDescriptor(DescriptorInterface $descriptor)
164
    {
165
        $this->descriptor = $descriptor;
0 ignored issues
show
Documentation Bug introduced by
$descriptor is of type object<AppserverIo\Psr\D...nt\DescriptorInterface>, but the property $descriptor was declared to be of type object<AppserverIo\Routl...ultDescriptorInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
166
    }
167
168
    /**
169
     * Returns the descriptor instance.
170
     *
171
     * @return \AppserverIo\Psr\Deployment\DescriptorInterface The descriptor instance
172
     */
173
    public function getDescriptor()
174
    {
175
        return $this->descriptor;
176
    }
177
}
178