Completed
Push — master ( a439c5...b826d9 )
by Bill
05:53 queued 03:49
created

GitCommitCountAssessor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A assess() 0 12 2
A buildCommand() 0 12 2
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Assessors\GitCommitCount;
4
5
use Churn\Services\CommandService;
6
7
class GitCommitCountAssessor
8
{
9
    /**
10
     * The command service.
11
     * @var CommandService
12
     */
13
    protected $commandService;
14
15
    /**
16
     * Class constructor.
17
     * @param CommandService $commandService Command Service.
18
     */
19
    public function __construct(CommandService $commandService)
20
    {
21
        $this->commandService = $commandService;
22
    }
23
24
    /**
25
     * See how many commits the file at $filePath has.
26
     * @param  string $filePath Path and filename.
27
     * @return integer
28
     */
29
    public function assess($filePath): int
30
    {
31
        $command = $this->buildCommand($filePath);
32
33
        $result = $this->commandService->execute($command);
34
        if (! isset($result[0])) {
35
            return 0;
36
        }
37
        $result = trim($result[0]);
38
        $explodedResult = explode(' ', $result);
39
        return (integer) $explodedResult[0];
40
    }
41
42
    /**
43
     * Build the command to get the number of commits for the file at $filePath.
44
     * @param  string $filePath Patha nd filename.
45
     * @return string
46
     */
47
    protected function buildCommand($filePath): string
48
    {
49
        $commandTemplate = "git log --name-only --pretty=format: %s | sort | uniq -c | sort -nr";
50
51
        $pos =strrpos($filePath, '/');
52
        if ($pos) {
53
            $folder = substr($filePath, 0, $pos);
54
            $commandTemplate = "cd {$folder} && git log --name-only --pretty=format: %s | sort | uniq -c | sort -nr";
55
        }
56
57
        return sprintf($commandTemplate, $filePath, $filePath);
58
    }
59
}
60