Passed
Push — master ( 998a95...21763d )
by Federico
02:41
created

git.php ➔ getGitLog()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 54
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
eloc 44
c 3
b 0
f 0
nc 9
nop 1
dl 0
loc 54
rs 7.255

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
	function getGitLog( $_dir = "./" ) {
3
		if(!file_exists($_dir)) return [];
4
		$currentDir = getcwd();
5
		chdir($_dir);
6
		$git_history = [];
7
		$git_logs	= [];
8
		$gitPath	= str_replace('\\', '/', exec("git rev-parse --show-toplevel"));
9
		$rootPath	= str_replace('\\', '/', getcwd ());
10
		if( $gitPath != $rootPath ) {
11
			chdir($currentDir);
12
			return [];
13
		}
14
		exec("git log --decorate=full --tags", $git_logs);
15
		$last_hash = null;
16
		foreach ($git_logs as $line) {
0 ignored issues
show
Bug introduced by
The expression $git_logs of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
17
			$line = trim($line);
18
			if (!empty($line)) {
19
				// Commit
20
				if (strpos($line, 'commit') === 0) {
21
					$hash = explode(' ', $line);
22
					$hash = trim(end($hash));
23
					$git_history[$hash] = [
24
						'tag' => '-1.0.0',
25
						'author' => '',
26
						'date' => '',
27
						'message' => ''
28
					];
29
					$last_hash = $hash;
30
					if (strpos($line, 'tag') !== false) {
31
						$tag = explode(':', $line);
32
						$tag = explode('/', $tag[1]);
33
						$tag = explode(',', $tag[2]);
34
						$tag = explode(')', $tag[0]);
35
						$tag = trim($tag[0]);
36
						$git_history[$last_hash]['tag'] = $tag;
37
					}
38
				}
39
				else if (strpos($line, 'Author') === 0) {
40
					$author = explode(':', $line);
41
					$author = trim(end($author));
42
					$git_history[$last_hash]['author'] = $author;
43
				}
44
				else if (strpos($line, 'Date') === 0) {
45
					$date = explode(':', $line, 2);
46
					$date = trim(end($date));
47
					$git_history[$last_hash]['date'] = date('d/m/Y H:i:s A', strtotime($date));
48
				}
49
				else
50
					$git_history[$last_hash]['message'] .= $line ." ";
51
			}
52
		}
53
		chdir($currentDir);
54
		return $git_history;
55
	}
56
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
57