Passed
Push — main ( d6472a...9a4825 )
by Sebastian
03:57
created

Debug   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 14
c 4
b 0
f 0
dl 0
loc 51
ccs 13
cts 14
cp 0.9286
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getArgumentOutput() 0 7 2
A debugOutput() 0 15 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\Hook;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO;
16
use CaptainHook\App\Exception\ActionFailed;
17
use Exception;
18
use SebastianFeldmann\Git\Repository;
19
20
/**
21
 * Debug hook to test hook triggering
22
 *
23
 * @package CaptainHook
24
 * @author  Sebastian Feldmann <[email protected]>
25
 * @link    https://github.com/captainhook-git/captainhook
26
 * @since   Class available since Release 4.0.4
27
 */
28
abstract class Debug implements Action
29
{
30
    /**
31
     * Executes the action
32
     *
33
     * @param  \CaptainHook\App\Config           $config
34
     * @param  \CaptainHook\App\Console\IO       $io
35
     * @param  \SebastianFeldmann\Git\Repository $repository
36
     * @param  \CaptainHook\App\Config\Action    $action
37
     * @return void
38
     * @throws \Exception
39
     */
40
    abstract public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void;
41
42
    /**
43
     * Generate some debug output
44
     *
45
     * @param \CaptainHook\App\Console\IO       $io
46
     * @param \SebastianFeldmann\Git\Repository $repository
47
     * @return void
48
     */
49 2
    protected function debugOutput(IO $io, Repository $repository): void
50
    {
51 2
        $originalHookArguments = $io->getArguments();
52
53 2
        $currentGitTag = 'no tags yet';
0 ignored issues
show
Unused Code introduced by
The assignment to $currentGitTag is dead and can be removed.
Loading history...
54
        try {
55 2
            $currentGitTag = $repository->getInfoOperator()->getCurrentTag();
56
        } catch (Exception $e) {
57
            // ignore it, it just means there are no tags yet
58
        }
59
60 2
        $io->write('<info>Debug Action</info>');
61 2
        $io->write($this->getArgumentOutput($originalHookArguments));
62 2
        $io->write('  Current git-tag: <comment>' . $currentGitTag . '</comment>');
63 2
        $io->write('  StandardInput: ' . PHP_EOL . '    ' . implode(PHP_EOL . '    ', $io->getStandardInput()));
64
    }
65
66
    /**
67
     * Format output to display original hook arguments
68
     *
69
     * @param  array<string> $args
70
     * @return string
71
     */
72 2
    protected function getArgumentOutput(array $args): string
73
    {
74 2
        $out = 'Original arguments:' . PHP_EOL;
75 2
        foreach ($args as $name => $value) {
76 2
            $out .= '    ' . $name . ' => <comment>' . $value . '</comment>' . PHP_EOL;
77
        }
78 2
        return '  ' . trim($out);
79
    }
80
}
81