Completed
Pull Request — master (#60)
by
unknown
01:29
created

VersionsCheckPlugin   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 0
loc 152
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A activate() 0 14 3
A getSubscribedEvents() 0 11 1
A command() 0 9 3
A postUpdate() 0 8 3
A resolveOptions() 0 23 5
A checkVersions() 0 16 3
A disable() 0 4 1
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
    /** @var boolean */
47
    private $disabled = false;
48
    
49
    /**
50
     * @var array
51
     */
52
    private $classes = array(
53
        "SLLH\ComposerVersionsCheck\VersionsCheckPlugin",
54
        "SLLH\ComposerVersionsCheck\VersionsCheck",
55
        "SLLH\ComposerVersionsCheck\OutdatedPackage"
56
    );
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function activate(Composer $composer, IOInterface $io)
62
    {
63
        // guard for self-update problem
64
        foreach ($this->classes as $class) {
65
            if (!class_exists($class)) {
66
                return $this->disable();
67
            }
68
        }
69
70
        $this->composer = $composer;
71
        $this->io = $io;
72
        $this->versionsCheck = new VersionsCheck();
73
        $this->options = $this->resolveOptions();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->resolveOptions() can be null. However, the property $options is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public static function getSubscribedEvents()
80
    {
81
        return array(
82
            PluginEvents::COMMAND => array(
83
                array('command'),
84
            ),
85
            ScriptEvents::POST_UPDATE_CMD => array(
86
                array('postUpdate', -100),
87
            ),
88
        );
89
    }
90
91
    /**
92
     * @param CommandEvent $event
93
     */
94
    public function command(CommandEvent $event)
95
    {
96
        if ($this->disabled) {
97
            return;
98
        }
99
        
100
        $input = $event->getInput();
101
        $this->preferLowest = $input->hasOption('prefer-lowest') && true === $input->getOption('prefer-lowest');
102
    }
103
104
    /**
105
     * @param Event $event
106
     */
107
    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...
108
    {
109
        if ($this->disabled || true === $this->preferLowest) {
110
            return;
111
        }
112
113
        $this->checkVersions($this->composer->getRepositoryManager(), $this->composer->getPackage());
114
    }
115
116
    /**
117
     * Tries to get plugin options and resolves them.
118
     *
119
     * @return array
120
     */
121
    private function resolveOptions()
122
    {
123
        if ($this->disabled) {
124
            return;
125
        }
126
127
        $pluginConfig = $this->composer->getConfig()
128
            ? $this->composer->getConfig()->get('sllh-composer-versions-check')
129
            : null
130
        ;
131
132
        $options = array(
133
            'show-links' => false,
134
        );
135
136
        if (null === $pluginConfig) {
137
            return $options;
138
        }
139
140
        $options['show-links'] = isset($pluginConfig['show-links']) ? (bool) $pluginConfig['show-links'] : $options['show-links'];
141
142
        return $options;
143
    }
144
145
    /**
146
     * @param RepositoryManager    $repositoryManager
147
     * @param RootPackageInterface $rootPackage
148
     */
149
    private function checkVersions(RepositoryManager $repositoryManager, RootPackageInterface $rootPackage)
150
    {
151
        if ($this->disabled) {
152
            return;
153
        }
154
        
155
        foreach ($repositoryManager->getRepositories() as $repository) {
156
            $this->versionsCheck->checkPackages(
157
                $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...
158
                $repositoryManager->getLocalRepository(),
159
                $rootPackage
160
            );
161
        }
162
163
        $this->io->write($this->versionsCheck->getOutput($this->options['show-links']), false);
164
    }
165
    
166
    public function disable()
167
    {
168
        $this->disabled = true;
169
    }
170
}