Completed
Push — master ( ae5ea4...18538b )
by Sebastian
05:19
created

PrepareCommitMsg::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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\Hooks;
15
use SebastianFeldmann\Git\CommitMessage;
16
use SebastianFeldmann\Git\Repository;
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
    /**
39
     * File to read/write the commit message from/to
40
     *
41
     * @var string
42
     */
43
    private $file;
44
45
    /**
46
     * Commit mode [null|message|template|merge|squash|commit]
47
     *
48
     * @var string
49
     */
50
    private $mode;
51
52
    /**
53
     * Commit hash if mode is commit during -c or --amend
54
     *
55
     * @var string
56
     */
57
    private $hash;
58
59
    /**
60
     * Configure the command
61
     */
62 1
    protected function configure()
63
    {
64 1
        parent::configure();
65 1
        $this->addArgument('file', InputArgument::REQUIRED, 'File containing the commit log message');
66 1
        $this->addArgument('mode', InputArgument::OPTIONAL, 'Current commit mode');
67 1
        $this->addArgument('hash', InputArgument::OPTIONAL, 'Given commit hash');
68 1
    }
69
    /**
70
     * Read the commit message from file
71
     *
72
     * @param \Symfony\Component\Console\Input\InputInterface   $input
73
     * @param \Symfony\Component\Console\Output\OutputInterface $output
74
     * @param \CaptainHook\App\Config                           $config
75
     * @param \SebastianFeldmann\Git\Repository                 $repository
76
     */
77 1
    protected function setup(InputInterface $input, OutputInterface $output, Config $config, Repository $repository)
78
    {
79 1
        $this->file       = $input->getArgument('file');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getArgument('file') can also be of type array<integer,string>. However, the property $file is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
80 1
        $this->mode       = $input->getArgument('mode');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getArgument('mode') can also be of type array<integer,string>. However, the property $mode is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
81 1
        $this->hash       = $input->getArgument('hash');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getArgument('hash') can also be of type array<integer,string>. However, the property $hash is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
82 1
        $gitConfig        = $repository->getConfigOperator();
83 1
        $commentCharacter = $gitConfig->getSafely('core.commentchar', '#');
84
85 1
        $repository->setCommitMsg(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...
86
87 1
        parent::setup($input, $output, $config, $repository);
88 1
    }
89
90
    /**
91
     * Post action after all the configured actions are executed
92
     *
93
     * @param \Symfony\Component\Console\Input\InputInterface   $input
94
     * @param \Symfony\Component\Console\Output\OutputInterface $output
95
     * @param \CaptainHook\App\Config                           $config
96
     * @param \SebastianFeldmann\Git\Repository                 $repository
97
     */
98 1
    protected function tearDown(InputInterface $input, OutputInterface $output, Config $config, Repository $repository)
99
    {
100 1
        file_put_contents($this->file, $repository->getCommitMsg()->getRawContent());
101 1
    }
102
}
103