EventSubscriber   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 4 1
A __construct() 0 6 1
B onInstallEvent() 0 25 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Plugins\PHPCSFixer;
15
16
use Dotfiles\Core\Config\Config;
17
use Dotfiles\Core\Event\PatchEvent;
18
use Dotfiles\Core\Util\Downloader;
19
use Dotfiles\Core\Util\Filesystem;
20
use Psr\Log\LoggerInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23
24
class EventSubscriber implements EventSubscriberInterface
25
{
26
    public const URL = 'http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar';
27
28
    /**
29
     * @var Config
30
     */
31
    private $config;
32
33
    /**
34
     * @var Downloader
35
     */
36
    private $downloader;
37
38
    /**
39
     * @var LoggerInterface
40
     */
41
    private $logger;
42
43
    /**
44
     * @var OutputInterface
45
     */
46
    private $output;
47
48
    public function __construct(Config $config, Downloader $downloader, OutputInterface $output, LoggerInterface $logger)
49
    {
50
        $this->config = $config;
51
        $this->downloader = $downloader;
52
        $this->output = $output;
53
        $this->logger = $logger;
54
    }
55
56
    public static function getSubscribedEvents()
57
    {
58
        return array(
59
            'dotfiles.install' => 'onInstallEvent',
60
        );
61
    }
62
63
    public function onInstallEvent(PatchEvent $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
    public function onInstallEvent(/** @scrutinizer ignore-unused */ PatchEvent $event): void

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

Loading history...
64
    {
65
        $config = $this->config;
66
        $downloader = $this->downloader;
67
        $dryRun = $config->get('dotfiles.dry_run');
68
        $tempDir = $config->get('dotfiles.temp_dir');
69
        $targetFile = $tempDir.'/phpcs/php-cs-fixer.phar';
70
        $installDir = $config->get('dotfiles.bin_dir');
71
        $installFile = $installDir.DIRECTORY_SEPARATOR.$config->get('phpcs.file_name');
72
73
        if (is_file($installFile)) {
74
            $this->output->writeln('PHP-CS-Fixer already installed, skipping');
75
76
            return;
77
        }
78
79
        if (!is_file($targetFile)) {
80
            $downloader->run(static::URL, $targetFile, $dryRun);
0 ignored issues
show
Unused Code introduced by
The call to Dotfiles\Core\Util\Downloader::run() has too many arguments starting with $dryRun. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
            $downloader->/** @scrutinizer ignore-call */ 
81
                         run(static::URL, $targetFile, $dryRun);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
81
        }
82
83
        if (is_file($targetFile)) {
84
            $fs = new Filesystem();
85
            $fs->copy($targetFile, $installFile);
86
            $fs->chmod($installFile, 0755);
87
            $this->output->writeln('PHP-CS-Fixer installed to: <comment>'.$installFile.'</comment>');
88
        }
89
    }
90
}
91