Completed
Pull Request — master (#15)
by Andreas
02:21
created

PrepareCommitMsg   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 47
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A setup() 0 11 1
A teardown() 0 4 1
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace CaptainHook\App\Console\Command\Hook;
11
12
use CaptainHook\App\Config;
13
use CaptainHook\App\Console\Command\Hook;
14
use CaptainHook\App\Console\IO;
15
use CaptainHook\App\Hooks;
16
use SebastianFeldmann\Git;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * Class PrepareCommitMessage
23
 *
24
 * @package CaptainHook
25
 * @author  Sebastian Feldmann <[email protected]>
26
 * @link    https://github.com/sebastianfeldmann/captainhook
27
 * @since   Class available since Release 3.0.1
28
 */
29
class PrepareCommitMsg extends Hook
30
{
31
    /**
32
     * Hook to execute
33
     *
34
     * @var string
35
     */
36
    protected $hookName = Hooks::PREPARE_COMMIT_MSG;
37
38
    private $file;
39
40 1
    /**
41
     * Configure the command.
42 1
     */
43 1
    protected function configure()
44 1
    {
45 1
        parent::configure();
46 1
        $this->addArgument('file', InputArgument::REQUIRED, 'File containing the commit log message');
47
        $this->addArgument('mode', InputArgument::OPTIONAL, 'Current commit mode');
48
        $this->addArgument('hash', InputArgument::OPTIONAL, 'Given commit hash');
49
    }
50
    /**
51
     * Read the commit message from file.
52
     *
53
     * @param \Symfony\Component\Console\Input\InputInterface   $input
54
     * @param \Symfony\Component\Console\Output\OutputInterface $output
55 1
     * @param \CaptainHook\App\Config                           $config
56
     * @param \SebastianFeldmann\Git\Repository                 $repository
57
     */
58 1
    protected function setup(InputInterface $input, OutputInterface $output, Config $config, Git\Repository $repository)
59
    {
60
        $this->file = $input->getArgument('file');
61
62
        $gitConfig        = $repository->getConfigOperator();
63
        $commentCharacter = $gitConfig->getSafely('core.commentchar', '#');
64
65
        $repository->setCommitMsg(Git\CommitMessage::createFromFile($this->file, $commentCharacter));
0 ignored issues
show
Bug introduced by
It seems like $this->file can also be of type array<integer,string> or null; however, SebastianFeldmann\Git\Co...ssage::createFromFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
66
67
        parent::setup($input, $output, $config, $repository);
68
    }
69
70
    protected function teardown(IO $io, Config $config, Git\Repository $repository)
71
    {
72
        file_put_contents($this->file, $repository->getCommitMsg()->getRawContent());
73
    }
74
75
}
76