|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hal\Application\Config; |
|
3
|
|
|
|
|
4
|
|
|
use Hal\Application\Config\File\ConfigFileReaderFactory; |
|
5
|
|
|
|
|
6
|
|
|
class Parser |
|
7
|
|
|
{ |
|
8
|
|
|
public function parse($argv) |
|
9
|
|
|
{ |
|
10
|
|
|
$config = new Config; |
|
11
|
|
|
|
|
12
|
|
|
if (sizeof($argv) === 0) { |
|
13
|
|
|
return $config; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
if (preg_match('!\.php$!', $argv[0]) || preg_match('!phpmetrics$!', $argv[0]) || preg_match('!phpmetrics.phar$!', $argv[0])) { |
|
17
|
|
|
array_shift($argv); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
// Checking for a configuration file option key and importing options |
|
21
|
|
View Code Duplication |
foreach ($argv as $k => $arg) { |
|
|
|
|
|
|
22
|
|
|
if (preg_match('!\-\-config=(.*)!', $arg, $matches)) { |
|
23
|
|
|
$fileReader = ConfigFileReaderFactory::createFromFileName($matches[1]); |
|
24
|
|
|
$fileReader->read($config); |
|
25
|
|
|
unset($argv[$k]); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// arguments with options |
|
30
|
|
View Code Duplication |
foreach ($argv as $k => $arg) { |
|
|
|
|
|
|
31
|
|
|
if (preg_match('!\-\-([\w\-]+)=(.*)!', $arg, $matches)) { |
|
32
|
|
|
list(, $parameter, $value) = $matches; |
|
33
|
|
|
$config->set($parameter, trim($value, ' "\'')); |
|
34
|
|
|
unset($argv[$k]); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// arguments without options |
|
39
|
|
View Code Duplication |
foreach ($argv as $k => $arg) { |
|
|
|
|
|
|
40
|
|
|
if (preg_match('!\-\-([\w\-]+)$!', $arg, $matches)) { |
|
41
|
|
|
list(, $parameter) = $matches; |
|
42
|
|
|
$config->set($parameter, true); |
|
43
|
|
|
unset($argv[$k]); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// last argument |
|
48
|
|
|
$files = array_pop($argv); |
|
49
|
|
|
if ($files && !preg_match('!^\-\-!', $files)) { |
|
50
|
|
|
$config->set('files', explode(',', $files)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $config; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
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.