ChangePasswordCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 41.98 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 34
loc 81
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 23 1
A execute() 0 10 1
B interact() 34 34 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the awurth/silex-user package.
5
 *
6
 * (c) Alexis Wurth <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AWurth\Silex\User\Command;
13
14
use Exception;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\Question;
19
20
/**
21
 * ChangePasswordCommand.
22
 */
23
class ChangePasswordCommand extends ContainerAwareCommand
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('silex-user:change-password')
32
            ->setDescription('Change the password of a user.')
33
            ->setDefinition([
34
                new InputArgument('username', InputArgument::REQUIRED, 'The username'),
35
                new InputArgument('password', InputArgument::REQUIRED, 'The password')
36
            ])
37
            ->setHelp(<<<'EOT'
38
The <info>silex-user:change-password</info> command changes the password of a user:
39
40
  <info>php %command.full_name% matthieu</info>
41
42
This interactive shell will first ask you for a password.
43
44
You can alternatively specify the password as a second argument:
45
46
  <info>php %command.full_name% matthieu mypassword</info>
47
48
EOT
49
            );
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $username = $input->getArgument('username');
58
        $password = $input->getArgument('password');
59
60
        $manipulator = $this->container['silex_user.util.user_manipulator'];
61
        $manipulator->changePassword($username, $password);
62
63
        $output->writeln(sprintf('Changed password for user <comment>%s</comment>', $username));
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 View Code Duplication
    protected function interact(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $questions = [];
72
73
        if (!$input->getArgument('username')) {
74
            $question = new Question('Please give the username:');
75
            $question->setValidator(function ($username) {
76
                if (empty($username)) {
77
                    throw new Exception('Username can not be empty');
78
                }
79
80
                return $username;
81
            });
82
            $questions['username'] = $question;
83
        }
84
85
        if (!$input->getArgument('password')) {
86
            $question = new Question('Please enter the new password:');
87
            $question->setValidator(function ($password) {
88
                if (empty($password)) {
89
                    throw new Exception('Password can not be empty');
90
                }
91
92
                return $password;
93
            });
94
            $question->setHidden(true);
95
            $questions['password'] = $question;
96
        }
97
98
        foreach ($questions as $name => $question) {
99
            $answer = $this->getHelper('question')->ask($input, $output, $question);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method ask() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

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...
100
            $input->setArgument($name, $answer);
101
        }
102
    }
103
}
104