SubsplitCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 89
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A publish() 0 17 2
A runCommand() 0 4 1
B execute() 0 37 3
A configure() 0 4 1
A handleProcessRun() 0 7 2
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\Core\Command;
15
16
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Dotfiles\Core\Command\Command. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Process\Process;
20
21
/**
22
 * Class SubsplitCommand.
23
 *
24
 * @codeCoverageIgnore
25
 */
26
class SubsplitCommand extends Command
27
{
28
    public const SOURCE = '[email protected]:kilip/dotfiles.git';
29
30
    /**
31
     * @var OutputInterface
32
     */
33
    private $output;
34
35
    private $workdir;
36
37
    public function handleProcessRun($type, $buffer): void
38
    {
39
        $contents = '<info>output:</info> '.$buffer;
40
        if (Process::ERR == $type) {
41
            $contents = '<error>error:</error> '.$buffer;
42
        }
43
        $this->output->write($contents);
44
    }
45
46
    protected function configure(): void
47
    {
48
        $this->setName('subsplit');
49
        $this->workdir = realpath(__DIR__.'/../../../');
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output): void
53
    {
54
        $this->output = $output;
55
        $workdir = $this->workdir;
56
57
        if (!is_dir($dir = $workdir.'/.subsplit')) {
58
            $this->runCommand('git subsplit --debug init '.static::SOURCE);
59
        } else {
60
            $this->runCommand('git subsplit --debug update '.static::SOURCE);
61
        }
62
63
        $tree = array(
64
            'core' => array(
65
                'path' => 'src/Core',
66
                'repo' => '[email protected]:dotfilesphp/core.git',
67
            ),
68
            'bash' => array(
69
                'path' => 'src/Plugins/Bash',
70
                'repo' => '[email protected]:dotfilesphp/bash-plugin.git',
71
            ),
72
            'phpbrew' => array(
73
                'path' => 'src/Plugins/PHPBrew',
74
                'repo' => '[email protected]:dotfilesphp/phpbrew-plugin.git',
75
            ),
76
            'composer' => array(
77
                'path' => 'src/Plugins/Composer',
78
                'repo' => '[email protected]:dotfilesphp/composer-plugin.git',
79
            ),
80
            'phpcsfixer' => array(
81
                'path' => 'src/Plugins/PHPCSFixer',
82
                'repo' => '[email protected]:dotfilesphp/phpcsfixer-plugin.git',
83
            ),
84
        );
85
86
        foreach ($tree as $name => $config) {
87
            $this->output->writeln("processing <comment>$name</comment>");
88
            $this->publish($config['path'], $config['repo']);
89
        }
90
    }
91
92
    private function publish($path, $repo, $heads = 'master', $tag = null): void
93
    {
94
        $command = array(
95
            'git',
96
            'subsplit',
97
            'publish',
98
            '--heads='.$heads,
99
        );
100
        if (null !== $tag) {
101
            $command[] = '--tags='.$tag;
102
        }
103
104
        $command[] = $path.':'.$repo;
105
106
        $command = implode(' ', $command);
107
        //$this->output->writeln("<comment>$command</comment>");
108
        $this->runCommand($command);
109
    }
110
111
    private function runCommand($command): void
112
    {
113
        $process = new Process($command, $this->workdir);
114
        $process->run(array($this, 'handleProcessRun'));
115
    }
116
}
117