Completed
Push — master ( ab54c8...77228d )
by Jean-Christophe
01:47
created

UGitRepository::getRemoteUrl()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 0
1
<?php
2
3
namespace Ubiquity\utils\git;
4
5
use Cz\Git\GitRepository;
6
use Cz\Git\GitException;
7
8
class UGitRepository extends GitRepository {
9
	
10
	/**
11
	 * Runs command.
12
	 * @param  string|array
13
	 * @return self
14
	 * @throws Cz\Git\GitException
15
	 */
16
	protected function run($cmd/*, $options = NULL*/){
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
17
		$args = func_get_args();
18
		$cmd = $this->_processCommand($args);
19
		exec($cmd . ' 2>&1', $output, $ret);
20
		
21
		if($ret !== 0)
22
		{
23
			throw new GitException("Command '$cmd' failed (exit-code $ret).", $ret);
24
		}
25
		
26
		return $this;
27
	}
28
	
29
	protected function _processCommand(array $args)
30
	{
31
		$cmd = array();
32
		
33
		$programName = array_shift($args);
34
		
35
		foreach($args as $arg)
36
		{
37
			if(is_array($arg))
38
			{
39
				foreach($arg as $key => $value)
40
				{
41
					$_c = '';
42
					
43
					if(is_string($key)){
44
						$_c = "$key ";
45
					}
46
					if(is_array($value)){
47
						foreach ($value as $v){
48
							$cmd[] = $_c . escapeshellarg($v);
49
						}
50
					}else{
51
						$cmd[] = $_c . escapeshellarg($value);
52
					}
53
				}
54
			}
55
			elseif(is_scalar($arg) && !is_bool($arg))
56
			{
57
				$cmd[] = escapeshellarg($arg);
58
			}
59
		}
60
		return "$programName " . implode(' ', $cmd);
61
	}
62
	
63
	/**
64
	 * Returns list of untracked files in repo.
65
	 * @return string[]|NULL  NULL => no files untracked
66
	 */
67
	public function getUntrackedFiles(){
68
		return $this->extractFromCommand('git ls-files --others --exclude-standard', function($value) {
69
			return trim($value);
70
		});
71
	}
72
	
73
	/**
74
	 * Returns list of modified files in repo.
75
	 * @return string[]|NULL  NULL => no files modified
76
	 */
77
	public function getModifiedFiles(){
78
		try{
79
		return $this->extractFromCommand('git diff --name-status HEAD', function($array) {
80
			$array=trim(preg_replace('!\s+!', ' ', $array));
81
			return explode(' ', $array);
82
		});
83
		}catch (\Cz\Git\GitException $e){
1 ignored issue
show
Bug introduced by
The class Cz\Git\GitException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
84
			return [];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type documented by Ubiquity\utils\git\UGitR...itory::getModifiedFiles of type string[]|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
85
		}
86
	}
87
	
88 View Code Duplication
	public function getChangesInFile($filename){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
		try{
90
			$output=$this->extractFromCommand('git diff '.$filename);
91
			if(is_array($output))
92
				return implode('\r\n', $output);
93
			return $output;
94
		}catch (\Cz\Git\GitException $e){
1 ignored issue
show
Bug introduced by
The class Cz\Git\GitException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
95
			return "";
96
		}
97
	}
98
	
99 View Code Duplication
	public function getChangesInCommit($commitHash){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
		try{
101
			$output=$this->extractFromCommand("git show {$commitHash}");
102
			if(is_array($output))
103
				return implode('\r\n', $output);
104
				return $output;
105
		}catch (\Cz\Git\GitException $e){
1 ignored issue
show
Bug introduced by
The class Cz\Git\GitException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
106
			return "";
107
		}
108
	}
109
	
110
	/**
111
	 * Returns the remote URL
112
	 * @return string
113
	 */
114 View Code Duplication
	public function getRemoteUrl(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
		try{
116
			$values= $this->extractFromCommand('git config --get remote.origin.url',function($str){
117
				return trim($str);
118
			});
119
			if(isset($values)){
120
				return implode(" ", $values);
121
			}
122
		}catch (\Cz\Git\GitException $e){
1 ignored issue
show
Bug introduced by
The class Cz\Git\GitException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
123
			return "";
124
		}
125
		return "";
126
	}
127
	
128
	/**
129
	 * Ignore file(s).
130
	 * `git update-index --assume-unchanged <file>`
131
	 * @param  $files string|string[]
132
	 * @throws Cz\Git\GitException
133
	 * @return self
134
	 */
135
	public function ignoreFiles($files){
136
		if(!is_array($files)){
137
			$file = func_get_args();
0 ignored issues
show
Unused Code introduced by
$file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
138
		}
139
		$this->begin();
140
		$this->run('git reset', NULL,["--"=>$files]);
141
		return $this->end();
142
	}
143
	
144
	public function getCommits(){
145
		$nonPushed=$this->getNonPushedCommitHash();
146
		try{
147
			return $this->extractFromCommand('git log --pretty=format:"%h___%an___%ar___%s___%H"', function($str) use($nonPushed){
148
				$array=explode("___", $str);
149
				$pushed=true;
150
				if(is_array($nonPushed))
151
					$pushed=!in_array($array[0], $nonPushed);
152
				return new GitCommit($array[0],$array[1],$array[2],$array[3],$array[4],$pushed);
153
			});
154
		}catch (\Cz\Git\GitException $e){
1 ignored issue
show
Bug introduced by
The class Cz\Git\GitException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
155
			return [];
156
		}
157
		return [];
0 ignored issues
show
Unused Code introduced by
return array(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
158
	}
159
	
160
	public function getNonPushedCommitHash(){
161
		try{
162
			return $this->extractFromCommand('git log origin/master..master --pretty=format:"%h"');
163
		}catch (\Cz\Git\GitException $e){
1 ignored issue
show
Bug introduced by
The class Cz\Git\GitException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
164
			return [];
165
		}
166
		return [];
0 ignored issues
show
Unused Code introduced by
return array(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
167
	}
168
}
169