Completed
Pull Request — master (#19)
by Sullivan
03:10
created

DependsCommandWrapper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 54
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A dependsOutput() 0 21 2
1
<?php
2
3
namespace SLLH\ComposerVersionsCheck;
4
5
use Composer\Command\DependsCommand;
6
use Composer\Composer;
7
use Composer\IO\ConsoleIO;
8
use Composer\Package\PackageInterface;
9
use Symfony\Component\Console\Helper\HelperSet;
10
use Symfony\Component\Console\Input\ArrayInput;
11
use Symfony\Component\Console\Input\StringInput;
12
use Symfony\Component\Console\Output\BufferedOutput;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Depends command wrapper class to get needed output.
17
 *
18
 * @author Sullivan Senechal <[email protected]>
19
 */
20
final class DependsCommandWrapper
21
{
22
    /**
23
     * @var OutputInterface
24
     */
25
    private $output;
26
27
    /**
28
     * @var DependsCommand
29
     */
30
    private $dependsCommand;
31
32
    /**
33
     * @param Composer $composer
34
     * @param bool     $decoratedOutput
35
     */
36
    public function __construct(Composer $composer, $decoratedOutput = false)
37
    {
38
        $this->dependsCommand = new DependsCommand();
39
        $this->dependsCommand->setComposer($composer);
40
41
        $this->output = new BufferedOutput();
42
        $this->output->setDecorated($decoratedOutput);
43
44
        $this->dependsCommand->setIO(new ConsoleIO(new StringInput('depends'), $this->output, new HelperSet()));
45
    }
46
47
    /**
48
     * @param PackageInterface $package
49
     *
50
     * @return string[] The output, array of lines.
51
     */
52
    public function dependsOutput(PackageInterface $package)
53
    {
54
        $input = new ArrayInput(array(
55
            'depends',
56
            'package' => $package->getPrettyName(),
57
        ), $this->dependsCommand->getDefinition());
58
        $input->setOption('link-type', array('require', 'require-dev'));
0 ignored issues
show
Documentation introduced by
array('require', 'require-dev') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
        $this->dependsCommand->run($input, $this->output);
60
61
        $stringOutput = $this->output->fetch();
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 fetch() does only exist in the following implementations of said interface: Symfony\Component\Console\Output\BufferedOutput, Symfony\Component\Consol...ts\Fixtures\DummyOutput.

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...
62
        if (false === strstr($stringOutput, 'There is no installed package depending on ')) {
63
            $output = explode(PHP_EOL, $stringOutput);
64
            $output = array_map(function ($line) {
65
                return '    '.$line;
66
            }, $output);
67
68
            return array_slice($output, 0, -1);
69
        }
70
71
        return array();
72
    }
73
}
74