Skip to content

Commit f24fee0

Browse files
Merge pull request #20 from vasucp1207/linter
Add Code Linter To ‘utopia-php/cli
2 parents 88bcab0 + c9bc018 commit f24fee0

File tree

8 files changed

+86
-60
lines changed

8 files changed

+86
-60
lines changed

.github/workflows/linter.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: "Linter"
2+
3+
on: [pull_request]
4+
jobs:
5+
lint:
6+
name: Linter
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: Checkout repository
11+
uses: actions/checkout@v3
12+
with:
13+
fetch-depth: 2
14+
15+
- run: git checkout HEAD^2
16+
17+
- name: Run Linter
18+
run: |
19+
docker run --rm -v $PWD:/app composer sh -c \
20+
"composer install --profile --ignore-platform-reqs && composer lint"

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
},
2020
"require-dev": {
2121
"phpunit/phpunit": "^9.3",
22-
"squizlabs/php_codesniffer": "^3.6"
22+
"squizlabs/php_codesniffer": "^3.6",
23+
"vimeo/psalm": "4.0.1",
24+
"laravel/pint": "1.2.*"
2325
},
2426
"minimum-stability": "dev"
2527
}

src/CLI/CLI.php

Lines changed: 31 additions & 25 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
@\cli_set_process_title($this->command);
9091
}
@@ -100,6 +101,7 @@ public function init(): Hook
100101
{
101102
$hook = new Hook();
102103
$this->init[] = $hook;
104+
103105
return $hook;
104106
}
105107

@@ -114,6 +116,7 @@ public function shutdown(): Hook
114116
{
115117
$hook = new Hook();
116118
$this->shutdown[] = $hook;
119+
117120
return $hook;
118121
}
119122

@@ -128,6 +131,7 @@ public function error(): Hook
128131
{
129132
$hook = new Hook();
130133
$this->errors[] = $hook;
134+
131135
return $hook;
132136
}
133137

@@ -136,8 +140,7 @@ public function error(): Hook
136140
*
137141
* Add a new command task
138142
*
139-
* @param string $name
140-
*
143+
* @param string $name
141144
* @return Task
142145
*/
143146
public function task(string $name): Task
@@ -152,15 +155,16 @@ public function task(string $name): Task
152155
/**
153156
* If a resource has been created return it, otherwise create it and then return it
154157
*
155-
* @param string $name
156-
* @param bool $fresh
158+
* @param string $name
159+
* @param bool $fresh
157160
* @return mixed
161+
*
158162
* @throws Exception
159163
*/
160164
public function getResource(string $name, bool $fresh = false): mixed
161165
{
162-
if (!\array_key_exists($name, $this->resources) || $fresh || self::$resourcesCallbacks[$name]['reset']) {
163-
if (!\array_key_exists($name, self::$resourcesCallbacks)) {
166+
if (! \array_key_exists($name, $this->resources) || $fresh || self::$resourcesCallbacks[$name]['reset']) {
167+
if (! \array_key_exists($name, self::$resourcesCallbacks)) {
164168
throw new Exception('Failed to find resource: "' . $name . '"');
165169
}
166170

@@ -178,7 +182,7 @@ public function getResource(string $name, bool $fresh = false): mixed
178182
/**
179183
* Get Resources By List
180184
*
181-
* @param array $list
185+
* @param array $list
182186
* @return array
183187
*/
184188
public function getResources(array $list): array
@@ -195,13 +199,12 @@ public function getResources(array $list): array
195199
/**
196200
* Set a new resource callback
197201
*
198-
* @param string $name
199-
* @param callable $callback
200-
* @param array $injections
202+
* @param string $name
203+
* @param callable $callback
204+
* @param array $injections
205+
* @return void
201206
*
202207
* @throws Exception
203-
*
204-
* @return void
205208
*/
206209
public static function setResource(string $name, callable $callback, array $injections = []): void
207210
{
@@ -211,9 +214,10 @@ public static function setResource(string $name, callable $callback, array $inje
211214
/**
212215
* task-name --foo=test
213216
*
214-
* @param array $args
215-
* @throws Exception
217+
* @param array $args
216218
* @return array
219+
*
220+
* @throws Exception
217221
*/
218222
public function parse(array $args): array
219223
{
@@ -240,7 +244,7 @@ public function parse(array $args): array
240244
unset($arg);
241245

242246
foreach ($args as $arg) {
243-
$pair = explode("=", $arg);
247+
$pair = explode('=', $arg);
244248
$key = $pair[0];
245249
$value = $pair[1];
246250
$output[$key][] = $value;
@@ -273,7 +277,7 @@ public function match(): ?Task
273277
* Get Params
274278
* Get runtime params for the provided Hook
275279
*
276-
* @param Hook $hook
280+
* @param Hook $hook
277281
* @return array
278282
*/
279283
protected function getParams(Hook $hook): array
@@ -293,6 +297,7 @@ protected function getParams(Hook $hook): array
293297
}
294298

295299
ksort($params);
300+
296301
return $params;
297302
}
298303

@@ -355,9 +360,10 @@ public function getArgs(): array
355360
*
356361
* Creates an validator instance and validate given value with given rules.
357362
*
358-
* @param string $key
359-
* @param array $param
360-
* @param mixed $value
363+
* @param string $key
364+
* @param array $param
365+
* @param mixed $value
366+
*
361367
* @throws Exception
362368
*/
363369
protected function validate(string $key, array $param, $value): void
@@ -371,15 +377,15 @@ protected function validate(string $key, array $param, $value): void
371377
}
372378

373379
// is the validator object an instance of the Validator class
374-
if (!$validator instanceof Validator) {
380+
if (! $validator instanceof Validator) {
375381
throw new Exception('Validator object is not an instance of the Validator class', 500);
376382
}
377383

378-
if (!$validator->isValid($value)) {
384+
if (! $validator->isValid($value)) {
379385
throw new Exception('Invalid ' . $key . ': ' . $validator->getDescription(), 400);
380386
}
381387
} else {
382-
if (!$param['optional']) {
388+
if (! $param['optional']) {
383389
throw new Exception('Param "' . $key . '" is not optional.', 400);
384390
}
385391
}

src/CLI/Console.php

Lines changed: 25 additions & 24 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,7 +22,7 @@ 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
@@ -35,7 +35,7 @@ public static function log(string $message): int|false
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
@@ -48,7 +48,7 @@ public static function success(string $message): int|false
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
@@ -61,7 +61,7 @@ public static function error(string $message): int|false
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
@@ -74,7 +74,7 @@ public static function info(string $message): int|false
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
@@ -87,19 +87,19 @@ public static function warning(string $message): int|false
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,11 +124,11 @@ 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
@@ -138,7 +138,7 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri
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

src/CLI/Task.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Utopia\CLI;
44

5-
use Utopia\Validator;
6-
use Exception;
75
use Utopia\Hook;
86

97
class Task extends Hook
@@ -13,10 +11,10 @@ class Task extends Hook
1311
*/
1412
protected string $name = '';
1513

16-
1714
/**
1815
* Task constructor.
19-
* @param string $name
16+
*
17+
* @param string $name
2018
*/
2119
public function __construct(string $name)
2220
{

0 commit comments

Comments
 (0)