MergeCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 67
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A merge() 0 26 5
A mergeCmdSwitchOptions() 0 7 1
1
<?php
2
3
/**
4
 * GitElephant - An abstraction layer for git written in PHP
5
 * Copyright (C) 2013  Matteo Giachino
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
19
 */
20
21
namespace GitElephant\Command;
22
23
use GitElephant\Objects\Branch;
24
use GitElephant\Repository;
25
26
/**
27
 * Merge command generator
28
 *
29
 * @author Matteo Giachino <[email protected]>
30
 */
31
class MergeCommand extends BaseCommand
32
{
33
    public const MERGE_COMMAND = 'merge';
34
    public const MERGE_OPTION_FF_ONLY = '--ff-only';
35
    public const MERGE_OPTION_NO_FF = '--no-ff';
36
37
    /**
38
     * constructor
39
     *
40
     * @param \GitElephant\Repository $repo The repository object this command
41
     *                                      will interact with
42
     */
43 3
    public function __construct(Repository $repo = null)
44
    {
45 3
        parent::__construct($repo);
46 3
    }
47
48
    /**
49
     * Generate a merge command
50
     *
51
     * @param \GitElephant\Objects\Branch $with    the branch to merge
52
     * @param string                      $message a message for the merge commit, if merge is 3-way
53
     * @param array                       $options option flags for git merge
54
     *
55
     * @throws \RuntimeException
56
     * @return string
57
     */
58 3
    public function merge(Branch $with, $message = '', array $options = []): string
59
    {
60 3
        if (in_array(self::MERGE_OPTION_FF_ONLY, $options) && in_array(self::MERGE_OPTION_NO_FF, $options)) {
61 1
            throw new \Symfony\Component\Process\Exception\InvalidArgumentException(
62 1
                "Invalid options: cannot use flags --ff-only and --no-ff together."
63
            );
64
        }
65
66 2
        $normalizedOptions = $this->normalizeOptions($options, $this->mergeCmdSwitchOptions());
67
68 2
        $this->clearAll();
69 2
        $this->addCommandName(static::MERGE_COMMAND);
70
71 2
        foreach ($normalizedOptions as $value) {
72 1
            $this->addCommandArgument($value);
73
        }
74
75 2
        if (!empty($message)) {
76 1
            $this->addCommandArgument('-m');
77 1
            $this->addCommandArgument($message);
78
        }
79
        
80 2
        $this->addCommandSubject($with->getFullRef());
81
82 2
        return $this->getCommand();
83
    }
84
85
    /**
86
     * Valid options for remote command that do not require an associated value
87
     *
88
     * @return array Associative array mapping all non-value options and their respective normalized option
89
     */
90 2
    public function mergeCmdSwitchOptions(): array
91
    {
92
        return [
93 2
            self::MERGE_OPTION_FF_ONLY => self::MERGE_OPTION_FF_ONLY,
94 2
            self::MERGE_OPTION_NO_FF => self::MERGE_OPTION_NO_FF,
95
        ];
96
    }
97
}
98