Completed
Push — develop ( 71fd61...bbac44 )
by Jaap
06:03 queued 02:27
created

src/phpDocumentor/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
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @author    Mike van Riel <[email protected]>
8
 * @copyright 2010-2012 Mike van Riel / Naenius. (http://www.naenius.com)
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Command;
14
15
use Cilex\Provider\Console\Command as BaseCommand;
16
use Symfony\Component\Console\Helper\HelperSet;
17
use Symfony\Component\Console\Helper\ProgressHelper;
18
use Symfony\Component\Console\Input\InputInterface;
19
20
/**
21
 * Base command for phpDocumentor commands.
22
 *
23
 * Includes additional methods to forward the output to the logging events
24
 * of phpDocumentor.
25
 */
26
class Command extends BaseCommand
27
{
28
    /**
29
     * Registers the current command.
30
     *
31
     * @param HelperSet $helperSet
32
     */
33 1
    public function setHelperSet(HelperSet $helperSet)
34
    {
35 1
        parent::setHelperSet($helperSet);
36
37 1
        $this->getHelper('phpdocumentor_logger')->addOptions($this);
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 addOptions() does only exist in the following implementations of said interface: phpDocumentor\Command\Helper\LoggerHelper.

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...
38 1
    }
39
40
    /**
41
     * Returns the Progress bar helper.
42
     *
43
     * With this helper it is possible to display a progress bar and make it fill.
44
     *
45
     * @param InputInterface $input
46
     *
47
     * @return ProgressHelper
48
     */
49 2
    protected function getProgressBar(InputInterface $input)
50
    {
51 2
        if (!$input->getOption('progressbar')) {
52 1
            return null;
53
        }
54
55 1
        return $this->getHelperSet()->get('progress');
56
    }
57
}
58