Skip to content

Commit 35b691e

Browse files
vasuvasu
authored andcommitted
fixing pint issues
1 parent bd2b9a4 commit 35b691e

7 files changed

Lines changed: 87 additions & 83 deletions

File tree

src/CLI/CLI.php

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,17 @@ class CLI
7575
/**
7676
* CLI constructor.
7777
*
78-
* @param array $args
78+
* @param array $args
79+
*
7980
* @throws Exception
8081
*/
8182
public function __construct(array $args = [])
8283
{
83-
if (\php_sapi_name() !== "cli") {
84+
if (\php_sapi_name() !== 'cli') {
8485
throw new Exception('CLI tasks can only work from the command line');
8586
}
8687

87-
$this->args = $this->parse((!empty($args) || !isset($_SERVER['argv'])) ? $args : $_SERVER['argv']);
88+
$this->args = $this->parse((! empty($args) || ! isset($_SERVER['argv'])) ? $args : $_SERVER['argv']);
8889

8990
$this->error = function (Exception $error): void {
9091
Console::error($error->getMessage());
@@ -104,6 +105,7 @@ public function init(): Hook
104105
{
105106
$hook = new Hook();
106107
$this->init[] = $hook;
108+
107109
return $hook;
108110
}
109111

@@ -118,6 +120,7 @@ public function shutdown(): Hook
118120
{
119121
$hook = new Hook();
120122
$this->shutdown[] = $hook;
123+
121124
return $hook;
122125
}
123126

@@ -132,6 +135,7 @@ public function error(): Hook
132135
{
133136
$hook = new Hook();
134137
$this->errors[] = $hook;
138+
135139
return $hook;
136140
}
137141

@@ -140,8 +144,7 @@ public function error(): Hook
140144
*
141145
* Add a new command task
142146
*
143-
* @param string $name
144-
*
147+
* @param string $name
145148
* @return Task
146149
*/
147150
public function task(string $name): Task
@@ -156,16 +159,17 @@ public function task(string $name): Task
156159
/**
157160
* If a resource has been created return it, otherwise create it and then return it
158161
*
159-
* @param string $name
160-
* @param bool $fresh
162+
* @param string $name
163+
* @param bool $fresh
161164
* @return mixed
165+
*
162166
* @throws Exception
163167
*/
164168
public function getResource(string $name, bool $fresh = false): mixed
165169
{
166-
if (!\array_key_exists($name, $this->resources) || $fresh || self::$resourcesCallbacks[$name]['reset']) {
167-
if (!\array_key_exists($name, self::$resourcesCallbacks)) {
168-
throw new Exception('Failed to find resource: "' . $name . '"');
170+
if (! \array_key_exists($name, $this->resources) || $fresh || self::$resourcesCallbacks[$name]['reset']) {
171+
if (! \array_key_exists($name, self::$resourcesCallbacks)) {
172+
throw new Exception('Failed to find resource: "'.$name.'"');
169173
}
170174

171175
$this->resources[$name] = \call_user_func_array(
@@ -182,7 +186,7 @@ public function getResource(string $name, bool $fresh = false): mixed
182186
/**
183187
* Get Resources By List
184188
*
185-
* @param array $list
189+
* @param array $list
186190
* @return array
187191
*/
188192
public function getResources(array $list): array
@@ -199,13 +203,12 @@ public function getResources(array $list): array
199203
/**
200204
* Set a new resource callback
201205
*
202-
* @param string $name
203-
* @param callable $callback
204-
* @param array $injections
206+
* @param string $name
207+
* @param callable $callback
208+
* @param array $injections
209+
* @return void
205210
*
206211
* @throws Exception
207-
*
208-
* @return void
209212
*/
210213
public static function setResource(string $name, callable $callback, array $injections = []): void
211214
{
@@ -215,9 +218,10 @@ public static function setResource(string $name, callable $callback, array $inje
215218
/**
216219
* task-name --foo=test
217220
*
218-
* @param array $args
219-
* @throws Exception
221+
* @param array $args
220222
* @return array
223+
*
224+
* @throws Exception
221225
*/
222226
public function parse(array $args): array
223227
{
@@ -244,7 +248,7 @@ public function parse(array $args): array
244248
unset($arg);
245249

246250
foreach ($args as $arg) {
247-
$pair = explode("=", $arg);
251+
$pair = explode('=', $arg);
248252
$key = $pair[0];
249253
$value = $pair[1];
250254
$output[$key][] = $value;
@@ -277,7 +281,7 @@ public function match(): ?Task
277281
* Get Params
278282
* Get runtime params for the provided Hook
279283
*
280-
* @param Hook $hook
284+
* @param Hook $hook
281285
* @return array
282286
*/
283287
protected function getParams(Hook $hook): array
@@ -297,6 +301,7 @@ protected function getParams(Hook $hook): array
297301
}
298302

299303
ksort($params);
304+
300305
return $params;
301306
}
302307

@@ -359,9 +364,10 @@ public function getArgs(): array
359364
*
360365
* Creates an validator instance and validate given value with given rules.
361366
*
362-
* @param string $key
363-
* @param array $param
364-
* @param mixed $value
367+
* @param string $key
368+
* @param array $param
369+
* @param mixed $value
370+
*
365371
* @throws Exception
366372
*/
367373
protected function validate(string $key, array $param, $value): void
@@ -375,16 +381,16 @@ protected function validate(string $key, array $param, $value): void
375381
}
376382

377383
// is the validator object an instance of the Validator class
378-
if (!$validator instanceof Validator) {
384+
if (! $validator instanceof Validator) {
379385
throw new Exception('Validator object is not an instance of the Validator class', 500);
380386
}
381387

382-
if (!$validator->isValid($value)) {
383-
throw new Exception('Invalid ' . $key . ': ' . $validator->getDescription(), 400);
388+
if (! $validator->isValid($value)) {
389+
throw new Exception('Invalid '.$key.': '.$validator->getDescription(), 400);
384390
}
385391
} else {
386-
if (!$param['optional']) {
387-
throw new Exception('Param "' . $key . '" is not optional.', 400);
392+
if (! $param['optional']) {
393+
throw new Exception('Param "'.$key.'" is not optional.', 400);
388394
}
389395
}
390396
}

src/CLI/Console.php

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Console
99
*
1010
* Sets the process title visible in tools such as top and ps.
1111
*
12-
* @param string $title
12+
* @param string $title
1313
* @return bool
1414
*/
1515
public static function title(string $title): bool
@@ -22,84 +22,84 @@ public static function title(string $title): bool
2222
*
2323
* Log messages to console
2424
*
25-
* @param string $message
25+
* @param string $message
2626
* @return bool|int
2727
*/
2828
public static function log(string $message): int|false
2929
{
30-
return \fwrite(STDOUT, $message . "\n");
30+
return \fwrite(STDOUT, $message."\n");
3131
}
3232

3333
/**
3434
* Success
3535
*
3636
* Log success messages to console
3737
*
38-
* @param string $message
38+
* @param string $message
3939
* @return bool|int
4040
*/
4141
public static function success(string $message): int|false
4242
{
43-
return \fwrite(STDOUT, "\033[32m" . $message . "\033[0m\n");
43+
return \fwrite(STDOUT, "\033[32m".$message."\033[0m\n");
4444
}
4545

4646
/**
4747
* Error
4848
*
4949
* Log error messages to console
5050
*
51-
* @param string $message
51+
* @param string $message
5252
* @return bool|int
5353
*/
5454
public static function error(string $message): int|false
5555
{
56-
return \fwrite(STDERR, "\033[31m" . $message . "\033[0m\n");
56+
return \fwrite(STDERR, "\033[31m".$message."\033[0m\n");
5757
}
5858

5959
/**
6060
* Info
6161
*
6262
* Log informative messages to console
6363
*
64-
* @param string $message
64+
* @param string $message
6565
* @return bool|int
6666
*/
6767
public static function info(string $message): int|false
6868
{
69-
return \fwrite(STDOUT, "\033[34m" . $message . "\033[0m\n");
69+
return \fwrite(STDOUT, "\033[34m".$message."\033[0m\n");
7070
}
7171

7272
/**
7373
* Warning
7474
*
7575
* Log warning messages to console
7676
*
77-
* @param string $message
77+
* @param string $message
7878
* @return bool|int
7979
*/
8080
public static function warning(string $message): int|false
8181
{
82-
return \fwrite(STDERR, "\033[1;33m" . $message . "\033[0m\n");
82+
return \fwrite(STDERR, "\033[1;33m".$message."\033[0m\n");
8383
}
8484

8585
/**
8686
* Warning
8787
*
8888
* Log warning messages to console
8989
*
90-
* @param string $question
90+
* @param string $question
9191
* @return string
9292
*/
9393
public static function confirm(string $question): string
9494
{
95-
if (!self::isInteractive()) {
95+
if (! self::isInteractive()) {
9696
return '';
9797
}
9898

9999
self::log($question);
100100

101101
$handle = \fopen('php://stdin', 'r');
102-
$line = \trim(\fgets($handle));
102+
$line = \trim(\fgets($handle));
103103

104104
\fclose($handle);
105105

@@ -111,7 +111,7 @@ public static function confirm(string $question): string
111111
*
112112
* Log warning messages to console
113113
*
114-
* @param string $message
114+
* @param string $message
115115
* @return void
116116
*/
117117
public static function exit(int $status = 0): void
@@ -124,21 +124,21 @@ public static function exit(int $status = 0): void
124124
*
125125
* This function was inspired by: https://stackoverflow.com/a/13287902/2299554
126126
*
127-
* @param string $cmd
128-
* @param string $stdin
129-
* @param string $stdout
130-
* @param string $stderr
131-
* @param int $timeout
127+
* @param string $cmd
128+
* @param string $stdin
129+
* @param string $stdout
130+
* @param string $stderr
131+
* @param int $timeout
132132
* @return int
133133
*/
134134
public static function execute(string $cmd, string $stdin, string &$stdout, string &$stderr, int $timeout = -1): int
135135
{
136-
$cmd = '( ' . $cmd . ' ) 3>/dev/null ; echo $? >&3';
136+
$cmd = '( '.$cmd.' ) 3>/dev/null ; echo $? >&3';
137137

138138
$pipes = [];
139139
$process = \proc_open(
140140
$cmd,
141-
[['pipe','r'],['pipe','w'],['pipe','w'],['pipe', 'w']],
141+
[['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w'], ['pipe', 'w']],
142142
$pipes
143143
);
144144
$start = \time();
@@ -163,15 +163,16 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri
163163

164164
if ($timeout > 0 && \time() - $start > $timeout) {
165165
\proc_terminate($process, 9);
166+
166167
return 1;
167168
}
168169

169-
if (!\proc_get_status($process)['running']) {
170+
if (! \proc_get_status($process)['running']) {
170171
\fclose($pipes[1]);
171172
\fclose($pipes[2]);
172173
\proc_close($process);
173174

174-
$exitCode = (int) str_replace("\n", "", $status);
175+
$exitCode = (int) str_replace("\n", '', $status);
175176

176177
return $exitCode;
177178
}
@@ -189,21 +190,21 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri
189190
*/
190191
public static function isInteractive(): bool
191192
{
192-
return ('cli' === PHP_SAPI && defined('STDOUT'));
193+
return 'cli' === PHP_SAPI && defined('STDOUT');
193194
}
194195

195196
/**
196-
* @param callable $callback
197-
* @param float $sleep // in seconds!
198-
* @param callable $onError
197+
* @param callable $callback
198+
* @param float $sleep // in seconds!
199+
* @param callable $onError
199200
*/
200201
public static function loop(callable $callback, $sleep = 1 /* seconds */, callable $onError = null): void
201202
{
202203
gc_enable();
203204

204205
$time = 0;
205206

206-
while (!connection_aborted() || PHP_SAPI == "cli") {
207+
while (! connection_aborted() || PHP_SAPI == 'cli') {
207208
try {
208209
$callback();
209210
} catch (\Exception $e) {
@@ -227,7 +228,7 @@ public static function loop(callable $callback, $sleep = 1 /* seconds */, callab
227228

228229
$time = $time + $sleep;
229230

230-
if (PHP_SAPI == "cli") {
231+
if (PHP_SAPI == 'cli') {
231232
if ($time >= 60 * 5) { // Every 5 minutes
232233
$time = 0;
233234
gc_collect_cycles(); //Forces collection of any existing garbage cycles

0 commit comments

Comments
 (0)