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

Command   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 10
c 0
b 0
f 0
ccs 8
cts 8
cp 1
wmc 3
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setHelperSet() 0 6 1
A getProgressBar() 0 8 2
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
Bug introduced by
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