Completed
Push — develop ( 5f7df1...9cb564 )
by Matteo
10s
created

MergeCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 7
c 4
b 0
f 1
lcom 1
cbo 3
dl 0
loc 64
ccs 22
cts 22
cp 1
rs 10

3 Methods

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