Completed
Push — master ( 82bcf8...197f8d )
by Alexis
03:41
created

ActivateUserCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 54
loc 54
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 15 15 1
A execute() 9 9 1
A interact() 16 16 3

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\SilexUser\Command;
13
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\Question;
18
19
/**
20
 * @author Antoine Hérault <[email protected]>
21
 */
22 View Code Duplication
class ActivateUserCommand extends ContainerAwareCommand
0 ignored issues
show
Duplication introduced by
This class 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...
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function configure()
28
    {
29
        $this
30
            ->setName('silex-user:activate')
31
            ->setDescription('Activate a user')
32
            ->setDefinition([
33
                new InputArgument('username', InputArgument::REQUIRED, 'The username')
34
            ])
35
            ->setHelp(<<<'EOT'
36
The <info>silex-user:activate</info> command activates a user (so they will be able to log in):
37
38
  <info>php %command.full_name% matthieu</info>
39
EOT
40
            );
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        $username = $input->getArgument('username');
49
50
        $manipulator = $this->getContainer()->get('silex_user.util.user_manipulator');
51
        $manipulator->activate($username);
52
53
        $output->writeln(sprintf('User "%s" has been activated.', $username));
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function interact(InputInterface $input, OutputInterface $output)
60
    {
61
        if (!$input->getArgument('username')) {
62
            $question = new Question('Please choose a username:');
63
            $question->setValidator(function ($username) {
64
                if (empty($username)) {
65
                    throw new \Exception('Username can not be empty');
66
                }
67
68
                return $username;
69
            });
70
            $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...
71
72
            $input->setArgument('username', $answer);
73
        }
74
    }
75
}
76