Config   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 112
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 16
loc 112
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A get() 8 8 1
A set() 0 8 1
A unSet() 8 8 1
C getScopeFlags() 0 29 7
A getGitExecutable() 0 4 1
A getGitConfigFile() 0 8 2
A executeCommand() 0 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AurimasNiekis\GitConfig;
4
5
use InvalidArgumentException;
6
use AurimasNiekis\GitConfig\Exception\GitNotFoundException;
7
8
/**
9
 * Class Config
10
 *
11
 * @package AurimasNiekis\GitConfig
12
 * @author  Aurimas Niekis <[email protected]>
13
 */
14
class Config
15
{
16
    /**
17
     * @var string
18
     */
19
    private $gitExecutable;
20
21
    /**
22
     * @var string
23
     */
24
    private $gitConfigFile;
25
26 8
    public function __construct(string $gitExecutable = null, string $gitConfigFile = null)
27
    {
28 8
        if (null === $gitExecutable) {
29 7
            $exitCode = rtrim(shell_exec('which git > /dev/null; echo $?'));
30
31 7
            if ('0' !== $exitCode) {
32 1
                throw new GitNotFoundException();
33
            }
34
        } else {
35 1
            $this->gitExecutable = $gitExecutable;
36
        }
37
38 7
        $this->gitConfigFile = $gitConfigFile;
39 7
    }
40
41 7 View Code Duplication
    public function get(string $name, bool $global = false, bool $system = false, bool $allScopes = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43 7
        $command = $this->getScopeFlags($global, $system, $allScopes);
44
45 4
        $command .= ' --get ' . $name;
46
47 4
        return $this->executeCommand($command);
48
    }
49
50 2
    public function set(string $name, string $value, bool $global = false, bool $system = false)
51
    {
52 2
        $command = $this->getScopeFlags($global, $system);
53
54 2
        $command .= ' ' . $name . ' "' . $value . '"';
55
56 2
        return $this->executeCommand($command);
57
    }
58
59 1 View Code Duplication
    public function unSet(string $name, bool $global = false, bool $system = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61 1
        $command = $this->getScopeFlags($global, $system);
62
63 1
        $command .= ' --unset ' . $name;
64
65 1
        return $this->executeCommand($command);
66
    }
67
68 7
    private function getScopeFlags(bool $global = false, bool $system = false, bool $allScopes = null)
69
    {
70 7
        $command = '';
71
72 7
        $flags = 0;
73 7
        if (true === $global) {
74 1
            $flags++;
75
76 1
            $command .= ' --global';
77
        }
78
79 7
        if (true === $system) {
80 1
            $flags++;
81
82 1
            $command .= ' --system';
83
        }
84
85 7
        if (true === $allScopes) {
86 6
            $flags++;
87
        }
88
89 7
        if (1 < $flags || (null !== $allScopes && 0 === $flags)) {
90 3
            throw new InvalidArgumentException(
91 3
                'Only $global, $system or $allScopes could be "true" at same time'
92
            );
93
        }
94
95 4
        return $command;
96
    }
97
98 4
    private function getGitExecutable(): string
99
    {
100 4
        return $this->gitExecutable ?? 'git';
101
    }
102
103 4
    private function getGitConfigFile(): string
104
    {
105 4
        if (null !== $this->gitConfigFile) {
106 3
            return ' --file ' . $this->gitConfigFile;
107
        }
108
109 1
        return '';
110
    }
111
112 4
    private function executeCommand(string $command): string
113
    {
114 4
        return rtrim(
115
            shell_exec(
116
                sprintf(
117 4
                    '%s config%s%s',
118 4
                    $this->getGitExecutable(),
119 4
                    $this->getGitConfigFile(),
120
                    $command
121
                )
122
            )
123
        );
124
    }
125
}