Completed
Pull Request — master (#62)
by Ayesh
05:40
created

VersionsCheckPlugin::uninstall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
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 static function getSubscribedEvents()
61
    {
62
        return array(
63
            PluginEvents::COMMAND => array(
64
                array('command'),
65
            ),
66
            ScriptEvents::POST_UPDATE_CMD => array(
67
                array('postUpdate', -100),
68
            ),
69
        );
70
    }
71
72
    /**
73
     * @param CommandEvent $event
74
     */
75
    public function command(CommandEvent $event)
76
    {
77
        $input = $event->getInput();
78
        $this->preferLowest = $input->hasOption('prefer-lowest') && true === $input->getOption('prefer-lowest');
79
    }
80
81
    /**
82
     * @param Event $event
83
     */
84
    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...
85
    {
86
        if (true === $this->preferLowest) {
87
            return;
88
        }
89
90
        $this->checkVersions($this->composer->getRepositoryManager(), $this->composer->getPackage());
91
    }
92
93
    /**
94
     * Tries to get plugin options and resolves them.
95
     *
96
     * @return array
97
     */
98
    private function resolveOptions()
99
    {
100
        $pluginConfig = $this->composer->getConfig()
101
            ? $this->composer->getConfig()->get('sllh-composer-versions-check')
102
            : null
103
        ;
104
105
        $options = array(
106
            'show-links' => false,
107
        );
108
109
        if (null === $pluginConfig) {
110
            return $options;
111
        }
112
113
        $options['show-links'] = isset($pluginConfig['show-links']) ? (bool) $pluginConfig['show-links'] : $options['show-links'];
114
115
        return $options;
116
    }
117
118
    /**
119
     * @param RepositoryManager    $repositoryManager
120
     * @param RootPackageInterface $rootPackage
121
     */
122
    private function checkVersions(RepositoryManager $repositoryManager, RootPackageInterface $rootPackage)
123
    {
124
        foreach ($repositoryManager->getRepositories() as $repository) {
125
            $this->versionsCheck->checkPackages(
126
                $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...
127
                $repositoryManager->getLocalRepository(),
128
                $rootPackage
129
            );
130
        }
131
132
        $this->io->write($this->versionsCheck->getOutput($this->options['show-links']), false);
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138
    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...
139
    {
140
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    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...
147
    {
148
149
    }
150
}
151