Completed
Pull Request — master (#59)
by
unknown
05:54
created

VersionsCheckPlugin::disable()   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 0
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
     * {@inheritdoc}
51
     */
52
    public function activate(Composer $composer, IOInterface $io)
53
    {
54
        // guard for self-update problem
55
        if (__CLASS__ !== 'SLLH\ComposerVersionsCheck\OutdatedPackage\VersionsCheckPlugin') {
56
            return $this->disable();
57
        }
58
59
        $this->composer = $composer;
60
        $this->io = $io;
61
        $this->versionsCheck = new VersionsCheck();
62
        $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...
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public static function getSubscribedEvents()
69
    {
70
        return array(
71
            PluginEvents::COMMAND => array(
72
                array('command'),
73
            ),
74
            ScriptEvents::POST_UPDATE_CMD => array(
75
                array('postUpdate', -100),
76
            ),
77
        );
78
    }
79
80
    /**
81
     * @param CommandEvent $event
82
     */
83
    public function command(CommandEvent $event)
84
    {
85
        $input = $event->getInput();
86
        $this->preferLowest = $input->hasOption('prefer-lowest') && true === $input->getOption('prefer-lowest');
87
    }
88
89
    /**
90
     * @param Event $event
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 ($this->disabled || 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
        if ($this->disabled) {
109
            return;
110
        }
111
112
        $pluginConfig = $this->composer->getConfig()
113
            ? $this->composer->getConfig()->get('sllh-composer-versions-check')
114
            : null
115
        ;
116
117
        $options = array(
118
            'show-links' => false,
119
        );
120
121
        if (null === $pluginConfig) {
122
            return $options;
123
        }
124
125
        $options['show-links'] = isset($pluginConfig['show-links']) ? (bool) $pluginConfig['show-links'] : $options['show-links'];
126
127
        return $options;
128
    }
129
130
    /**
131
     * @param RepositoryManager    $repositoryManager
132
     * @param RootPackageInterface $rootPackage
133
     */
134
    private function checkVersions(RepositoryManager $repositoryManager, RootPackageInterface $rootPackage)
135
    {
136
        if ($this->disabled) {
137
            return;
138
        }
139
        
140
        foreach ($repositoryManager->getRepositories() as $repository) {
141
            $this->versionsCheck->checkPackages(
142
                $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...
143
                $repositoryManager->getLocalRepository(),
144
                $rootPackage
145
            );
146
        }
147
148
        $this->io->write($this->versionsCheck->getOutput($this->options['show-links']), false);
149
    }
150
    
151
    public function disable()
152
    {
153
        $this->disabled = true;
154
    }
155
}