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

chore: bump dependency and formatting #23

Merged
merged 3 commits into from
Jul 10, 2024
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: 7 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,15 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"sonypradana/php-library": "^0.19",
"react/socket": "^1.12"
"php": "^8.0",
"sonypradana/collection": "^0.35",
"sonypradana/console": "^0.35",
"react/socket": "^1.15"
},
"require-dev": {
"phpunit/phpunit": "^9.6.8",
"friendsofphp/php-cs-fixer": "^3.17.0",
"phpstan/phpstan": "^1.10.15"
},
"extra": {
"laravel": {
"dont-discover": [
"*"
]
}
"phpunit/phpunit": "^9.6",
"friendsofphp/php-cs-fixer": "^3.59",
"phpstan/phpstan": "^1.11"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
16 changes: 8 additions & 8 deletions src/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public function serve()
if (!$this->promt()) {
exit;
}
$style->new_lines()->flush();
$style->newLines()->flush();
}

/** @var string */
$uri = $this->OPTION[0] ?? Config::get('socket.uri', '127.0.0.1:8080');

// header information
$style->push('socket server')->textGreen()->new_lines();
$style->push('socket server')->textGreen()->newLines();
$style->tap(info($uri));
$style->push('listening...')->textDim()->out();

Expand All @@ -75,7 +75,7 @@ public function serve()

