Completed
Push — add/relase-scripts-package ( 097cd4...aa7fa7 )
by Yaroslav
55:18 queued 47:13
created

Git_Shell_Command::commit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Git shell commands
4
 *
5
 * @package automattic/jetpack-scripts
6
 */
7
8
namespace Automattic\Jetpack\Scripts;
9
10
/**
11
 * Wrapper around some git commands
12
 */
13
class Git_Shell_Command {
14
	/**
15
	 * Constructor!
16
	 *
17
	 * @param String $name repository name.
18
	 */
19
	public function __construct( $name ) {
20
		$this->name    = $name;
0 ignored issues
show
Bug introduced by
The property name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
		$this->path    = explode( '/', $name )[1];
0 ignored issues
show
Bug introduced by
The property path does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
		$this->dir_arg = "--git-dir=$this->path/.git --work-tree=$this->path";
0 ignored issues
show
Bug introduced by
The property dir_arg does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
	}
24
25
	/**
26
	 * Returns the latest repo tag
27
	 */
28
	public function get_latest_tag() {
29
		$cmd    = "git $this->dir_arg describe --abbrev=0";
30
		$result = Cmd::run( $cmd );
31
32
		return $result['output'];
33
	}
34
35
	/**
36
	 * Returns a `shortstat` diff between two tags
37
	 *
38
	 * @param String $source git tag or branch.
39
	 * @param String $target git tag or branch.
40
	 */
41
	public function get_diff_between( $source, $target ) {
42
		$cmd    = "git $this->dir_arg diff $source $target --shortstat";
43
		$result = Cmd::run( $cmd );
44
45
		return $result['output'];
46
	}
47
48
	/**
49
	 * Clones a repository
50
	 *
51
	 * @param String $type URL type to use.
52
	 */
53
	public function clone_repository( $type = 'ssh' ) {
54
		if ( 'ssh' === $type ) {
55
			$url = "[email protected]:$this->name.git";
56
		} else {
57
			$url = "https://github.com/$this->name.git";
58
59
		}
60
		Cmd::run( "rm -rf $this->path" );
61
62
		$cmd    = "git clone $url 2>&1";
63
		$result = Cmd::run( $cmd );
64
65
		return $result['output'];
66
	}
67
68
	/**
69
	 * Checkout to a new branch
70
	 *
71
	 * @param String $branch branch name.
72
	 */
73
	public function checkout_new_branch( $branch ) {
74
		$cmd    = "git $this->dir_arg checkout -q -b $branch 2>&1";
75
		$result = Cmd::run( $cmd );
76
77
		return $result['output'];
78
	}
79
80
	/**
81
	 * Tag new version
82
	 *
83
	 * @param String $name tag name.
84
	 */
85
	public function tag_new_version( $name ) {
86
		$cmd    = "git $this->dir_arg tag -a $name -m 'New release for $name' 2>&1";
87
		$result = Cmd::run( $cmd );
88
89
		return $result['output'];
90
	}
91
92
	/**
93
	 * Get status
94
	 */
95
	public function status() {
96
		$result = Cmd::run( "git $this->dir_arg status --porcelain --untracked-files=no" );
97
98
		return $result['output'];
99
	}
100
101
	/**
102
	 * Commit all changes
103
	 */
104
	public function commit() {
105
		$result = Cmd::run( "git $this->dir_arg commit -a -m 'Add changes'" );
106
107
		return $result['output'];
108
	}
109
110
	/**
111
	 * Checkout to a new branch
112
	 *
113
	 * @param String $branch branch name.
114
	 */
115
	public function push_to_remote( $branch ) {
116
		$cmd    = "git $this->dir_arg push --set-upstream origin $branch 2>&1";
117
		$result = Cmd::run( $cmd );
0 ignored issues
show
Unused Code introduced by
$result 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...
118
119
		$cmd    = "git $this->dir_arg push origin --tags 2>&1";
120
		$result = Cmd::run( $cmd );
121
122
		return $result['output'];
123
	}
124
}
125