Test Failed
Push — master ( dde0ed...e2b526 )
by Francesco
03:04
created

Command/AbstractCommand.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the MesCryptoBundle package.
5
 *
6
 * (c) Francesco Cartenì <http://www.multimediaexperiencestudio.it/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mes\Security\CryptoBundle\Command;
13
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\LogLevel;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Helper\FormatterHelper;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Logger\ConsoleLogger;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Style\StyleInterface;
22
use Symfony\Component\Console\Style\SymfonyStyle;
23
24
/**
25
 * Class AbstractCommand.
26
 */
27
abstract class AbstractCommand extends Command
28
{
29
    /**
30
     * @var StyleInterface
31
     */
32
    protected $symfonyStyle;
33
34
    /**
35
     * @param \Symfony\Component\Console\Output\OutputInterface $output
36
     * @param array|null                                        $verbosityMap
37
     *
38
     * @return LoggerInterface
39
     */
40 3
    protected function getLogger(OutputInterface $output, array $verbosityMap = null)
41
    {
42 3
        if (null === $verbosityMap) {
43
            $verbosityMap = array(
44 3
                LogLevel::INFO => OutputInterface::VERBOSITY_DEBUG,
45 3
            );
46 3
        }
47
48 3
        return new ConsoleLogger($output, $verbosityMap);
49
    }
50
51
    /**
52
     * @param InputInterface  $input
53
     * @param OutputInterface $output
54
     *
55
     * @return StyleInterface
56
     */
57 3
    protected function getStyle(InputInterface $input, OutputInterface $output)
58
    {
59 3
        if (null === $this->symfonyStyle) {
60 3
            $this->symfonyStyle = new SymfonyStyle($input, $output);
61 3
        }
62
63 3
        return $this->symfonyStyle;
64
    }
65
66
    /**
67
     * @param \Psr\Log\LoggerInterface $logger
68
     * @param $message
69
     * @param $level
70
     */
71 1
    protected function log(LoggerInterface $logger, $message, $level)
72
    {
73 1
        $logger->log($level, $message);
74 1
    }
75
76
    /**
77
     * @param $question
78
     * @param $default
79
     * @param string $sep
80
     *
81
     * @return string
82
     */
83 3
    protected function getQuestion($question, $default, $sep = ':')
84
    {
85 3
        $question = $default ? sprintf('<info>%s</info> [<comment>%s</comment>]%s ', $question, $default, $sep) : sprintf('<info>%s</info>%s ', $question, $sep);
86 3
        $question .= "\n > ";
87
88 3
        return $question;
89
    }
90
91
    /**
92
     * @param \Symfony\Component\Console\Output\OutputInterface $output
93
     * @param $text
94
     * @param string $style
95
     */
96 3
    protected function writeSection(OutputInterface $output, $text, $style = 'bg=blue;fg=white')
97
    {
98
        /*
99
         * @var FormatterHelper
100
         */
101 3
        $formatter = $this->getHelperSet()
102 3
                          ->get('formatter');
103
104 3
        $output->writeln(array(
105 3
            '',
106 3
            $formatter->formatBlock($text, $style, true),
107 3
            '',
108 3
        ));
109 3
    }
110
111
    /**
112
     * @param $text
113
     * @param $style
114
     *
115
     * @return string
116
     */
117 3
    protected function createBlock($text, $style)
118
    {
119 3
        return $this->getHelper('formatter')
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method formatBlock() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\FormatterHelper.

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...
120 3
                    ->formatBlock($text, $style, true);
121
    }
122
123
    /**
124
     * @param \Symfony\Component\Console\Output\OutputInterface $output
125
     * @param $options
126
     *
127
     * @return string
128
     */
129
    abstract protected function writeResults(OutputInterface $output, $options);
130
}
131