Completed
Push — master ( 80fdf3...351fbc )
by Vladimir
02:43
created

src/Command/Command.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
namespace BZIon\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Helper\ProgressBar;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Process\Process;
10
11
class Command extends ContainerAwareCommand
12
{
13
    private $progress;
14
15
    /**
16
     * Initialise the progress bar
17
     * @param  OutputInterface $output The output
18
     * @param  int             $count  The number of steps
19
     * @return void
20
     */
21
    protected function initProgress($output, $count)
22
    {
23
        $this->progress = new ProgressBar($output, $count);
24
25
        $this->progress->setBarCharacter('<comment>=</comment>');
26
        $this->progress->start();
27
    }
28
29
    /**
30
     * Advance the progress bar a step forward
31
     * @return void
32
     */
33
    protected function advance()
34
    {
35
        if (!$this->progress) {
36
            return;
37
        }
38
39
        $this->progress->advance();
40
    }
41
42
    /**
43
     * Mark the progress bar as finished
44
     * @return void
45
     */
46
    protected function finishProgress()
47
    {
48
        if (!$this->progress) {
49
            return;
50
        }
51
52
        $this->progress->finish();
53
    }
54
55
    protected function clearCache($output)
56
    {
57
        $commandOutput = ($output->isVerbose()) ? $output : new NullOutput();
58
59
        foreach (array('cache:clear', 'cache:warmup') as $commandName) {
60
            $command = $this->getApplication()->find($commandName);
61
            $arguments = array('command' => $commandName);
62
            $input = new ArrayInput($arguments);
63
            $command->run($input, $commandOutput);
64
            $this->advance();
65
        }
66
    }
67
68
    /**
69
     * Return a function that can be used by Symfony's process to show the output
70
     * of a process live on our screen
71
     *
72
     * @param  OutputInterface $output The console output
73
     *
74
     * @return null|\Closure
75
     */
76
    protected function getBufferFunction($output)
77
    {
78
        if (!$output->isVerbose()) {
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method isVerbose() does only exist in the following implementations of said interface: BZIon\Command\NullOutput, Symfony\Component\Console\Output\BufferedOutput, Symfony\Component\Console\Output\ConsoleOutput, Symfony\Component\Console\Output\NullOutput, Symfony\Component\Console\Output\Output, Symfony\Component\Console\Output\StreamOutput, Symfony\Component\Consol...ts\Fixtures\DummyOutput, Symfony\Component\Console\Tests\Output\TestOutput.

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...
79
            return null;
80
        }
81
82
        return function ($type, $buffer) {
83
            if (Process::ERR === $type) {
84
                echo 'ERR > ' . $buffer;
85
            } else {
86
                echo 'OUT > ' . $buffer;
87
            }
88
        };
89
    }
90
}
91