Completed
Push — master ( 82d2b1...d9ad16 )
by Andrii
04:10
created

DefaultGoal::getDeps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2015, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\goals;
13
14
use hidev\helpers\Helper;
15
use Yii;
16
17
/**
18
 * Default Goal.
19
 */
20
class DefaultGoal extends BaseGoal
21
{
22
    public $goalName;
23
24
    public $done = [];
25
26
    protected $_fileType = null;
27
28
    public function getFileType()
29
    {
30
        return $this->_fileType;
31
    }
32
33
    public function setFileType($type)
34
    {
35
        $this->_fileType = $type;
36
    }
37
38
    public function setDeps($deps)
39
    {
40
        $res = $this->getDeps();
41
        foreach (Helper::ksplit($deps) as $d => $e) {
42
            $res[is_int($d) ? $e : $d] = $e;
43
        }
44
        $this->setItem('deps', $res);
45
    }
46
47
    public function getDeps()
48
    {
49
        return Helper::ksplit($this->rawItem('deps'));
50
    }
51
52
    public function actionDeps()
53
    {
54
        foreach ($this->getDeps() as $name => $enabled) {
55
            if ($enabled) {
56
                $res = $this->module->runRequest($name);
57
                if (static::isNotOk($res)) {
58
                    return $res;
59
                }
60
            }
61
        }
62
63
        return 0;
64
    }
65
66
    public function runRequest($request)
67
    {
68
        return $request !== null ? $this->module->runRequest($request) : null;
69
    }
70
71
    /**
72
     * Runs array of requests. Stops on failure and returns exit code.
73
     * @param null|string|array $requests
74
     */
75
    public function runRequests($requests)
76
    {
77
        if (is_string($requests)) {
78
            $requests = [$requests];
79
        } elseif (!is_array($requests)) {
80
            return 0;
81
        }
82
        foreach ($requests as $request) {
83
            $res = $this->runRequest($request);
84
            if (static::isNotOk($res)) {
85
                return $res;
86
            }
87
        }
88
89
        return 0;
90
    }
91
92
    public function isDone($action, $timestamp = null)
0 ignored issues
show
Unused Code introduced by
The parameter $timestamp is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        if ($this->done[$action]) {
95
            Yii::trace("Already done: $this->goalName/$action");
96
97
            return true;
98
        }
99
100
        return false;
101
    }
102
103
    /**
104
     * Mark action as already done.
105
     *
106
     * @param string $action action id
107
     * @param int $time microtime when action was done, false for action was not done
108
     */
109
    public function markDone($action, $time = null)
110
    {
111
        $this->done[$action] = ($time === null || $time === true) ? microtime(1) : $time;
112
    }
113
114
    public function actionPerform($name = null, $path = null)
115
    {
116
        Yii::trace("Started: $this->goalName");
117
        return $this->runActions('deps, make');
118
    }
119
120
    public function actionLoad()
121
    {
122
        Yii::trace("Loading nothing for '$this->goalName'");
123
    }
124
125
    public function actionSave()
126
    {
127
        Yii::trace("Saving nothing for '$this->goalName'");
128
    }
129
130
    public function actionMake()
131
    {
132
        return $this->runActions('load, save');
133
    }
134
135
    public function runAction($id, $params = [])
136
    {
137
        if ($this->isDone($id)) {
138
            return;
139
        }
140
        $result = parent::runAction($id, $params);
141
        $this->markDone($id);
142
143
        return $result;
144
    }
145
146
    public function runActions($actions)
147
    {
148
        foreach (Helper::ksplit($actions) as $action) {
149
            $res = $this->runAction($action);
150
            if (static::isNotOk($res)) {
151
                return $res;
152
            }
153
        }
154
155
        return 0;
156
    }
157
158
    public static function isNotOk($res)
159
    {
160
        return is_object($res) ? $res->exitStatus : $res;
161
    }
162
163
    public function options($actionId)
164
    {
165
        return array_merge(parent::options($actionId), array_keys(Helper::getPublicVars(get_called_class())));
166
    }
167
168
    /**
169
     * Prepares given command arguments.
170
     * @param string|array $args
171
     * @return string
172
     */
173
    public function prepareCommand($args = '')
174
    {
175
        if (is_string($args)) {
176
            $res = ' ' . trim($args);
177
        } else {
178
            $res = '';
179
            foreach ($args as $a) {
180
                $res .= ' ' . escapeshellarg($a);
181
            }
182
        }
183
184
        return $res;
185
    }
186
187
    /**
188
     * Prepares given prog and given arguments and runs it with passthru.
189
     * @param string $prog
190
     * @param string $args
191
     * @return int exit code
192
     */
193
    public function passthru($prog, $args = '')
194
    {
195
        // die($this->config->install->getBin($prog) . $this->prepareCommand($args));
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
196
        passthru($this->config->install->getBin($prog) . $this->prepareCommand($args), $exitcode);
0 ignored issues
show
Documentation introduced by
The property config does not exist on object<hidev\goals\DefaultGoal>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
197
        return $exitcode;
198
    }
199
200
    public function getRobo()
201
    {
202
        return Yii::$app->get('robo');
203
    }
204
205
    public function getConfig()
206
    {
207
        return Yii::$app->get('config');
208
    }
209
210
    public function getPackage()
211
    {
212
        return $this->getConfig()->getItem('package');
213
    }
214
215
    public function getVendor()
216
    {
217
        return $this->getConfig()->getItem('vendor');
218
    }
219
220
    public function getVcs()
221
    {
222
        /// TODO determine VCS
223
        return $this->getConfig()->getItem('git');
224
    }
225
}
226