Shell   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 107
Duplicated Lines 13.08 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 14
loc 107
rs 10
c 0
b 0
f 0
wmc 27
lcom 2
cbo 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
B _compileCommand() 0 23 8
A getShellCommand() 0 3 1
A __callStatic() 0 11 4
A __construct() 0 3 2
A __toString() 0 5 2
A pipe() 7 7 3
A sequence() 7 7 3
A execCommand() 0 3 1
A alias() 0 3 1
A escape() 0 3 1
A run() 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
/**
4
 * Shell
5
 *
6
 * A shell access proxy.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015 - http://caffeina.it
11
 */
12
13
class Shell {
14
    use Module;
15
16
    protected static $aliases = [];
17
    protected $command;
18
19
    /**
20
     * Compile a shell command
21
     * @param string $command
22
     * @param array $params
23
     * @return string
24
     */
25
    protected static function _compileCommand($command,array $params){
26
        $s = $w = [];
27
        foreach ($params as $p) {
28
            if ($p instanceof static) {
29
              $s[] = '$('.$p->getShellCommand().')';
30
            } else if (is_array($p)) foreach ($p as $key => $value) {
31
                if(is_numeric($key)){
32
                  $w[] = '--'.$value;
33
                } else {
34
                  if(is_bool($value)){
35
                     if($value) $w[] = '--'.$key;
36
                  } else {
37
                    $w[] = '--'.$key.'='.escapeshellarg($value);
38
                  }
39
                }
40
            } else {
41
              $s[] = $p;
42
            }
43
        }
44
        return trim(
45
            '/usr/bin/env '.$command.' '.implode(' ',array_merge($w,$s))
46
        );
47
    }
48
49
    /**
50
     * Returns the compiled shell command
51
     * @return string
52
     */
53
    public function getShellCommand(){
54
        return $this->command;
55
    }
56
57
    public static function __callStatic($command,$params){
58
        $aliases = static::$aliases;
59
        // Check if is an alias
60
        if (isset($aliases[$command])){
61
            if(!$results = $aliases[$command](...$params))
62
                throw new Exception('Shell aliases must return a Shell class or a command string.');
63
            return $results instanceof static? $results : new static($results);
64
        } else {
65
            return new static($command,$params);
66
        }
67
    }
68
69
    public function __construct($command,$params=null){
70
        $this->command = $params?static::_compileCommand($command,$params):$command;
71
    }
72
73
    public function __toString(){
74
        $output = [];
75
        exec($this->command,$output,$error_code);
76
        return empty($output)?'':implode(PHP_EOL,$output);
77
    }
78
79
    /**
80
     * Concatenate multiple shell commands via piping
81
     * @return Shell The piped shell command
82
     */
83 View Code Duplication
    public static function pipe(/* ... */){
84
        $cmd = [];
85
        foreach (func_get_args() as $item) {
86
            $cmd[] = ($item instanceof static)?$item->getShellCommand():$item;
87
        }
88
        return new static(implode(' | ',$cmd));
89
    }
90
91
    /**
92
     * Concatenate multiple shell commands via logical implication ( && )
93
     * @return Shell The concatenated shell command
94
     */
95 View Code Duplication
    public static function sequence(...$items){
96
        $cmd = [];
97
        foreach ($items as $item) {
98
            $cmd[] = ($item instanceof static)?$item->getShellCommand():$item;
99
        }
100
        return new static(implode(' && ',$cmd));
101
    }
102
103
    public static function execCommand($command,$params = null){
104
        return new static($command,$params);
105
    }
106
107
    public static function alias($command,callable $callback){
108
        static::$aliases[$command] = $callback;
109
    }
110
111
    public static function escape($arg){
112
        return escapeshellarg($arg);
113
    }
114
115
    public function run(){
116
        return $this->__toString();
117
    }
118
119
} /* End of class */
120
121