Completed
Push — master ( e9ab60...4fa3cf )
by Sebastien
15:37 queued 05:47
created

CerbereConsoleListener::onCerberePostAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Cerbere\Event;
4
5
use Symfony\Component\Console\Helper\ProgressBar;
6
use Symfony\Component\Console\Output\ConsoleOutput;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10
/**
11
 * Class CerbereConsoleListener
12
 * @package Cerbere\Event
13
 */
14
class CerbereConsoleListener implements EventSubscriberInterface
15
{
16
    /**
17
     * @var OutputInterface
18
     */
19
    protected $output;
20
21
    /**
22
     * CerbereConsoleListener constructor.
23
     * @param \Symfony\Component\Console\Output\OutputInterface|null $output
24
     */
25 View Code Duplication
    public function __construct(OutputInterface $output = null)
0 ignored issues
show
Duplication introduced by
This method 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...
26
    {
27
        if (null === $output) {
28
            $output = new ConsoleOutput();
29
        }
30
31
        $this->output = $output;
32
    }
33
34
    /**
35
     * @return OutputInterface
36
     */
37
    public function getOutput()
38
    {
39
        return $this->output;
40
    }
41
42
    /**
43
     * @param OutputInterface $output
44
     */
45
    public function setOutput($output)
46
    {
47
        $this->output = $output;
48
    }
49
50
    /**
51
     * Returns an array of event names this subscriber wants to listen to.
52
     *
53
     * The array keys are event names and the value can be:
54
     *
55
     *  * The method name to call (priority defaults to 0)
56
     *  * An array composed of the method name to call and the priority
57
     *  * An array of arrays composed of the method names to call and respective
58
     *    priorities, or 0 if unset
59
     *
60
     * For instance:
61
     *
62
     *  * array('eventName' => 'methodName')
63
     *  * array('eventName' => array('methodName', $priority))
64
     *  * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
65
     *
66
     * @return array The event names to listen to
67
     */
68
    public static function getSubscribedEvents()
69
    {
70
        return array(
71
          CerbereEvents::CERBERE_FILE_DISCOVERED => array('onCerbereFileDiscovered', 0),
72
        );
73
    }
74
75
    /**
76
     * @param \Cerbere\Event\CerbereFileDiscoverEvent $event
77
     */
78
    public function onCerbereFileDiscovered(CerbereFileDiscoverEvent $event)
79
    {
80
        $this->output->getErrorOutput()->writeln($event->getFilename());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method getErrorOutput() does only exist in the following implementations of said interface: Psy\Output\ShellOutput, Symfony\Component\Console\Output\ConsoleOutput.

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...
81
    }
82
}
83