Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Code Linter To ‘utopia-php/cli #20

Merged
merged 5 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Linter"

on: [pull_request]
jobs:
lint:
name: Linter
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2

- run: git checkout HEAD^2

- name: Run Linter
run: |
docker run --rm -v $PWD:/app composer sh -c \
"composer install --profile --ignore-platform-reqs && composer lint"
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
},
"require-dev": {
"phpunit/phpunit": "^9.3",
"squizlabs/php_codesniffer": "^3.6"
"squizlabs/php_codesniffer": "^3.6",
"vimeo/psalm": "4.0.1",
"laravel/pint": "1.2.*"
},
"minimum-stability": "dev"
}
56 changes: 31 additions & 25 deletions src/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ class CLI
/**
* CLI constructor.
*
* @param array $args
* @param array $args
*
* @throws Exception
*/
public function __construct(array $args = [])
{
if (\php_sapi_name() !== "cli") {
if (\php_sapi_name() !== 'cli') {
throw new Exception('CLI tasks can only work from the command line');
}

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

$this->error = function (Exception $error): void {
Console::error($error->getMessage());
Expand All @@ -104,6 +105,7 @@ public function init(): Hook
{
$hook = new Hook();
$this->init[] = $hook;

return $hook;
}

Expand All @@ -118,6 +120,7 @@ public function shutdown(): Hook
{
$hook = new Hook();
$this->shutdown[] = $hook;

return $hook;
}

Expand All @@ -132,6 +135,7 @@ public function error(): Hook
{
$hook = new Hook();
$this->errors[] = $hook;

return $hook;
}

Expand All @@ -140,8 +144,7 @@ public function error(): Hook
*
* Add a new command task
*
* @param string $name
*
* @param string $name
* @return Task
*/
public function task(string $name): Task
Expand All @@ -156,15 +159,16 @@ public function task(string $name): Task
/**
* If a resource has been created return it, otherwise create it and then return it
*
* @param string $name
* @param bool $fresh
* @param string $name
* @param bool $fresh
* @return mixed
*
* @throws Exception
*/
public function getResource(string $name, bool $fresh = false): mixed
{
if (!\array_key_exists($name, $this->resources) || $fresh || self::$resourcesCallbacks[$name]['reset']) {
if (!\array_key_exists($name, self::$resourcesCallbacks)) {
if (! \array_key_exists($name, $this->resources) || $fresh || self::$resourcesCallbacks[$name]['reset']) {
if (! \array_key_exists($name, self::$resourcesCallbacks)) {
throw new Exception('Failed to find resource: "' . $name . '"');
}

Expand All @@ -182,7 +186,7 @@ public function getResource(string $name, bool $fresh = false): mixed
/**
* Get Resources By List
*
* @param array $list
* @param array $list
* @return array
*/
public function getResources(array $list): array
Expand All @@ -199,13 +203,12 @@ public function getResources(array $list): array
/**
* Set a new resource callback
*
* @param string $name
* @param callable $callback
* @param array $injections
* @param string $name
* @param callable $callback
* @param array $injections
* @return void
*
* @throws Exception
*
* @return void
*/
public static function setResource(string $name, callable $callback, array $injections = []): void
{
Expand All @@ -215,9 +218,10 @@ public static function setResource(string $name, callable $callback, array $inje
/**
* task-name --foo=test
*
* @param array $args
* @throws Exception
* @param array $args
* @return array
*
* @throws Exception
*/
public function parse(array $args): array
{
Expand All @@ -244,7 +248,7 @@ public function parse(array $args): array
unset($arg);

foreach ($args as $arg) {
$pair = explode("=", $arg);
$pair = explode('=', $arg);
$key = $pair[0];
$value = $pair[1];
$output[$key][] = $value;
Expand Down Expand Up @@ -277,7 +281,7 @@ public function match(): ?Task
* Get Params
* Get runtime params for the provided Hook
*
* @param Hook $hook
* @param Hook $hook
* @return array
*/
protected function getParams(Hook $hook): array
Expand All @@ -297,6 +301,7 @@ protected function getParams(Hook $hook): array
}

ksort($params);

return $params;
}

Expand Down Expand Up @@ -359,9 +364,10 @@ public function getArgs(): array
*
* Creates an validator instance and validate given value with given rules.
*
* @param string $key
* @param array $param
* @param mixed $value
* @param string $key
* @param array $param
* @param mixed $value
*
* @throws Exception
*/
protected function validate(string $key, array $param, $value): void
Expand All @@ -375,15 +381,15 @@ protected function validate(string $key, array $param, $value): void
}

// is the validator object an instance of the Validator class
if (!$validator instanceof Validator) {
if (! $validator instanceof Validator) {
throw new Exception('Validator object is not an instance of the Validator class', 500);
}

if (!$validator->isValid($value)) {
if (! $validator->isValid($value)) {
throw new Exception('Invalid ' . $key . ': ' . $validator->getDescription(), 400);
}
} else {
if (!$param['optional']) {
if (! $param['optional']) {
throw new Exception('Param "' . $key . '" is not optional.', 400);
}
}
Expand Down
49 changes: 25 additions & 24 deletions src/CLI/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Console
*
* Sets the process title visible in tools such as top and ps.
*
* @param string $title
* @param string $title
* @return bool
*/
public static function title(string $title): bool
Expand All @@ -22,7 +22,7 @@ public static function title(string $title): bool
*
* Log messages to console
*
* @param string $message
* @param string $message
* @return bool|int
*/
public static function log(string $message): int|false
Expand All @@ -35,7 +35,7 @@ public static function log(string $message): int|false
*
* Log success messages to console
*
* @param string $message
* @param string $message
* @return bool|int
*/
public static function success(string $message): int|false
Expand All @@ -48,7 +48,7 @@ public static function success(string $message): int|false
*
* Log error messages to console
*
* @param string $message
* @param string $message
* @return bool|int
*/
public static function error(string $message): int|false
Expand All @@ -61,7 +61,7 @@ public static function error(string $message): int|false
*
* Log informative messages to console
*
* @param string $message
* @param string $message
* @return bool|int
*/
public static function info(string $message): int|false
Expand All @@ -74,7 +74,7 @@ public static function info(string $message): int|false
*
* Log warning messages to console
*
* @param string $message
* @param string $message
* @return bool|int
*/
public static function warning(string $message): int|false
Expand All @@ -87,19 +87,19 @@ public static function warning(string $message): int|false
*
* Log warning messages to console
*
* @param string $question
* @param string $question
* @return string
*/
public static function confirm(string $question): string
{
if (!self::isInteractive()) {
if (! self::isInteractive()) {
return '';
}

self::log($question);

$handle = \fopen('php://stdin', 'r');
$line = \trim(\fgets($handle));
$line = \trim(\fgets($handle));

\fclose($handle);

Expand All @@ -111,7 +111,7 @@ public static function confirm(string $question): string
*
* Log warning messages to console
*
* @param string $message
* @param string $message
* @return void
*/
public static function exit(int $status = 0): void
Expand All @@ -124,11 +124,11 @@ public static function exit(int $status = 0): void
*
* This function was inspired by: https://stackoverflow.com/a/13287902/2299554
*
* @param string $cmd
* @param string $stdin
* @param string $stdout
* @param string $stderr
* @param int $timeout
* @param string $cmd
* @param string $stdin
* @param string $stdout
* @param string $stderr
* @param int $timeout
* @return int
*/
public static function execute(string $cmd, string $stdin, string &$stdout, string &$stderr, int $timeout = -1): int
Expand All @@ -138,7 +138,7 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri
$pipes = [];
$process = \proc_open(
$cmd,
[['pipe','r'],['pipe','w'],['pipe','w'],['pipe', 'w']],
[['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w'], ['pipe', 'w']],
$pipes
);
$start = \time();
Expand All @@ -163,15 +163,16 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri

if ($timeout > 0 && \time() - $start > $timeout) {
\proc_terminate($process, 9);

return 1;
}

if (!\proc_get_status($process)['running']) {
if (! \proc_get_status($process)['running']) {
\fclose($pipes[1]);
\fclose($pipes[2]);
\proc_close($process);

$exitCode = (int) str_replace("\n", "", $status);
$exitCode = (int) str_replace("\n", '', $status);

return $exitCode;
}
Expand All @@ -189,21 +190,21 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri
*/
public static function isInteractive(): bool
{
return ('cli' === PHP_SAPI && defined('STDOUT'));
return 'cli' === PHP_SAPI && defined('STDOUT');
}

/**
* @param callable $callback
* @param float $sleep // in seconds!
* @param callable $onError
* @param callable $callback
* @param float $sleep // in seconds!
* @param callable $onError
*/
public static function loop(callable $callback, $sleep = 1 /* seconds */, callable $onError = null): void
{
gc_enable();

$time = 0;

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

$time = $time + $sleep;

if (PHP_SAPI == "cli") {
if (PHP_SAPI == 'cli') {
if ($time >= 60 * 5) { // Every 5 minutes
$time = 0;
gc_collect_cycles(); //Forces collection of any existing garbage cycles
Expand Down
6 changes: 2 additions & 4 deletions src/CLI/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Utopia\CLI;

use Utopia\Validator;
use Exception;
use Utopia\Hook;

class Task extends Hook
Expand All @@ -13,10 +11,10 @@ class Task extends Hook
*/
protected string $name = '';


/**
* Task constructor.
* @param string $name
*
* @param string $name
*/
public function __construct(string $name)
{
Expand Down
Loading