Completed
Pull Request — master (#8)
by Alexandre
02:49
created

ExemplifyCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 84
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 12 1
1
<?php
2
3
namespace RMiller\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
11
class ExemplifyCommand extends Command
12
{
13
    protected function configure()
14
    {
15
        $this
16
            ->setName('exemplify')
17
            ->setDefinition(array(
18
                    new InputArgument('class', InputArgument::REQUIRED, 'Class method belongs to'),
19
                    new InputArgument('method', InputArgument::REQUIRED, 'Method to describe'),
20
                ))
21
            ->setDescription('Adds an example for a method')
22
            ->addOption('confirm', null, InputOption::VALUE_NONE, 'Ask for confirmation before creating example')
23
            ->setHelp(<<<EOF
24
The <info>%command.name%</info> command creates an example for a method:
25
26
  <info>php %command.full_name% ClassName MethodName</info>
27
28
Will generate an example in the ClassNameSpec.
29
30
EOF
31
            )
32
        ;
33
    }
34
35
    /**
36
     * @param InputInterface  $input
37
     * @param OutputInterface $output
38
     *
39
     * @return int|null|void
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $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...
44
        $container->configure();
45
46
        $classname = $input->getArgument('class');
47
        $method = $input->getArgument('method');
48
49
        if (!$this->confirm($input, $classname, $method)) {
50
            return;
51
        }
52
53
        $resource = $container->get('locator.resource_manager')->createResource($classname);
54
55
        $container->get('code_generator')->generate($resource, 'specification_method', [
56
            'method' => $method,
57
            'type' => $this->confirmMethodType($output),
58
        ]);
59
    }
60
61
    /**
62
     * @param InputInterface $input
63
     * @param $classname
64
     * @param $method
65
     * @return bool
66
     */
67
    private function confirm(InputInterface $input, $classname, $method)
68
    {
69
        if (!$input->getOption('confirm')) {
70
            return true;
71
        }
72
73
        $question = sprintf('Do you want to generate an example for %s::%s? (Y/n)', $classname, $method);
74
        $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...
75
76
        return $io->askConfirmation($question, true);
77
    }
78
79
    /**
80
     * @param OutputInterface $output
81
     */
82
    private function confirmMethodType(OutputInterface $output)
83
    {
84
        $formattedMethodTypes = ['instance method','named constructor', 'static method'];
85
        $methodTypes = ['instance-method', 'named-constructor', 'static-method'];
86
87
        return $methodTypes[$this->getHelper('dialog')->select(
0 ignored issues
show
Bug introduced by
The method select() does not seem to exist on object<Symfony\Component...Helper\HelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
            $output,
89
            'Please select the method type (defaults to instance method)',
90
            $formattedMethodTypes,
91
            0
92
        )];
93
    }
94
}
95