Conditions | 9 |
Paths | 9 |
Total Lines | 54 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
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) { |
||
|
|||
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 | ?> |
||
57 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.