Completed
Push — master ( ef682c...1e33b0 )
by Pieter
02:53
created

Repository   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 23.3 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 11
dl 24
loc 103
ccs 0
cts 73
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createBare() 12 12 2
A create() 12 12 2
A clone() 0 17 2
A getPath() 0 4 1
A getBranches() 0 16 3
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
    private $name;
19
20
    public function __construct(Client $client, Directory $directory, string $name)
21
    {
22
        $this->client    = $client;
23
        $this->directory = $directory;
24
        $this->name      = $name;
25
    }
26
27 View Code Duplication
    public static function createBare(Client $client, Directory $directory, string $name): Repository
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...
28
    {
29
        $repositoryDirectory = $directory->create($directory->getPath() . '/' . $name);
30
31
        $result = $client->run($repositoryDirectory, new PlainArgument('init'), new LongFlag('bare'));
32
33
        if (!$result->isSuccess()) {
34
            throw new Exception($result->getErrorMessage());
35
        }
36
37
        return new self($client, $directory, $name);
38
    }
39
40 View Code Duplication
    public static function create(Client $client, Directory $directory, string $name): Repository
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...
41
    {
42
        $repositoryDirectory = $directory->create($directory->getPath() . '/' . $name);
43
44
        $result = $client->run($repositoryDirectory, new PlainArgument('init'));
45
46
        if (!$result->isSuccess()) {
47
            throw new Exception($result->getErrorMessage());
48
        }
49
50
        return new self($client, $directory, $name);
51
    }
52
53
    public function clone(Directory $directory, string $name): Repository
54
    {
55
        $repositoryDirectory = $directory->create($directory->getPath() . '/' . $name);
56
57
        $result = $this->client->run(
58
            $repositoryDirectory,
59
            new PlainArgument('clone'),
60
            new PlainArgument($this->getPath()),
61
            new PlainArgument($repositoryDirectory->getPath())
62
        );
63
64
        if (!$result->isSuccess()) {
65
            throw new Exception($result->getErrorMessage());
66
        }
67
68
        return new self($this->client, $directory, $name);
69
    }
70
71
    public function getPath(): string
72
    {
73
        return $this->directory->getPath() . '/' . $this->name;
74
    }
75
76
    public function getBranches(): Branches
77
    {
78
        $result = $this->client->run($this->getPath(), 'branch');
79
80
        if (!$result->isSuccess()) {
81
            throw new Exception('Cannot list the branches.');
82
        }
83
84
        $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...
85
86
        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...
87
            $branches->add(new Branch($branch));
88
        }
89
90
        return $branches;
91
    }
92
93
    public function add(File $file)
94
    {
95
        $this->client->run($this->directory, new PlainArgument('add'), new PlainArgument($file->getPath()));
96
    }
97
98
    public function commit(Author $author, string $message)
99
    {
100
        $this->client->run(
101
            $this->directory,
102
            new PlainArgument('commit'),
103
            new ShortFlag('m'),
104
            new PlainArgument($message),
105
            new LongFlag('author'),
106
            new PlainArgument($author->getFormatted())
107
        );
108
    }
109
110
    public function push()
111
    {
112
        $this->client->run($this->directory, new PlainArgument('push'));
113
    }
114
}
115