LintPlugin::command()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 5
nop 1
1
<?php
2
3
namespace SLLH\ComposerLint;
4
5
use Composer\Composer;
6
use Composer\EventDispatcher\EventSubscriberInterface;
7
use Composer\Factory;
8
use Composer\IO\IOInterface;
9
use Composer\Json\JsonFile;
10
use Composer\Plugin\CommandEvent;
11
use Composer\Plugin\PluginEvents;
12
use Composer\Plugin\PluginInterface;
13
14
/**
15
 * @author Sullivan Senechal <[email protected]>
16
 */
17
final class LintPlugin implements PluginInterface, EventSubscriberInterface
18
{
19
    /**
20
     * @var Composer
21
     */
22
    private $composer;
23
24
    /**
25
     * @var IOInterface
26
     */
27
    private $io;
28
29
    /**
30
     * @var Linter
31
     */
32
    private $linter;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function activate(Composer $composer, IOInterface $io)
38
    {
39
        $this->composer = $composer;
40
        $this->io = $io;
41
        $config = $this->composer->getConfig()->get('sllh-composer-lint');
42
        $this->linter = new Linter($config ?: array());
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    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...
49
    {
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    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...
56
    {
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public static function getSubscribedEvents()
63
    {
64
        return array(
65
            PluginEvents::COMMAND => array(
66
                array('command'),
67
            ),
68
        );
69
    }
70
71
    /**
72
     * @return bool true if no violation, false otherwise
73
     */
74
    public function command(CommandEvent $event)
75
    {
76
        if ('validate' !== $event->getCommandName()) {
77
            return true;
78
        }
79
80
        $file = $event->getInput()->getArgument('file') ?: Factory::getComposerFile();
81
        $json = new JsonFile($file);
82
        $manifest = $json->read();
83
84
        $errors = $this->linter->validate($manifest);
85
86
        foreach ($errors as $error) {
87
            $this->io->writeError(sprintf('<error>%s</error>', $error));
88
        }
89
90
        return empty($errors);
91
    }
92
}
93