GitService   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 71
rs 10
c 1
b 0
f 0
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A isGitRepository() 0 3 1
A checkout() 0 3 1
A getCommitTimestamp() 0 3 1
A hasUncommittedChanges() 0 3 1
A __construct() 0 7 1
A getGitPath() 0 3 1
A hasUnstagedChanges() 0 3 1
A getCommits() 0 3 1
A isGitAvailable() 0 3 1
A updateIndex() 0 3 1
A isValidBranch() 0 3 1
A git() 0 6 1
1
<?php
2
3
namespace Bdelespierre\GitStats\Services;
4
5
use Bdelespierre\GitStats\Interfaces\ExecutableFinderInterface;
6
use Bdelespierre\GitStats\Interfaces\GitServiceInterface;
7
use Bdelespierre\GitStats\Interfaces\ProcessServiceInterface;
8
use Symfony\Component\Process\ExecutableFinder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Bdelespierre\GitStats\Services\ExecutableFinder. 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...
9
use Symfony\Component\Process\Process;
10
11
class GitService implements GitServiceInterface
12
{
13
    protected ProcessServiceInterface $process;
14
    protected ExecutableFinderInterface $finder;
15
    protected ?string $git;
16
17
    public function __construct(
18
        ProcessServiceInterface $process,
19
        ExecutableFinderInterface $finder
20
    ) {
21
        $this->process = $process;
22
        $this->finder = $finder;
23
        $this->git = $this->getGitPath();
24
    }
25
26
    private function git(array $command): Process
27
    {
28
        $process = $this->process->make([$this->git, ...$command]);
29
        $process->run();
30
31
        return $process;
32
    }
33
34
    private function getGitPath(): ?string
35
    {
36
        return $this->finder->find('git');
37
    }
38
39
    public function isGitAvailable(): bool
40
    {
41
        return $this->git != null;
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->git of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
42
    }
43
44
    public function isGitRepository(): bool
45
    {
46
        return $this->git(["rev-parse", "--git-dir"])->isSuccessful();
47
    }
48
49
    public function updateIndex(): bool
50
    {
51
        return $this->git(["update-index", "-q", "--ignore-submodules", "--refresh"])->isSuccessful();
52
    }
53
54
    public function hasUnstagedChanges(): bool
55
    {
56
        return ! $this->git(["diff-files", "--quiet", "--ignore-submodules", "--"])->isSuccessful();
57
    }
58
59
    public function hasUncommittedChanges(): bool
60
    {
61
        return ! $this->git(["diff-index", "--cached", "--quiet", "HEAD", "--ignore-submodules", "--"])->isSuccessful();
62
    }
63
64
    public function isValidBranch(string $branch): bool
65
    {
66
        return $this->git(["rev-parse", "--verify", $branch])->isSuccessful();
67
    }
68
69
    public function checkout(string $commit): bool
70
    {
71
        return $this->git(["checkout", $commit])->isSuccessful();
72
    }
73
74
    public function getCommits(string $branch): iterable
75
    {
76
        return array_filter(explode(PHP_EOL, $this->git(["rev-list", $branch], true)->getOutput()));
0 ignored issues
show
Unused Code introduced by
The call to Bdelespierre\GitStats\Services\GitService::git() has too many arguments starting with true. ( Ignorable by Annotation )

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

76
        return array_filter(explode(PHP_EOL, $this->/** @scrutinizer ignore-call */ git(["rev-list", $branch], true)->getOutput()));

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...
77
    }
78
79
    public function getCommitTimestamp(string $commit): int
80
    {
81
        return (int) trim($this->git(["show", "-s", "--format=%ct", $commit])->getOutput());
82
    }
83
}
84