Passed
Push — master ( 5dd2f3...e80b48 )
by Théo
02:17
created

PharDiff   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 38
dl 0
loc 81
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A gnuDiff() 0 20 2
A __construct() 0 18 2
A gitDiff() 0 20 2
A listChecksums() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\PharInfo;
16
17
use function array_map;
18
use function escapeshellarg;
19
use ParagonIE\Pharaoh\PharDiff as ParagoniePharDiff;
20
use function realpath;
21
use function str_replace;
22
23
final class PharDiff
24
{
25
    /** @var ParagoniePharDiff */
26
    private $diff;
27
28
    /** @var Pharaoh */
29
    private $pharA;
30
31
    /** @var Pharaoh */
32
    private $pharB;
33
34
    public function __construct(string $pathA, string $pathB)
35
    {
36
        $phars = array_map(
37
            static function (string $path): Pharaoh {
38
                $realPath = realpath($path);
39
40
                return new Pharaoh(false !== $realPath ? $realPath : $path);
41
            },
42
            [$pathA, $pathB]
43
        );
44
45
        $this->pharA = $phars[0];
46
        $this->pharB = $phars[1];
47
48
        $diff = new ParagoniePharDiff(...$phars);
49
        $diff->setVerbose(true);
50
51
        $this->diff = $diff;
52
    }
53
54
    public function gitDiff(): ?string
55
    {
56
        $argA = escapeshellarg($this->pharA->tmp);
57
        $argB = escapeshellarg($this->pharB->tmp);
58
59
        /** @var string $diff */
60
        $diff = `git diff --no-index $argA $argB`;
61
62
        $diff = str_replace(
63
            $this->pharA->tmp,
64
            $this->pharA->getFileName(),
65
            $diff
66
        );
67
        $diff = str_replace(
68
            $this->pharB->tmp,
69
            $this->pharB->getFileName(),
70
            $diff
71
        );
72
73
        return '' === $diff ? null : $diff;
74
    }
75
76
    public function gnuDiff(): ?string
77
    {
78
        $argA = escapeshellarg($this->pharA->tmp);
79
        $argB = escapeshellarg($this->pharB->tmp);
80
81
        /** @var string $diff */
82
        $diff = `diff $argA $argB`;
83
84
        $diff = str_replace(
85
            $this->pharA->tmp,
86
            $this->pharA->getFileName(),
87
            $diff
88
        );
89
        $diff = str_replace(
90
            $this->pharB->tmp,
91
            $this->pharB->getFileName(),
92
            $diff
93
        );
94
95
        return '' === $diff ? null : $diff;
96
    }
97
98
    /**
99
     * @see ParagoniePharDiff::listChecksums()
100
     */
101
    public function listChecksums(string $algo = 'sha384'): int
102
    {
103
        return $this->diff->listChecksums($algo);
104
    }
105
}
106