AbstractResult::getResult()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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\Routlt\Description\ResultConfigurationDescriptorInterface;
27
use AppserverIo\Psr\Deployment\DescriptorInterface;
28
29
/**
30
 * Abstract result implementation that provides basic result functionality.
31
 *
32
 * @author     Tim Wagner <[email protected]>
33
 * @copyright  2015 TechDivision GmbH <[email protected]>
34
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link       http://github.com/appserver-io/routlt
36
 * @link       http://www.appserver.io
37
 */
38
abstract class AbstractResult implements ResultInterface, ActionAware, DescriptorAware
39
{
40
41
    /**
42
     * The descriptor instance.
43
     *
44
     * @var  \AppserverIo\Routlt\Description\ResultDescriptorInterface
45
     */
46
    protected $descriptor;
47
48
    /**
49
     * The action result name.
50
     *
51
     * @var string
52
     */
53
    protected $name;
54
55
    /**
56
     * The action result type.
57
     *
58
     * @var string
59
     */
60
    protected $type;
61
62
    /**
63
     * The action result value.
64
     *
65
     * @var array
66
     */
67
    protected $result;
68
69
    /**
70
     * The HTTP response code that has to be send.
71
     *
72
     * @var string
73
     */
74
    protected $code = 200;
75
76
    /**
77
     * Initializes the result from the result configuration descriptor instance.
78
     *
79
     * @param \AppserverIo\Routlt\Description\ResultConfigurationDescriptorInterface $resultConfigurationDescriptor The result configuration descriptor
80
     *
81
     * @return void
82
     */
83 9
    public function init(ResultConfigurationDescriptorInterface $resultConfigurationDescriptor)
84
    {
85
86
        // initialize the properites
87 9
        $this->name = $resultConfigurationDescriptor->getName();
88 9
        $this->type = $resultConfigurationDescriptor->getType();
89 9
        $this->code = $resultConfigurationDescriptor->getCode();
0 ignored issues
show
Documentation Bug introduced by
The property $code was declared of type string, but $resultConfigurationDescriptor->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...
90 9
        $this->result = $resultConfigurationDescriptor->getResult();
0 ignored issues
show
Documentation Bug introduced by
It seems like $resultConfigurationDescriptor->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...
91 9
    }
92
93
    /**
94
     * Returns the action result name.
95
     *
96
     * @return string The action result name
97
     */
98 3
    public function getName()
99
    {
100 3
        return $this->name;
101
    }
102
103
    /**
104
     * Returns the action result type.
105
     *
106
     * @return string The action result type
107
     */
108 3
    public function getType()
109
    {
110 3
        return $this->type;
111
    }
112
113
    /**
114
     * Returns the action result value.
115
     *
116
     * @return string The action result value
117
     */
118 6
    public function getResult()
119
    {
120 6
        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...
121
    }
122
123
    /**
124
     * Returns the HTTP response code that has to be send.
125
     *
126
     * @return integer The HTTP response code
127
     */
128
    public function getCode()
129
    {
130
        return $this->code;
131
    }
132
133
    /**
134
     * Sets the actual action instance.
135
     *
136
     * @param \AppserverIo\Routlt\ActionInterface $action The action instance
137
     *
138
     * @return void
139
     */
140 5
    public function setAction(ActionInterface $action)
141
    {
142 5
        $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...
143 5
    }
144
145
    /**
146
     * Returns the action instance.
147
     *
148
     * @return \AppserverIo\Routlt\ActionInterface The action instance
149
     */
150 5
    public function getAction()
151
    {
152 5
        return $this->action;
153
    }
154
155
    /**
156
     * Sets the descriptor instance.
157
     *
158
     * @param \AppserverIo\Psr\Deployment\DescriptorInterface $descriptor The descriptor instance
159
     *
160
     * @return void
161
     */
162
    public function setDescriptor(DescriptorInterface $descriptor)
163
    {
164
        $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...
165
    }
166
167
    /**
168
     * Returns the descriptor instance.
169
     *
170
     * @return \AppserverIo\Psr\Deployment\DescriptorInterface The descriptor instance
171
     */
172
    public function getDescriptor()
173
    {
174
        return $this->descriptor;
175
    }
176
}
177