$socket->on('connection', function (\React\Socket\ConnectionInterface $connection) {
$connection->on('data', function ($chunk) {
style(now()->__toString())->textDim()->underline()
style((new \DateTime())->format('Y-m-d H:i:s'))->textDim()->underline()
->push($chunk)
->out();
});
Expand All @@ -88,7 +88,7 @@ public function serve()
public function help()
{
$this->command_describes = [
'serve' => 'Start socket server, default - ' . Config::get('socket.uri', '127.0.0.1:8080'),
'serve' => 'Start socket server, default - ' . Config::castString('socket.uri', '127.0.0.1:8080'),
'config' => 'Set config to config file',
'help' => 'Show help command information',
];
Expand Down Expand Up @@ -117,12 +117,12 @@ public function help()
$print
->textGreen()
->push(' command line application')->textBlue()
->new_lines(2);
->newLines(2);

$print->push('command:')->new_lines();
$print = $this->printCommands($print)->new_lines();
$print->push('command:')->newLines();
$print = $this->printCommands($print)->newLines();

$print->push('option:')->new_lines();
$print->push('option:')->newLines();
$print = $this->printOptions($print);

return $print;
Expand Down
32 changes: 24 additions & 8 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class Config
{
/** @var array<string, string|bool|int|null> */
/** @var array<string, string[]|string|bool|int|null> */
private static $configs = [];

/**
Expand Down Expand Up @@ -36,7 +36,7 @@ public static function load($config_file = null)
/**
* Save current config to config file.
*
* @param array<string, string|bool|int|null> $configs
* @param array<string, string[]|string|bool|int|null> $configs
*
* @return void
*/
Expand All @@ -57,10 +57,10 @@ private static function save($configs)
/**
* Get config by key.
*
* @param string $key
* @param string|int|bool|null $default
* @param string $key
* @param string[]|string|int|bool|null $default
*
* @return string|int|bool|null
* @return string[]|string|int|bool|null
*/
public static function get($key, $default = null)
{
Expand All @@ -74,8 +74,8 @@ public static function get($key, $default = null)
/**
* Set/create array item of config.
*
* @param string $key
* @param string|bool|int|null $val
* @param string $key
* @param string[]|string|bool|int|null $val
*
* @return void
*/
Expand All @@ -99,10 +99,26 @@ public static function flush()
/**
* Get cached config.
*
* @return array<string, string|bool|int|null>
* @return array<string, string[]|string|bool|int|null>
*/
public static function all()
{
return self::$configs;
}

/**
* Cast get return type as string.
*
* @param string $key
* @param string|null $default
*
* @return string
*/
public static function castString($key, $default = null)
{
/** @var string */
$string = self::get($key, $default);

return $string;
}
}
20 changes: 11 additions & 9 deletions src/JsonPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public function dump(...$var)
$snapshot = Here::getCapture($file, $capture);

$this->sendJson([
'file' => $this->content['file'],
'line' => $this->content['line'],
'snapshot' => array_map(fn ($trim) => trim($trim), $snapshot),
'file' => $this->content['file'],
'line' => $this->content['line'],
'snapshot' => array_map(fn ($trim) => trim($trim), $snapshot),
]);
}

Expand All @@ -51,20 +51,22 @@ public function count($group)
$group_file = [];
foreach (Here::getHere() as $files) {
if ($files['group'] === $group) {
$group_file[$files['file']][] = $files;
/** @var string */
$file = $files['file'];
$group_file[$file][] = $files;
}
}
// group by line
$groups = [];
foreach ($group_file as $file) {
foreach ($file as $result) {
foreach ($group_file as $files) {
foreach ($files as $result) {
if (in_array($result['line'], $groups)) {
continue;
}
$groups[] = $result['line'];

// counter
$count = array_filter($file, fn ($item) => $item['line'] === $result['line']);
$count = array_filter($files, fn ($item) => $item['line'] === $result['line']);

// build snapshot
/** @var array<int, int> */
Expand Down Expand Up @@ -121,14 +123,14 @@ protected function send($out)
return;
}

$uri = (string) Config::get('socket.uri', '127.0.0.1:8080');
$uri = Config::castString('socket.uri', '127.0.0.1:8080');
$out = $out === false ? '' : $out;

$connector = new \React\Socket\Connector();

$connector->connect($uri)->then(function (\React\Socket\ConnectionInterface $connection) use ($out) {
$connection->end($out);
}, function (\Exception $e) {
}, function (\Throwable $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
}
Expand Down
25 changes: 13 additions & 12 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Here\Styles\ArrayStyle;
use Here\Styles\VarStyle;
use System\Console\Style\Style;
use System\Text\Str;

final class Printer extends AbstractsPrinter
{
Expand Down Expand Up @@ -41,7 +40,7 @@ protected function send($out)
}

$use_socket = Config::get('socket.enable', false);
$uri = (string) Config::get('socket.uri', '127.0.0.1:8080');
$uri = Config::castString('socket.uri', '127.0.0.1:8080');
$out = $out === false ? '' : $out;

if ($use_socket === false) {
Expand All @@ -54,7 +53,7 @@ protected function send($out)

$connector->connect($uri)->then(function (\React\Socket\ConnectionInterface $connection) use ($out) {
$connection->end($out);
}, function (\Exception $e) {
}, function (\Throwable $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
}
Expand Down Expand Up @@ -101,19 +100,21 @@ public function count($group)
$group_file = [];
foreach (Here::getHere() as $files) {
if ($files['group'] === $group) {
$group_file[$files['file']][] = $files;
/** @var string */
$file = $files['file'];
$group_file[$file][] = $files;
}
}
// group by line
$groups = [];
foreach ($group_file as $file) {
foreach ($file as $result) {
foreach ($group_file as $files) {
foreach ($files as $result) {
if (in_array($result['line'], $groups)) {
continue;
}
$groups[] = $result['line'];

$count = array_filter($file, fn ($item) => $item['line'] === $result['line']);
$count = array_filter($files, fn ($item) => $item['line'] === $result['line']);

$this->printInfo($print, $result, count($count));
$this->printSnapshot($print, $result);
Expand Down Expand Up @@ -144,7 +145,7 @@ public function countAll()
/** {@inheritdoc} */
protected function printInfo(&$print, $content, $with_counter = false)
{
$print->new_lines();
$print->newLines();

$print->push(' work ')->textDarkGray()->bgGreen();
if ($with_counter !== false) {
Expand All @@ -161,7 +162,7 @@ protected function printInfo(&$print, $content, $with_counter = false)
/** {@inheritdoc} */
protected function printSnapshot(&$print, $content, ...$var)
{
$print->new_lines();
$print->newLines();

$count = count($var);
$has_var = $count > 0;
Expand All @@ -183,7 +184,7 @@ protected function printSnapshot(&$print, $content, ...$var)
$arrow = $current ? '-> ' : ' ';

$print->push($arrow)->textGreen();
$print->push(Str::fill((string) $line, ' ', $lenght) . ' | ' . $code)->textDim();
$print->push(str_pad((string) $line, $lenght, ' ', STR_PAD_LEFT) . ' | ' . $code)->textDim();
if ($current
&& $has_var === true
&& $this->EOL_var === false
Expand All @@ -209,13 +210,13 @@ protected function printSnapshot(&$print, $content, ...$var)
*/
private function printVar(&$style, $var, $margin_left)
{
$style->push(Str::fill('', ' ', $margin_left) . 'var : ')->textLightYellow();
$style->push(str_pad('', $margin_left, ' ', STR_PAD_LEFT) . 'var : ')->textLightYellow();

$tab_size = (int) round($margin_left / 2);
$style = is_array($var)
? (new ArrayStyle($style))->ref($var)->tabSize($tab_size)->render()
: (new VarStyle($style))->ref($var)->tabSize($tab_size)->render();

return $style->new_lines();
return $style->newLines();
}
}
6 changes: 3 additions & 3 deletions src/Styles/ArrayStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public function render(): Style
foreach ($var as $key => $value) {
$this->current_line++;
if ($this->current_line > $this->max_line) {
$this->style->new_lines()->repeat(' ', $this->tab_size * 2)->push('...')->textDim();
$this->style->newLines()->repeat(' ', $this->tab_size * 2)->push('...')->textDim();
break;
}

$this->style->new_lines();
$this->style->newLines();
$this->style->repeat(' ', $this->tab_size * 2);
$this->style->push($key)->textLightGreen();
$this->style->push(' => ')->textYellow();
Expand All @@ -53,7 +53,7 @@ public function render(): Style
$this->style = $style->render();
}

$this->style->new_lines()->repeat(' ', ($this->tab_size * 2) - 2);
$this->style->newLines()->repeat(' ', ($this->tab_size * 2) - 2);
$this->style->push(']')->textYellow();

return $this->style;
Expand Down
6 changes: 3 additions & 3 deletions src/Styles/ClassStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function render(): Style
foreach ($class->getDefaultProperties() as $name => $value) {
$this->current_line++;
if ($this->current_line > $this->max_line) {
$this->style->new_lines()->repeat(' ', $this->tab_size * 2)->push('...')->textDim();
$this->style->newLines()->repeat(' ', $this->tab_size * 2)->push('...')->textDim();
break;
}

Expand All @@ -52,7 +52,7 @@ public function render(): Style
break;
}

$this->style->new_lines();
$this->style->newLines();
$this->style->repeat(' ', $this->tab_size * 2);
$this->style->push($visible)->textYellow();
$this->style->push($name);
Expand All @@ -71,7 +71,7 @@ public function render(): Style
}

if ($class->hasMethod('__tostring')) {
$this->style->new_lines();
$this->style->newLines();
$this->style->repeat(' ', $this->tab_size * 2);
$this->style->push('__toString');
$this->style->push(': ')->textYellow();
Expand Down
13 changes: 12 additions & 1 deletion tests/Unit/Styles/ClassStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Here\Styles\ClassStyle;
use PHPUnit\Framework\TestCase;
use PHPUnit\Util\Test;
use System\Console\Style\Style;

final class ClaasStyleTest extends TestCase
Expand All @@ -14,9 +15,19 @@ public function itCanRenderVariableStyle()
$style = new Style();
$var = new ClassStyle($style);

$var->ref(now('04-09-2022'));
$var->ref(new TestClassStyle());
$out = $var->render()->__toString();

$this->assertStringContainsString('2022', $out);
}
}

class TestClassStyle
{
/**
* Public property to scan.
*
* @var string
*/
public $date = '04-09-2022';
}
2 changes: 1 addition & 1 deletion tests/Unit/Styles/DefaultStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function itCanRenderVariableStyle()
$style = new Style();
$var = new DefaultStyle($style);

$var->ref(now('04-09-2022')->__toString());
$var->ref((new DateTime('04-09-2022'))->format('Y-m-d H:i:s'));
$out = $var->render()->__toString();

$this->assertStringContainsString('2022', $out);
Expand Down
Loading
Loading