Completed
Push — master ( 0c9b9b...10ad9a )
by Sullivan
01:46 queued 19s
created

VersionsCheckPlugin::uninstall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace SLLH\ComposerVersionsCheck;
4
5
use Composer\Composer;
6
use Composer\EventDispatcher\EventSubscriberInterface;
7
use Composer\IO\IOInterface;
8
use Composer\Package\RootPackageInterface;
9
use Composer\Plugin\CommandEvent;
10
use Composer\Plugin\PluginEvents;
11
use Composer\Plugin\PluginInterface;
12
use Composer\Repository\RepositoryManager;
13
use Composer\Script\Event;
14
use Composer\Script\ScriptEvents;
15
16
/**
17
 * @author Sullivan Senechal <[email protected]>
18
 */
19
final class VersionsCheckPlugin implements PluginInterface, EventSubscriberInterface
20
{
21
    /**
22
     * @var Composer
23
     */
24
    private $composer;
25
26
    /**
27
     * @var IOInterface
28
     */
29
    private $io;
30
31
    /**
32
     * @var VersionsCheck
33
     */
34
    private $versionsCheck;
35
36
    /**
37
     * @var bool
38
     */
39
    private $preferLowest;
40
41
    /**
42
     * @var array
43
     */
44
    private $options = array();
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function activate(Composer $composer, IOInterface $io)
50
    {
51
        $this->composer = $composer;
52
        $this->io = $io;
53
        $this->versionsCheck = new VersionsCheck();
54
        $this->options = $this->resolveOptions();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function deactivate(Composer $composer, IOInterface $io)
0 ignored issues
show
Unused Code introduced by
The parameter $composer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $io is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function uninstall(Composer $composer, IOInterface $io)
0 ignored issues
show
Unused Code introduced by
The parameter $composer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $io is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
    {
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public static function getSubscribedEvents()
75
    {
76
        return array(
77
            PluginEvents::COMMAND => array(
78
                array('command'),
79
            ),
80
            ScriptEvents::POST_UPDATE_CMD => array(
81
                array('postUpdate', -100),
82
            ),
83
        );
84
    }
85
86
    public function command(CommandEvent $event)
87
    {
88
        $input = $event->getInput();
89
        $this->preferLowest = $input->hasOption('prefer-lowest') && true === $input->getOption('prefer-lowest');
90
    }
91
92
    public function postUpdate(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        if (true === $this->preferLowest) {
95
            return;
96
        }
97
98
        $this->checkVersions($this->composer->getRepositoryManager(), $this->composer->getPackage());
99
    }
100
101
    /**
102
     * Tries to get plugin options and resolves them.
103
     *
104
     * @return array
105
     */
106
    private function resolveOptions()
107
    {
108
        $pluginConfig = $this->composer->getConfig()
109
            ? $this->composer->getConfig()->get('sllh-composer-versions-check')
110
            : null
111
        ;
112
113
        $options = array(
114
            'show-links' => false,
115
        );
116
117
        if (null === $pluginConfig) {
118
            return $options;
119
        }
120
121
        $options['show-links'] = isset($pluginConfig['show-links']) ? (bool) $pluginConfig['show-links'] : $options['show-links'];
122
123
        return $options;
124
    }
125
126
    private function checkVersions(RepositoryManager $repositoryManager, RootPackageInterface $rootPackage)
127
    {
128
        foreach ($repositoryManager->getRepositories() as $repository) {
129
            $this->versionsCheck->checkPackages(
130
                $repository,
0 ignored issues
show
Compatibility introduced by
$repository of type object<Composer\Repository\RepositoryInterface> is not a sub-type of object<Composer\Repository\ArrayRepository>. It seems like you assume a concrete implementation of the interface Composer\Repository\RepositoryInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
131
                $repositoryManager->getLocalRepository(),
132
                $rootPackage
133
            );
134
        }
135
136
        $this->io->write($this->versionsCheck->getOutput($this->options['show-links']), false);
137
    }
138
}
139