HttpServlet::doConnect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * \AppserverIo\Psr\Servlet\Http\HttpServlet
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      https://github.com/appserver-io-psr/servlet
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Psr\Servlet\Http;
22
23
use AppserverIo\Psr\HttpMessage\Protocol;
24
use AppserverIo\Psr\Servlet\GenericServlet;
25
use AppserverIo\Psr\Servlet\ServletException;
26
use AppserverIo\Psr\Servlet\ServletRequestInterface;
27
use AppserverIo\Psr\Servlet\ServletResponseInterface;
28
29
/**
30
 * Abstract Http servlet implementation.
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      https://github.com/appserver-io-psr/servlet
36
 * @link      http://www.appserver.io
37
 */
38
abstract class HttpServlet extends GenericServlet
39
{
40
41
    /**
42
     * Implements Http CONNECT method.
43
     *
44
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
45
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
46
     *
47
     * @param \AppserverIo\Psr\Servlet\
48
     */
49
    protected $sessionSettings;
50
51
    /**
52
     * Implements Http CONNECT method.
53
     *
54
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
55
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
56
     *
57
     * @return void
58
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not implemented
59
     */
60
    public function doConnect(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
0 ignored issues
show
Unused Code introduced by
The parameter $servletRequest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $servletResponse is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        throw new ServletException(sprintf('Method %s is not implemented in servlet %s.', __METHOD__, get_class($this)));
63
    }
64
65
    /**
66
     * Implements Http DELETE method.
67
     *
68
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
69
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
70
     *
71
     * @return void
72
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not implemented
73
     */
74
    public function doDelete(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
0 ignored issues
show
Unused Code introduced by
The parameter $servletRequest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $servletResponse is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        throw new ServletException(sprintf('Method %s is not implemented in servlet %s.', __METHOD__, get_class($this)));
77
    }
78
79
    /**
80
     * Implements Http GET method.
81
     *
82
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
83
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
84
     *
85
     * @return void
86
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not implemented
87
     */
88
    public function doGet(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
0 ignored issues
show
Unused Code introduced by
The parameter $servletRequest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $servletResponse is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90
        throw new ServletException(sprintf('Method %s is not implemented in servlet %s.', __METHOD__, get_class($this)));
91
    }
92
93
    /**
94
     * Implements Http HEAD method.
95
     *
96
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
97
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
98
     *
99
     * @return void
100
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not implemented
101
     */
102
    public function doHead(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
0 ignored issues
show
Unused Code introduced by
The parameter $servletRequest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $servletResponse is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    {
104
        throw new ServletException(sprintf('Method %s is not implemented in servlet %s.', __METHOD__, get_class($this)));
105
    }
106
107
    /**
108
     * Implements Http OPTIONS method.
109
     *
110
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
111
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
112
     *
113
     * @return void
114
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not implemented
115
     */
116
    public function doOptions(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
0 ignored issues
show
Unused Code introduced by
The parameter $servletRequest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $servletResponse is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
    {
118
        throw new ServletException(sprintf('Method %s is not implemented in servlet %s.', __METHOD__, get_class($this)));
119
    }
120
121
    /**
122
     * Implements Http POST method.
123
     *
124
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
125
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
126
     *
127
     * @return void
128
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not implemented
129
     */
130
    public function doPost(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
0 ignored issues
show
Unused Code introduced by
The parameter $servletRequest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $servletResponse is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
        throw new ServletException(sprintf('Method %s is not implemented in servlet %s.', __METHOD__, get_class($this)));
133
    }
134
135
    /**
136
     * Implements Http PUT method.
137
     *
138
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
139
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
140
     *
141
     * @return void
142
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not implemented
143
     */
144
    public function doPut(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
0 ignored issues
show
Unused Code introduced by
The parameter $servletRequest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $servletResponse is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
145
    {
146
        throw new ServletException(sprintf('Method %s is not implemented in servlet %s.', __METHOD__, get_class($this)));
147
    }
148
149
    /**
150
     * Implements Http TRACE method.
151
     *
152
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
153
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
154
     *
155
     * @return void
156
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not implemented
157
     */
158
    public function doTrace(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
0 ignored issues
show
Unused Code introduced by
The parameter $servletRequest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $servletResponse is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
159
    {
160
        throw new ServletException(sprintf('Method %s is not implemented in servlet %s.', __METHOD__, get_class($this)));
161
    }
162
163
    /**
164
     * Implements Http PATCH method.
165
     *
166
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
167
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
168
     *
169
     * @return void
170
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not implemented
171
     */
172
    public function doPatch(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
0 ignored issues
show
Unused Code introduced by
The parameter $servletRequest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $servletResponse is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
173
    {
174
        throw new ServletException(sprintf('Method %s is not implemented in servlet %s.', __METHOD__, get_class($this)));
175
    }
176
177
    /**
178
     * Delegation method for specific Http methods.
179
     *
180
     * @param \AppserverIo\Psr\Servlet\ServletRequestInterface  $servletRequest  The request instance
181
     * @param \AppserverIo\Psr\Servlet\ServletResponseInterface $servletResponse The response instance
182
     *
183
     * @return void
184
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not available
185
     */
186
    public function service(ServletRequestInterface $servletRequest, ServletResponseInterface $servletResponse)
187
    {
188
189
        // pre-initialize response
190
        $servletResponse->addHeader(Protocol::HEADER_X_POWERED_BY, get_class($this));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface AppserverIo\Psr\Servlet\ServletResponseInterface as the method addHeader() does only exist in the following implementations of said interface: AppserverIo\Psr\Servlet\...pServletResponseWrapper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
191
192
        // check the request method to invoke the appropriate method
193
        switch ($servletRequest->getMethod()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface AppserverIo\Psr\Servlet\ServletRequestInterface as the method getMethod() does only exist in the following implementations of said interface: AppserverIo\Psr\Servlet\...tpServletRequestWrapper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
194
            case Protocol::METHOD_CONNECT:
195
                $this->doConnect($servletRequest, $servletResponse);
0 ignored issues
show
Compatibility introduced by
$servletRequest of type object<AppserverIo\Psr\S...ervletRequestInterface> is not a sub-type of object<AppserverIo\Psr\S...ervletRequestInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletRequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
$servletResponse of type object<AppserverIo\Psr\S...rvletResponseInterface> is not a sub-type of object<AppserverIo\Psr\S...rvletResponseInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
196
                break;
197
            case Protocol::METHOD_DELETE:
198
                $this->doDelete($servletRequest, $servletResponse);
0 ignored issues
show
Compatibility introduced by
$servletRequest of type object<AppserverIo\Psr\S...ervletRequestInterface> is not a sub-type of object<AppserverIo\Psr\S...ervletRequestInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletRequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
$servletResponse of type object<AppserverIo\Psr\S...rvletResponseInterface> is not a sub-type of object<AppserverIo\Psr\S...rvletResponseInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
199
                break;
200
            case Protocol::METHOD_GET:
201
                $this->doGet($servletRequest, $servletResponse);
0 ignored issues
show
Compatibility introduced by
$servletRequest of type object<AppserverIo\Psr\S...ervletRequestInterface> is not a sub-type of object<AppserverIo\Psr\S...ervletRequestInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletRequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
$servletResponse of type object<AppserverIo\Psr\S...rvletResponseInterface> is not a sub-type of object<AppserverIo\Psr\S...rvletResponseInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
202
                break;
203
            case Protocol::METHOD_HEAD:
204
                $this->doHead($servletRequest, $servletResponse);
0 ignored issues
show
Compatibility introduced by
$servletRequest of type object<AppserverIo\Psr\S...ervletRequestInterface> is not a sub-type of object<AppserverIo\Psr\S...ervletRequestInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletRequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
$servletResponse of type object<AppserverIo\Psr\S...rvletResponseInterface> is not a sub-type of object<AppserverIo\Psr\S...rvletResponseInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
205
                break;
206
            case Protocol::METHOD_OPTIONS:
207
                $this->doOptions($servletRequest, $servletResponse);
0 ignored issues
show
Compatibility introduced by
$servletRequest of type object<AppserverIo\Psr\S...ervletRequestInterface> is not a sub-type of object<AppserverIo\Psr\S...ervletRequestInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletRequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
$servletResponse of type object<AppserverIo\Psr\S...rvletResponseInterface> is not a sub-type of object<AppserverIo\Psr\S...rvletResponseInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
208
                break;
209
            case Protocol::METHOD_POST:
210
                $this->doPost($servletRequest, $servletResponse);
0 ignored issues
show
Compatibility introduced by
$servletRequest of type object<AppserverIo\Psr\S...ervletRequestInterface> is not a sub-type of object<AppserverIo\Psr\S...ervletRequestInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletRequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
$servletResponse of type object<AppserverIo\Psr\S...rvletResponseInterface> is not a sub-type of object<AppserverIo\Psr\S...rvletResponseInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
211
                break;
212
            case Protocol::METHOD_PUT:
213
                $this->doPut($servletRequest, $servletResponse);
0 ignored issues
show
Compatibility introduced by
$servletRequest of type object<AppserverIo\Psr\S...ervletRequestInterface> is not a sub-type of object<AppserverIo\Psr\S...ervletRequestInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletRequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
$servletResponse of type object<AppserverIo\Psr\S...rvletResponseInterface> is not a sub-type of object<AppserverIo\Psr\S...rvletResponseInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
214
                break;
215
            case Protocol::METHOD_TRACE:
216
                $this->doTrace($servletRequest, $servletResponse);
0 ignored issues
show
Compatibility introduced by
$servletRequest of type object<AppserverIo\Psr\S...ervletRequestInterface> is not a sub-type of object<AppserverIo\Psr\S...ervletRequestInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletRequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
$servletResponse of type object<AppserverIo\Psr\S...rvletResponseInterface> is not a sub-type of object<AppserverIo\Psr\S...rvletResponseInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
217
                break;
218
            case Protocol::METHOD_PATCH:
219
                $this->doPatch($servletRequest, $servletResponse);
0 ignored issues
show
Compatibility introduced by
$servletRequest of type object<AppserverIo\Psr\S...ervletRequestInterface> is not a sub-type of object<AppserverIo\Psr\S...ervletRequestInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletRequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
$servletResponse of type object<AppserverIo\Psr\S...rvletResponseInterface> is not a sub-type of object<AppserverIo\Psr\S...rvletResponseInterface>. It seems like you assume a child interface of the interface AppserverIo\Psr\Servlet\ServletResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
220
                break;
221
            default:
222
                throw new ServletException(
223
                    sprintf('%s is not implemented yet.', $servletRequest->getMethod())
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface AppserverIo\Psr\Servlet\ServletRequestInterface as the method getMethod() does only exist in the following implementations of said interface: AppserverIo\Psr\Servlet\...tpServletRequestWrapper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
224
                );
225
        }
226
    }
227
228
    /**
229
     * Destroys the object on shutdown.
230
     *
231
     * @return void
232
     */
233
    public function destroy()
234
    {
235
    }
236
}
237