Skip to content

Commit d6a27f2

Browse files
authored
Merge pull request #40 from utopia-php/feat/rector-hoist
build: hoist baseline rector + phpstan configs to the root
1 parent f930ce6 commit d6a27f2

7 files changed

Lines changed: 82 additions & 151 deletions

File tree

src/CLI/Adapter.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Utopia\CLI;
46

57
abstract class Adapter
@@ -15,38 +17,26 @@ public function __construct(int $workerNum = 0)
1517
* Starts the Server.
1618
*
1719
* @param $callback
18-
* @return self
1920
*/
2021
abstract public function start($callback): self;
2122

2223
/**
2324
* Stops the Server.
24-
*
25-
* @return self
2625
*/
2726
abstract public function stop(): self;
2827

2928
/**
3029
* Is called when a Worker starts.
31-
*
32-
* @param callable $callback
33-
* @return self
3430
*/
3531
abstract public function onWorkerStart(callable $callback): self;
3632

3733
/**
3834
* Is called when a Worker stops.
39-
*
40-
* @param callable $callback
41-
* @return self
4235
*/
4336
abstract public function onWorkerStop(callable $callback): self;
4437

4538
/**
4639
* Is called when a job is processed.
47-
*
48-
* @param callable $callback
49-
* @return self
5040
*/
5141
abstract public function onJob(callable $callback): self;
5242
}

