Completed
Push — master ( 3a2809...fa6500 )
by Pieter
03:01
created

Repository   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 116
Duplicated Lines 18.1 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 54.9%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 10
dl 21
loc 116
ccs 28
cts 51
cp 0.549
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createBare() 0 6 1
A create() 0 6 1
A clone() 0 11 1
A getPath() 0 4 1
A getActiveBranch() 0 14 4
A getBranches() 0 12 2
A createBranch() 11 11 1
A checkout() 10 10 1
A add() 0 4 1
A commit() 0 11 1
A push() 0 4 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 declare(strict_types=1);
2
3
namespace Gitilicious\GitClient\Git;
4
5
use Gitilicious\GitClient\Cli\Input\LongFlag;
6
use Gitilicious\GitClient\Cli\Input\PlainArgument;
7
use Gitilicious\GitClient\Cli\Input\ShortFlag;
8
use Gitilicious\GitClient\Client;
9
use Gitilicious\GitClient\FileSystem\Directory;
10
use Gitilicious\GitClient\FileSystem\File;
11
12
class Repository
13
{
14
    private $client;
15
16
    private $directory;
17
18
    public function __construct(Client $client, Directory $directory)
19
    {
20 10
        $this->client    = $client;
21
        $this->directory = $directory;
22 10
    }
23 10
24 10
    public static function createBare(Client $client, Directory $directory): Repository
25 10
    {
26
        $client->run($directory, new PlainArgument('init'), new LongFlag('bare'));
27 4
28
        return new self($client, $directory);
29 4
    }
30
31 4
    public static function create(Client $client, Directory $directory): Repository
32
    {
33 4
        $client->run($directory, new PlainArgument('init'));
34 1
35
        return new self($client, $directory);
36
    }
37 3
38
    public function clone(Directory $directory): Repository
39
    {
40 8
        $this->client->run(
41
            $directory,
42 8
            new PlainArgument('clone'),
43
            new PlainArgument($this->directory->getPath()),
44 8
            new PlainArgument($directory->getPath())
45
        );
46 8
47 1
        return new self($this->client, $directory);
48
    }
49
50 7
    public function getPath(): string
51
    {
52
        return $this->directory->getPath();
53 3
    }
54
55 3
    public function getActiveBranch(): Branch
56
    {
57 3
        $branches = $this->getBranches();
58
59 3
        if (!count($branches)) {
60 3
            return new Branch('* master');
61 3
        }
62
63
        foreach ($branches as $branch) {
64 3
            if ($branch->isActive()) {
65 1
                return $branch;
66
            }
67
        }
68 2
    }
69
70
    public function getBranches(): Branches
71 4
    {
72
        $result = $this->client->run($this->directory, new PlainArgument('branch'));
73 4
74
        $branches = new Branches($this->client);
0 ignored issues
show
Unused Code introduced by
The call to Branches::__construct() has too many arguments starting with $this->client.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
75
76
        foreach ($result->getOutput() as $branch) {
0 ignored issues
show
Bug introduced by
The expression $result->getOutput() of type object<Gitilicious\GitClient\Cli\Output\Output> is not traversable.
Loading history...
77
            $branches->add(new Branch($branch));
78
        }
79
80
        return $branches;
81
    }
82
83 View Code Duplication
    public function createBranch(string $name): Branch
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...
84
    {
85
        $this->client->run(
86
            $this->directory,
87
            new PlainArgument('checkout'),
88
            new ShortFlag('b'),
89
            new PlainArgument($name)
90
        );
91
92
        return new Branch($name);
93
    }
94
95 View Code Duplication
    public function checkout(string $name): Branch
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...
96
    {
97
        $this->client->run(
98
            $this->directory,
99
            new PlainArgument('checkout'),
100
            new PlainArgument($name)
101
        );
102
103
        return new Branch('* ' . $name);
104
    }
105
106
    public function add(File $file)
107
    {
108
        $this->client->run($this->directory, new PlainArgument('add'), new PlainArgument($file->getPath()));
109
    }
110
111
    public function commit(Author $author, string $message)
112
    {
113
        $this->client->run(
114
            $this->directory,
115
            new PlainArgument('commit'),
116
            new ShortFlag('m'),
117
            new PlainArgument($message),
118
            new LongFlag('author'),
119
            new PlainArgument($author->getFormatted())
120
        );
121
    }
122
123
    public function push()
124
    {
125
        $this->client->run($this->directory, new PlainArgument('push'));
126
    }
127
}
128