Completed
Push — master ( 5f4179...916f6e )
by Richard
11s
created

ExemplifyCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 4
dl 0
loc 86
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 21 1
A execute() 0 19 2
A confirm() 0 11 2
A confirmMethodType() 0 14 1
1
<?php
2
3
namespace RMiller\BehatSpec\Extension\ExemplifyExtension\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\ChoiceQuestion;
11
12
class ExemplifyCommand extends Command
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('exemplify')
18
            ->setDefinition(array(
19
                    new InputArgument('class', InputArgument::REQUIRED, 'Class method belongs to'),
20
                    new InputArgument('method', InputArgument::REQUIRED, 'Method to describe'),
21
                ))
22
            ->setDescription('Adds an example for a method')
23
            ->addOption('confirm', null, InputOption::VALUE_NONE, 'Ask for confirmation before creating example')
24
            ->setHelp(<<<EOF
25
The <info>%command.name%</info> command creates an example for a method:
26
27
  <info>php %command.full_name% ClassName MethodName</info>
28
29
Will generate an example in the ClassNameSpec.
30
31
EOF
32
            )
33
        ;
34
    }
35
36
    /**
37
     * @param InputInterface  $input
38
     * @param OutputInterface $output
39
     *
40
     * @return int|null|void
41
     */
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $container = $this->getApplication()->getContainer();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getContainer() does only exist in the following sub-classes of Symfony\Component\Console\Application: PhpSpec\Console\Application. 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...
45
        $container->configure();
46
47
        $classname = $input->getArgument('class');
48
        $method = $input->getArgument('method');
49
50
        if (!$this->confirm($input, $classname, $method)) {
51
            return;
52
        }
53
54
        $resource = $container->get('locator.resource_manager')->createResource($classname);
55
56
        $container->get('code_generator')->generate($resource, 'specification_method', [
57
            'method' => $method,
58
            'type' => $this->confirmMethodType($input, $output),
59
        ]);
60
    }
61
62
    /**
63
     * @param InputInterface $input
64
     * @param $classname
65
     * @param $method
66
     * @return bool
67
     */
68
    private function confirm(InputInterface $input, $classname, $method)
69
    {
70
        if (!$input->getOption('confirm')) {
71
            return true;
72
        }
73
74
        $question = sprintf('Do you want to generate an example for %s::%s? (Y/n)', $classname, $method);
75
        $io = $this->getApplication()->getContainer()->get('console.io');
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getContainer() does only exist in the following sub-classes of Symfony\Component\Console\Application: PhpSpec\Console\Application. 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...
76
77
        return $io->askConfirmation($question, true);
78
    }
79
80
    /**
81
     * @param OutputInterface $output
82
     */
83
    private function confirmMethodType(InputInterface $input, OutputInterface $output)
84
    {
85
        $formattedMethodTypes = ['instance method','named constructor', 'static method'];
86
87
        return str_replace(' ', '-', $this->getHelper('question')->ask(
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...
88
            $input,
89
            $output,
90
            new ChoiceQuestion(
91
                'Please select the method type (defaults to instance method)',
92
                $formattedMethodTypes,
93
                0
94
            )
95
        ));
96
    }
97
}
98