src/CLI/Adapters/Generic.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
class Generic extends Adapter
88
{
9-
public function __construct(int $workerNum = 0)
10-
{
11-
parent::__construct($workerNum);
12-
}
13-
149
public function start($callback): self
1510
{
1611
$callback();

src/CLI/Adapters/Swoole.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function stop(): self
3838

3939
public function onWorkerStart($callback): self
4040
{
41-
$this->pool->on('WorkerStart', function (Pool $pool, string $workerId) use ($callback) {
41+
$this->pool->on('WorkerStart', function (Pool $pool, string $workerId) use ($callback): void {
4242
\call_user_func($callback, $workerId);
4343
});
4444

@@ -47,7 +47,7 @@ public function onWorkerStart($callback): self
4747

4848
public function onWorkerStop(callable $callback): self
4949
{
50-
$this->pool->on('WorkerStop', function (Pool $pool, string $workerId) use ($callback) {
50+
$this->pool->on('WorkerStop', function (Pool $pool, string $workerId) use ($callback): void {
5151
\call_user_func($callback, $workerId);
5252
});
5353

src/CLI/CLI.php

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,16 @@ class CLI
1616
* Adapter
1717
*
1818
* Type of adapter to pass the callable to
19-
*
20-
* @var Adapter
2119
*/
2220
protected Adapter $adapter;
2321

2422
/**
2523
* Command
2624
*
2725
* The name of the command requested for this process
28-
*
29-
* @var string
3026
*/
3127
protected string $command = '';
3228

33-
/**
34-
* @var Container
35-
*/
3629
protected Container $container;
3730

3831
protected ?Container $parentContainer = null;
@@ -41,17 +34,13 @@ class CLI
4134
* Args
4235
*
4336
* List of arguments passed to this process
44-
*
45-
* @var array
4637
*/
4738
protected array $args = [];
4839

4940
/**
5041
* Tasks
5142
*
5243
* List of commands tasks for this CLI process
53-
*
54-
* @var array
5544
*/
5645
protected array $tasks = [];
5746

@@ -85,9 +74,6 @@ class CLI
8574
/**
8675
* CLI constructor.
8776
*
88-
* @param Adapter|null $adapter
89-
* @param array $args
90-
* @param Container|null $container
9177
*
9278
* @throws Exception
9379
*/
@@ -97,7 +83,7 @@ public function __construct(?Adapter $adapter = null, array $args = [], ?Contain
9783
throw new Exception('CLI tasks can only work from the command line');
9884
}
9985

100-
$this->args = $this->parse((! empty($args) || ! isset($_SERVER['argv'])) ? $args : $_SERVER['argv']);
86+
$this->args = $this->parse(($args !== [] || ! isset($_SERVER['argv'])) ? $args : $_SERVER['argv']);
10187

10288
@cli_set_process_title($this->command);
10389

@@ -110,8 +96,6 @@ public function __construct(?Adapter $adapter = null, array $args = [], ?Contain
11096
* Init
11197
*
11298
* Set a callback function that will be initialized on application start
113-
*
114-
* @return Hook
11599
*/
116100
public function init(): Hook
117101
{
@@ -125,8 +109,6 @@ public function init(): Hook
125109
* Shutdown
126110
*
127111
* Set a callback function that will be initialized on application end
128-
*
129-
* @return Hook
130112
*/
131113
public function shutdown(): Hook
132114
{
@@ -140,8 +122,6 @@ public function shutdown(): Hook
140122
* Error
141123
*
142124
* An error callback for failed or no matched requests
143-
*
144-
* @return Hook
145125
*/
146126
public function error(): Hook
147127
{
@@ -155,9 +135,6 @@ public function error(): Hook
155135
* Task
156136
*
157137
* Add a new command task
158-
*
159-
* @param string $name
160-
* @return Task
161138
*/
162139
public function task(string $name): Task
163140
{
@@ -171,8 +148,6 @@ public function task(string $name): Task
171148
/**
172149
* If a resource has been created return it, otherwise create it and then return it
173150
*
174-
* @param string $name
175-
* @return mixed
176151
*
177152
* @throws Exception
178153
*/
@@ -188,8 +163,6 @@ public function getResource(string $name): mixed
188163
/**
189164
* Get Resources By List
190165
*
191-
* @param array $list
192-
* @return array
193166
*
194167
* @throws Exception
195168
*/
@@ -207,10 +180,6 @@ public function getResources(array $list): array
207180
/**
208181
* Set a new resource callback
209182
*
210-
* @param string $name
211-
* @param callable $callback
212-
* @param array $dependencies
213-
* @return void
214183
*
215184
* @throws Exception
216185
*/
@@ -227,8 +196,6 @@ public function getContainer(): Container
227196
/**
228197
* task-name --foo=test
229198
*
230-
* @param array $args
231-
* @return array
232199
*
233200
* @throws Exception
234201
*/
@@ -268,7 +235,7 @@ public function parse(array $args): array
268235
* If there is only one element in a particular key
269236
* unshift the value out of the array
270237
*/
271-
if (\count($value) == 1) {
238+
if (\count($value) === 1) {
272239
$output[$key] = array_shift($output[$key]);
273240
}
274241
}
@@ -278,8 +245,6 @@ public function parse(array $args): array
278245

279246
/**
280247
* Find the command that should be triggered
281-
*
282-
* @return Task|null
283248
*/
284249
public function match(): ?Task
285250
{
@@ -290,8 +255,6 @@ public function match(): ?Task
290255
* Get Params
291256
* Get runtime params for the provided Hook
292257
*
293-
* @param Hook $hook
294-
* @return array
295258
*
296259
* @throws Exception
297260
*/
@@ -338,11 +301,11 @@ protected function getParams(Hook $hook): array
338301
*/
339302
public function run(): self
340303
{
341-
$this->adapter->start(function () {
304+
$this->adapter->start(function (): void {
342305
$command = $this->match();
343306

344307
try {
345-
if ($command) {
308+
if ($command instanceof \Utopia\CLI\Task) {
346309
foreach ($this->init as $hook) {
347310
\call_user_func_array($hook->getAction(), $this->getParams($hook));
348311
}
@@ -358,7 +321,7 @@ public function run(): self
358321
}
359322
} catch (Exception $e) {
360323
foreach ($this->errors as $hook) {
361-
$this->setResource('error', fn() => $e);
324+
$this->setResource('error', fn(): \Exception => $e);
362325
\call_user_func_array($hook->getAction(), $this->getParams($hook));
363326
}
364327
}
@@ -379,8 +342,6 @@ public function getTasks(): array
379342

380343
/**
381344
* Get list of all args
382-
*
383-
* @return array
384345
*/
385346
public function getArgs(): array
386347
{
@@ -392,8 +353,6 @@ public function getArgs(): array
392353
*
393354
* Creates an validator instance and validate given value with given rules.
394355
*
395-
* @param string $key
396-
* @param array $param
397356
* @param mixed $value
398357
*
399358
* @throws Exception
@@ -403,23 +362,18 @@ protected function validate(string $key, array $param, $value): void
403362
if ('' !== $value) {
404363
// checking whether the class exists
405364
$validator = $param['validator'];
406-
407365
if (\is_callable($validator)) {
408366
$validator = $validator();
409367
}
410-
411368
// is the validator object an instance of the Validator class
412369
if (! $validator instanceof Validator) {
413370
throw new Exception('Validator object is not an instance of the Validator class', 500);
414371
}
415-
416372
if (! $validator->isValid($value)) {
417373
throw new Exception('Invalid ' . $key . ': ' . $validator->getDescription(), 400);
418374
}
419-
} else {
420-
if (! $param['optional']) {
421-
throw new Exception('Param "' . $key . '" is not optional.', 400);
422-
}
375+
} elseif (! $param['optional']) {
376+
throw new Exception('Param "' . $key . '" is not optional.', 400);
423377
}
424378
}
425379

@@ -439,10 +393,6 @@ protected function validate(string $key, array $param, $value): void
439393
* empty string, which bypasses `validate()` for optional params) are
440394
* passed through unchanged so callers that use `''` as a sentinel for
441395
* "not set" keep working.
442-
*
443-
* @param Validator|callable $validator
444-
* @param mixed $value
445-
* @return mixed
446396
*/
447397
protected function coerce(Validator|callable $validator, mixed $value): mixed
448398
{
@@ -464,7 +414,7 @@ protected function coerce(Validator|callable $validator, mixed $value): mixed
464414

465415
$coerced = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
466416

467-
return $coerced === null ? $value : $coerced;
417+
return $coerced ?? $value;
468418
}
469419

470420
public function setContainer(Container $container): self
@@ -483,8 +433,7 @@ public function reset(): void
483433
private function camelCaseIt($key): string
484434
{
485435
$key = str_replace('-', '_', $key);
486-
$camelCase = lcfirst(str_replace('_', '', ucwords($key, '_')));
487436

488-
return $camelCase;
437+
return lcfirst(str_replace('_', '', ucwords($key, '_')));
489438
}
490439
}

src/CLI/Task.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Utopia\CLI;
46

57
use Utopia\Servers\Hook;
68

79
class Task extends Hook
810
{
9-
/**
10-
* @var string
11-
*/
1211
protected string $name = '';
1312

1413
/**
1514
* Task constructor.
16-
*
17-
* @param string $name
1815
*/
1916
public function __construct(string $name)
2017
{
@@ -23,8 +20,6 @@ public function __construct(string $name)
2320

2421
/**
2522
* Get Name
26-
*
27-
* @return string
2823
*/
2924
public function getName(): string
3025
{

0 commit comments

Comments
 (0)