BranchFiles::replacement()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
ccs 8
cts 8
cp 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of CaptainHook.
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Runner\Action\Cli\Command\Placeholder;
13
14
use CaptainHook\App\Git;
15
use CaptainHook\App\Hook\FileList;
16
17
/**
18
 * Changed Files Placeholder
19
 *
20
 * This placeholder only works for pre-push, post-rewrite, post-checkout and post-merge actions.
21
 * If it is used in a pre-push hook and multiple refs are pushed the placeholder will contain
22
 * all changed files for all refs.
23
 *
24
 * Usage examples:
25
 *  - {$BRANCH_FILES|compare-to:main|separated-by:,}
26
 *  - {$BRANCH_FILES|in-dir:foo/bar}
27
 *  - {$BRANCH_FILES|of-type:php}
28
 *
29
 * @package CaptainHook
30
 * @author  Sebastian Feldmann <[email protected]>
31
 * @link    https://github.com/captainhook-git/captainhook
32
 * @since   Class available since Release 5.21.0
33
 */
34
class BranchFiles extends Foundation
35
{
36
    /**
37
     * @param  array<string, string> $options
38
     * @return string
39
     */
40 5
    public function replacement(array $options): string
41
    {
42 5
        $branch = $this->repository->getInfoOperator()->getCurrentBranch();
43 5
        $start  = $options['compared-to'] ?? $this->repository->getLogOperator()->getBranchRevFromRefLog($branch);
44
45 5
        if (empty($start)) {
46 1
            $this->io->write('could not find branch start');
47 1
            return '';
48
        }
49 4
        $files = $this->repository->getLogOperator()->getChangedFilesSince($start, ['A', 'C', 'M', 'R']);
50
51 4
        return implode(($options['separated-by'] ?? ' '), FileList::filter($files, $options));
52
    }
53
}
54