Completed
Push — master ( 07e805...55630b )
by Sebastian
01:47
created

Repository::setCommitMsg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of SebastianFeldmann\Git.
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 SebastianFeldmann\Git;
11
12
use SebastianFeldmann\Cli\Command\Runner;
13
14
/**
15
 * Class Repository
16
 *
17
 * @package SebastianFeldmann\Git
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/sebastianfeldmann/git
20
 * @since   Class available since Release 0.9.0
21
 */
22
class Repository
23
{
24
    /**
25
     * Path to git repository root.
26
     *
27
     * @var string
28
     */
29
    private $root;
30
31
    /**
32
     * Path to .git directory
33
     *
34
     * @var string
35
     */
36
    private $dotGitDir;
37
38
    /**
39
     * Commit message.
40
     *
41
     * @var \SebastianFeldmann\Git\CommitMessage
42
     */
43
    private $commitMsg;
44
45
    /**
46
     * @var \SebastianFeldmann\Cli\Command\Runner
47
     */
48
    private $runner;
49
50
    /**
51
     * Map of operators
52
     *
53
     * @var array
54
     */
55
    private $operator = [];
56
57
    /**
58
     * Repository constructor.
59
     *
60
     * @param string                                $root
61
     * @param \SebastianFeldmann\Cli\Command\Runner $runner
62
     */
63 8
    public function __construct(string $root = '', Runner $runner = null)
64
    {
65 8
        $path = empty($root) ? getcwd() : realpath($root);
66
        // check for existing .git dir
67 8
        if (!is_dir($path . DIRECTORY_SEPARATOR . '.git')) {
68 1
            throw new \RuntimeException(sprintf('Invalid git repository: %s', $root));
69
        }
70 7
        $this->root      = $path;
71 7
        $this->dotGitDir = $this->root . DIRECTORY_SEPARATOR . '.git';
72 7
        $this->runner    = null == $runner ? new Runner\Simple() : $runner;
0 ignored issues
show
Documentation Bug introduced by
It seems like null == $runner ? new \S...nner\Simple() : $runner can also be of type object<SebastianFeldmann...\Command\Runner\Simple>. However, the property $runner is declared as type object<SebastianFeldmann\Cli\Command\Runner>. 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...
73 7
    }
74
75
    /**
76
     * Root path getter.
77
     *
78
     * @return string
79
     */
80 1
    public function getRoot() : string
81
    {
82 1
        return $this->root;
83
    }
84
85
    /**
86
     * Returns the path to the hooks directory.
87
     *
88
     * @return string
89
     */
90 2
    public function getHooksDir() : string
91
    {
92 2
        return $this->dotGitDir . DIRECTORY_SEPARATOR . 'hooks';
93
    }
94
95
    /**
96
     * Check for a hook file.
97
     *
98
     * @param  string $hook
99
     * @return bool
100
     */
101 1
    public function hookExists($hook) : bool
102
    {
103 1
        return file_exists($this->getHooksDir() . DIRECTORY_SEPARATOR . $hook);
104
    }
105
106
    /**
107
     * CommitMessage setter.
108
     *
109
     * @param \SebastianFeldmann\Git\CommitMessage $commitMsg
110
     */
111 1
    public function setCommitMsg(CommitMessage $commitMsg)
112
    {
113 1
        $this->commitMsg = $commitMsg;
114 1
    }
115
116
    /**
117
     * CommitMessage getter.
118
     *
119
     * @return \SebastianFeldmann\Git\CommitMessage
120
     */
121 2
    public function getCommitMsg() : CommitMessage
122
    {
123 2
        if (null === $this->commitMsg) {
124 1
            throw new \RuntimeException('No commit message available');
125
        }
126 1
        return $this->commitMsg;
127
    }
128
129
    /**
130
     * Is there a merge in progress.
131
     *
132
     * @return bool
133
     */
134 2
    public function isMerging() : bool
135
    {
136 2
        foreach (['MERGE_MSG', 'MERGE_HEAD', 'MERGE_MODE'] as $fileName) {
137 2
            if (file_exists($this->dotGitDir . DIRECTORY_SEPARATOR . $fileName)) {
138 2
                return true;
139
            }
140
        }
141 1
        return false;
142
    }
143
144
    /**
145
     * Get changed file resolver.
146
     *
147
     * @return \SebastianFeldmann\Git\Operator\Index
148
     */
149 1
    public function getIndexOperator()
150
    {
151 1
        return $this->getOperator('Index');
152
    }
153
154
    /**
155
     * Return requested operator.
156
     *
157
     * @param  string $name
158
     * @return mixed
159
     */
160 1
    private function getOperator(string $name)
161
    {
162 1
        if (!isset($this->operator[$name])) {
163 1
            $class                 = '\\SebastianFeldmann\\Git\\Operator\\' . $name;
164 1
            $this->operator[$name] = new $class($this->runner, $this);
165
        }
166 1
        return $this->operator[$name];
167
    }
168
}
169