Base   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 237
Duplicated Lines 6.75 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 2
dl 16
loc 237
rs 9.6
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
doRun() 0 1 ?
A __construct() 0 6 1
A getRequiredArgumentCount() 0 3 2
A getAllArgumentsCount() 0 3 2
A getIndexByPos() 0 14 4
A getArgument() 0 14 3
A getArgumentFile() 0 9 2
A getArgumentPrivateKey() 8 8 2
A getArgumentPublicKey() 8 8 2
A getArgumentStringOrFileContent() 0 7 2
A getArgumentThreemaId() 0 7 3
A readStdIn() 0 15 4
A run() 0 10 3
A help() 0 14 3
A description() 0 3 1
A subject() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @author Threema GmbH
4
 * @copyright Copyright (c) 2015-2016 Threema GmbH
5
 */
6
7
8
namespace Threema\Console\Command;
9
10
use Threema\Console\Common;
11
use Threema\Core\Exception;
12
use Threema\Console\Command\CryptTool;
13
14
abstract class Base {
15
	const argThreemaId = 'threemaId';
16
	const argFrom = 'from';
17
	const argSecret = 'secret';
18
	const argPrivateKey = 'privateKey';
19
	const argPublicKey = 'publicKey';
20
	const argPrivateKeyFile = 'privateKeyFile';
21
	const argPublicKeyFile = 'publicKeyFile';
22
	const argNonce = 'nonce';
23
24
	/**
25
	 * @var string
26
	 */
27
	private $subject;
28
29
	/**
30
	 * @var string[]
31
	 */
32
	private $arguments = array();
33
34
	/**
35
	 * @var string[]
36
	 */
37
	private $requiredArguments;
38
39
	/**
40
	 * @var string
41
	 */
42
	private $description;
43
	/**
44
	 * @var array
45
	 */
46
	private $optionalArguments;
47
48
	/**
49
	 * @param string $subject
50
	 * @param string[] $requiredArguments
51
	 * @param string $description
52
	 * @param array $optionalArguments
53
	 */
54
	public function __construct($subject, array $requiredArguments, $description, array $optionalArguments = array()) {
55
		$this->subject = $subject;
56
		$this->requiredArguments = $requiredArguments;
57
		$this->description = $description;
58
		$this->optionalArguments = $optionalArguments;
59
	}
60
61
	/**
62
	 * @return int
63
	 */
64
	public function getRequiredArgumentCount() {
65
		return null !== $this->requiredArguments ? count($this->requiredArguments) : 0;
66
	}
67
68
	/**
69
	 * @return int
70
	 */
71
	public function getAllArgumentsCount() {
72
		return $this->getRequiredArgumentCount() + (null !== $this->optionalArguments ? count($this->optionalArguments) : 0);
73
	}
74
75
	/**
76
	 * @param string $pos
77
	 * @return int
78
	 * @throws \Threema\Core\Exception
79
	 */
80
	private function getIndexByPos($pos){
81
		$i = array_search($pos, $this->requiredArguments);
82
		if(false === $i) {
83
			$i = array_search($pos, $this->optionalArguments);
84
			if(false !== $i) {
85
				$i += count($this->requiredArguments);
86
			}
87
		}
88
		if(false === $i) {
89
			throw new Exception('argument '.$pos.' not found');
90
		}
91
92
		return $i;
93
	}
94
95
	/**
96
	 * @param string $pos
97
	 * @return null|string
98
	 * @throws \Threema\Core\Exception
99
	 */
100
	public function getArgument($pos) {
101
		$i = $this->getIndexByPos($pos);
102
		$required = $i < count($this->requiredArguments);
103
104
		if(false === array_key_exists($i, $this->arguments)) {
105
			if(true === $required) {
106
				throw new Exception('no argument at position '.$i.' ('.$pos.')');
107
			}
108
			else {
109
				return null;
110
			}
111
		}
112
		return $this->arguments[$i];
113
	}
114
115
	/**
116
	 * return a valid file path
117
	 *
118
	 * @param string $pos
119
	 * @return null|string
120
	 * @throws \Threema\Core\Exception
121
	 */
122
	public function getArgumentFile($pos) {
123
		$i = $this->getIndexByPos($pos);
124
125
		if(false === is_file($this->arguments[$i])) {
126
			throw new Exception('argument '.$i.' is not a file');
127
		}
128
129
		return $this->arguments[$i];
130
	}
131
132
	/**
133
	 * @param string $pos
134
	 * @return null|string
135
	 */
136 View Code Duplication
	public function getArgumentPrivateKey($pos) {
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...
137
		$content = Common::getPrivateKey($this->getArgumentStringOrFileContent($pos));
138
		$cryptTool = CryptTool::getInstance();
139
		if(null !== $content) {
140
			return $cryptTool->hex2bin($content);
141
		}
142
		return null;
143
	}
144
145
	/**
146
	 * @param string $pos
147
	 * @return null|string
148
	 */
149 View Code Duplication
	public function getArgumentPublicKey($pos) {
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...
150
		$content = Common::getPublicKey($this->getArgumentStringOrFileContent($pos));
151
		$cryptTool = CryptTool::getInstance();
152
		if(null !== $content) {
153
			return $cryptTool->hex2bin($content);
154
		}
155
		return null;
156
	}
157
158
	/**
159
	 * @param string $pos
160
	 * @return null|string
161
	 */
162
	private function getArgumentStringOrFileContent($pos) {
163
		$content = $this->getArgument($pos);
164
		if(file_exists($content)) {
165
			$content = trim(file_get_contents($content));
166
		}
167
		return $content;
168
	}
169
170
	/**
171
	 * @param string $pos
172
	 * @return null|string
173
	 */
174
	public function getArgumentThreemaId($pos) {
175
		$content = $this->getArgument($pos);
176
		if(null !== $content && strlen($content) == 8) {
177
			return strtoupper($content);
178
		}
179
		return null;
180
	}
181
182
	/**
183
	 * @return string
184
	 */
185
	protected function readStdIn() {
186
		$f = fopen( 'php://stdin', 'r' );
187
188
		$lines = array();
189
		while( $line = trim(fgets($f)) ) {
190
191
			if(strlen($line) == 0 || $line == "\n") {
192
				continue;
193
			}
194
195
			$lines[] = $line;
196
		}
197
198
		return implode("\n", $lines);
199
	}
200
201
	/**
202
	 * @param array $arguments
203
	 * @throws \Threema\Core\Exception
204
	 */
205
	final public function run(array $arguments) {
206
		$this->arguments = $arguments;
207
208
		$argCount = null !== $this->arguments ? count($this->arguments) : 0;
209
210
		if($argCount < count($this->requiredArguments)) {
211
			throw new Exception('invalid count of arguments');
212
		}
213
		$this->doRun();
214
	}
215
216
	/**
217
	 * @param bool $shellColors
218
	 * @return string
219
	 */
220
	final public function help($shellColors = true) {
221
		$args = array_merge($this->requiredArguments, $this->optionalArguments);
222
223
		if(count($args) > 0) {
224
			$colorPrefix = '';
225
			$colorPostfix = '';
226
			if(true === $shellColors) {
227
				$colorPrefix = "\033[0;36m\033[40m";
228
				$colorPostfix = "\033[0m";
229
			}
230
			return  $colorPrefix.'<'.implode('> <', $args).'>'.$colorPostfix;
231
		}
232
		return '';
233
	}
234
235
	/**
236
	 * @return string
237
	 */
238
	final public function description() {
239
		return $this->description;
240
	}
241
242
	/**
243
	 * @return string
244
	 */
245
	final public function subject() {
246
		return $this->subject;
247
	}
248
249
	abstract protected function doRun();
250
}
251