These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace JwPersistentUser; |
||
4 | |||
5 | use JwPersistentUser\Listener\WriteTokenToCookie; |
||
6 | use JwPersistentUser\Service\CookieAuthenticationService; |
||
7 | use Zend\EventManager\EventInterface; |
||
8 | use Zend\EventManager\EventManager; |
||
9 | use Zend\ModuleManager\Feature; |
||
10 | use Zend\ServiceManager\ServiceManager; |
||
11 | |||
12 | class Module implements |
||
13 | Feature\ConfigProviderInterface, |
||
14 | Feature\BootstrapListenerInterface, |
||
15 | Feature\AutoloaderProviderInterface |
||
16 | { |
||
17 | public function getConfig() |
||
18 | { |
||
19 | return include __DIR__ . '/config/module.config.php'; |
||
20 | } |
||
21 | |||
22 | public function onBootstrap(EventInterface $e) |
||
23 | { |
||
24 | /** @var EventManager $em */ |
||
25 | $em = $e->getApplication()->getEventManager(); |
||
0 ignored issues
–
show
|
|||
26 | |||
27 | /** @var ServiceManager $sm */ |
||
28 | $sm = $e->getApplication()->getServiceManager(); |
||
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
Zend\EventManager\EventInterface as the method getApplication() does only exist in the following implementations of said interface: Zend\Mvc\MvcEvent .
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
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
![]() |
|||
29 | |||
30 | $request = $e->getApplication()->getRequest(); |
||
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
Zend\EventManager\EventInterface as the method getApplication() does only exist in the following implementations of said interface: Zend\Mvc\MvcEvent .
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
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
![]() |
|||
31 | $response = $e->getApplication()->getResponse(); |
||
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
Zend\EventManager\EventInterface as the method getApplication() does only exist in the following implementations of said interface: Zend\Mvc\MvcEvent .
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
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
![]() |
|||
32 | |||
33 | // Try to login from Cookie if applicable |
||
34 | $service = new CookieAuthenticationService($sm); |
||
35 | $service->setEventManager(new EventManager($em->getSharedManager())); |
||
36 | $service->loginFrom($request, $response); |
||
37 | |||
38 | (new WriteTokenToCookie($sm))->attachShared($em->getSharedManager()); |
||
0 ignored issues
–
show
It seems like
$em->getSharedManager() can be null ; however, attachShared() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
![]() |
|||
39 | } |
||
40 | |||
41 | public function getAutoloaderConfig() |
||
42 | { |
||
43 | return [ |
||
44 | 'Zend\Loader\StandardAutoloader' => [ |
||
45 | 'namespaces' => [ |
||
46 | __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, |
||
47 | ], |
||
48 | ], |
||
49 | ]; |
||
50 | } |
||
51 | } |
||
52 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: