Completed
Push — master ( 269c38...74a7a2 )
by Hugues
02:49
created

RequestPasswordResetHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
declare (strict_types = 1);
4
5
namespace HMLB\UserBundle\Handler;
6
7
use HMLB\DDD\Message\Command\Command;
8
use HMLB\DDD\Message\Command\Handler;
9
use HMLB\UserBundle\Command\RequestPasswordReset;
10
use HMLB\UserBundle\User\User;
11
use HMLB\UserBundle\User\UserRepository;
12
13
class RequestPasswordResetHandler implements Handler
14
{
15
    /**
16
     * @var UserRepository
17
     */
18
    private $userRepository;
19
20
    /**
21
     * @param UserRepository $userRepository
22
     */
23
    public function __construct(UserRepository $userRepository)
24
    {
25
        $this->userRepository = $userRepository;
26
    }
27
28
    /**
29
     * @param Command|RequestPasswordReset $command
30
     */
31
    public function handle(Command $command)
32
    {
33
        /** @var User $user */
34
        $user = $this->userRepository->get($command->getUserId());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class HMLB\DDD\Message\Command\Command as the method getUserId() does only exist in the following sub-classes of HMLB\DDD\Message\Command\Command: HMLB\UserBundle\Command\ChangeEmail, HMLB\UserBundle\Command\ChangePassword, HMLB\UserBundle\Command\ConfirmEmail, HMLB\UserBundle\Command\RequestEmailValidation, HMLB\UserBundle\Command\RequestPasswordReset, HMLB\UserBundle\Command\ResetPassword. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
35
        $user->requestPasswordReset();
36
    }
37
}
38