Completed
Pull Request — master (#82)
by Greg
02:18
created

OptionsHookDispatcher::getOptionHooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
4
5
use Symfony\Component\Console\Command\Command;
6
use Consolidation\AnnotatedCommand\AnnotationData;
7
use Consolidation\AnnotatedCommand\Hooks\HookManager;
8
use Consolidation\AnnotatedCommand\Hooks\OptionHookInterface;
9
10
/**
11
 * Call hooks
12
 */
13
class OptionsHookDispatcher extends HookDispatcher implements OptionHookInterface
14
{
15
    public function getOptions(
16
        Command $command,
17
        AnnotationData $annotationData
18
    ) {
19
        $optionHooks = $this->getOptionHooks($annotationData);
20
        foreach ($optionHooks as $optionHook) {
21
            $this->callOptionHook($optionHook, $command, $annotationData);
22
        }
23
        $commandInfoList = $this->hookManager->getHookOptionsForCommand($command);
24
        $command->optionsHookForHookAnnotations($commandInfoList);
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\Command\Command as the method optionsHookForHookAnnotations() does only exist in the following sub-classes of Symfony\Component\Console\Command\Command: Consolidation\AnnotatedCommand\AnnotatedCommand. 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...
25
    }
26
27
    protected function callOptionHook($optionHook, $command, AnnotationData $annotationData)
28
    {
29
        if ($optionHook instanceof OptionHookInterface) {
30
            return $optionHook->getOptions($command, $annotationData);
31
        }
32
        if (is_callable($optionHook)) {
33
            return $optionHook($command, $annotationData);
34
        }
35
    }
36
37
    protected function getOptionHooks(AnnotationData $annotationData)
38
    {
39
        return $this->getHooks(
40
            [
41
                HookManager::PRE_OPTION_HOOK,
42
                HookManager::OPTION_HOOK,
43
                HookManager::POST_OPTION_HOOK
44
            ],
45
            $annotationData
46
        );
47
    }
48
}
49