CreateOAuthClientCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 39.44 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 0
cbo 5
dl 28
loc 71
ccs 0
cts 56
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 18 1
A execute() 0 16 1
B interact() 28 32 5

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
namespace AppBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class CreateOAuthClientCommand extends ContainerAwareCommand
11
{
12
    protected function configure()
13
    {
14
        $this
15
            ->setName('app:oauth-client:create')
16
            ->setDescription('Create OAuth Client.')
17
            ->addArgument('redirectUri', InputArgument::REQUIRED,                           'The redirect uri')
18
            ->addArgument('grantType',   InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The grant type')
19
            ->setHelp(<<<EOT
20
The <info>app:oauth:create</info> command creates a OAuth Client:
21
22
  <info>php app/console app:oauth:create</info>
23
24
This interactive shell will ask you for an client name, a redirect uri and then a grant type.
25
26
EOT
27
            );
28
        ;
29
    }
30
31
    protected function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        $redirectUri = $input->getArgument('redirectUri');
34
        $grantType   = $input->getArgument('grantType');
35
        $grantTypes  = explode(' ', $grantType);
36
37
        $clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
38
        $client = $clientManager->createClient();
39
        $client->setRedirectUris([$redirectUri]);
40
        $client->setAllowedGrantTypes($grantTypes);
41
        $clientManager->updateClient($client);
42
43
        $output->writeln(sprintf('Created OAuth Client'));
44
45
        return;
46
    }
47
48
    protected function interact(InputInterface $input, OutputInterface $output)
49
    {
50 View Code Duplication
        if (!$input->getArgument('redirectUri')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
            $redirectUri = $this->getHelper('dialog')->askAndValidate(
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 askAndValidate() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\DialogHelper.

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...
52
                $output,
53
                'Please choose an Redirect Uri:',
54
                function($redirectUri) {
55
                    if (empty($redirectUri)) {
56
                        throw new \Exception('Redirect Uri can not be empty');
57
                    }
58
59
                    return $redirectUri;
60
                }
61
            );
62
            $input->setArgument('redirectUri', $redirectUri);
63
        }
64
65 View Code Duplication
        if (!$input->getArgument('grantType')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
            $grantType = $this->getHelper('dialog')->askAndValidate(
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 askAndValidate() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\DialogHelper.

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...
67
                $output,
68
                'Please choose a Grant Types (separate multiple grant type with a space):',
69
                function($grantType) {
70
                    if (empty($grantType)) {
71
                        throw new \Exception('Grant Type can not be empty');
72
                    }
73
74
                    return $grantType;
75
                }
76
            );
77
            $input->setArgument('grantType', $grantType);
78
        }
79
    }
80
}
81