From 8cb07a951eb050d1641be75591db6fa18b25b6e7 Mon Sep 17 00:00:00 2001 From: SonyPradana Date: Tue, 9 Jul 2024 15:13:22 +0700 Subject: [PATCH 1/3] chore: bump dependency - use component package instead use full package (sonypradana/collection, sonypradana/console) - formatting --- composer.json | 12 +++--------- src/JsonPrinter.php | 6 +++--- src/Printer.php | 5 ++--- tests/assets/here-count.php | 2 +- tests/assets/here-countAll.php | 2 +- tests/assets/here.php | 2 +- 6 files changed, 11 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index 244f739..48bf609 100644 --- a/composer.json +++ b/composer.json @@ -22,8 +22,9 @@ } ], "require": { - "php": "^7.4 || ^8.0", - "sonypradana/php-library": "^0.19", + "php": "^8.0", + "sonypradana/collection": "^0.35", + "sonypradana/console": "^0.35", "react/socket": "^1.12" }, "require-dev": { @@ -31,13 +32,6 @@ "friendsofphp/php-cs-fixer": "^3.17.0", "phpstan/phpstan": "^1.10.15" }, - "extra": { - "laravel": { - "dont-discover": [ - "*" - ] - } - }, "minimum-stability": "dev", "prefer-stable": true, "scripts": { diff --git a/src/JsonPrinter.php b/src/JsonPrinter.php index 3ca4b7e..124a5d0 100644 --- a/src/JsonPrinter.php +++ b/src/JsonPrinter.php @@ -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), ]); } diff --git a/src/Printer.php b/src/Printer.php index 39e55b8..ad28baa 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -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 { @@ -183,7 +182,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 @@ -209,7 +208,7 @@ 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) diff --git a/tests/assets/here-count.php b/tests/assets/here-count.php index ffb7bd9..f5f0eda 100644 --- a/tests/assets/here-count.php +++ b/tests/assets/here-count.php @@ -8,7 +8,7 @@ // some line code here('test'); -foreach (range(1, 25)as $line) { +foreach (range(1, 25) as $line) { here('test'); } diff --git a/tests/assets/here-countAll.php b/tests/assets/here-countAll.php index ffb7bd9..f5f0eda 100644 --- a/tests/assets/here-countAll.php +++ b/tests/assets/here-countAll.php @@ -8,7 +8,7 @@ // some line code here('test'); -foreach (range(1, 25)as $line) { +foreach (range(1, 25) as $line) { here('test'); } diff --git a/tests/assets/here.php b/tests/assets/here.php index ffb7bd9..f5f0eda 100644 --- a/tests/assets/here.php +++ b/tests/assets/here.php @@ -8,7 +8,7 @@ // some line code here('test'); -foreach (range(1, 25)as $line) { +foreach (range(1, 25) as $line) { here('test'); } From ba7db291040d8126167002af18aafd527bd89b78 Mon Sep 17 00:00:00 2001 From: SonyPradana Date: Wed, 10 Jul 2024 10:12:56 +0700 Subject: [PATCH 2/3] formatting: fix phpstan level max --- src/Commands/ServeCommand.php | 16 ++++++------- src/Config.php | 32 +++++++++++++++++++------- src/JsonPrinter.php | 14 ++++++----- src/Printer.php | 20 ++++++++-------- src/Styles/ArrayStyle.php | 6 ++--- src/Styles/ClassStyle.php | 6 ++--- tests/Unit/Styles/ClassStyleTest.php | 13 ++++++++++- tests/Unit/Styles/DefaultStyleTest.php | 2 +- 8 files changed, 70 insertions(+), 39 deletions(-) diff --git a/src/Commands/ServeCommand.php b/src/Commands/ServeCommand.php index ab903fa..3744f22 100644 --- a/src/Commands/ServeCommand.php +++ b/src/Commands/ServeCommand.php @@ -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(); @@ -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(); }); @@ -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', ]; @@ -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; diff --git a/src/Config.php b/src/Config.php index 0a3e3bd..ef75724 100644 --- a/src/Config.php +++ b/src/Config.php @@ -6,7 +6,7 @@ final class Config { - /** @var array */ + /** @var array */ private static $configs = []; /** @@ -36,7 +36,7 @@ public static function load($config_file = null) /** * Save current config to config file. * - * @param array $configs + * @param array $configs * * @return void */ @@ -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) { @@ -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 */ @@ -99,10 +99,26 @@ public static function flush() /** * Get cached config. * - * @return array + * @return array */ 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; + } } diff --git a/src/JsonPrinter.php b/src/JsonPrinter.php index 124a5d0..587616f 100644 --- a/src/JsonPrinter.php +++ b/src/JsonPrinter.php @@ -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 */ @@ -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; }); } diff --git a/src/Printer.php b/src/Printer.php index ad28baa..d8cd069 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -40,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) { @@ -53,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; }); } @@ -100,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); @@ -143,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) { @@ -160,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; @@ -215,6 +217,6 @@ private function printVar(&$style, $var, $margin_left) ? (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(); } } diff --git a/src/Styles/ArrayStyle.php b/src/Styles/ArrayStyle.php index e6edad6..f056ac4 100644 --- a/src/Styles/ArrayStyle.php +++ b/src/Styles/ArrayStyle.php @@ -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(); @@ -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; diff --git a/src/Styles/ClassStyle.php b/src/Styles/ClassStyle.php index 0a83bdb..d1c0837 100644 --- a/src/Styles/ClassStyle.php +++ b/src/Styles/ClassStyle.php @@ -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; } @@ -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); @@ -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(); diff --git a/tests/Unit/Styles/ClassStyleTest.php b/tests/Unit/Styles/ClassStyleTest.php index 9893ac9..ad988b9 100644 --- a/tests/Unit/Styles/ClassStyleTest.php +++ b/tests/Unit/Styles/ClassStyleTest.php @@ -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 @@ -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'; +} diff --git a/tests/Unit/Styles/DefaultStyleTest.php b/tests/Unit/Styles/DefaultStyleTest.php index 34f6e8a..dae2b94 100644 --- a/tests/Unit/Styles/DefaultStyleTest.php +++ b/tests/Unit/Styles/DefaultStyleTest.php @@ -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); From 0db3a481ec684e6648cc07f9425ee9dd05031392 Mon Sep 17 00:00:00 2001 From: SonyPradana Date: Wed, 10 Jul 2024 10:18:02 +0700 Subject: [PATCH 3/3] run composer bump --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 48bf609..f3e5076 100644 --- a/composer.json +++ b/composer.json @@ -25,12 +25,12 @@ "php": "^8.0", "sonypradana/collection": "^0.35", "sonypradana/console": "^0.35", - "react/socket": "^1.12" + "react/socket": "^1.15" }, "require-dev": { - "phpunit/phpunit": "^9.6.8", - "friendsofphp/php-cs-fixer": "^3.17.0", - "phpstan/phpstan": "^1.10.15" + "phpunit/phpunit": "^9.6", + "friendsofphp/php-cs-fixer": "^3.59", + "phpstan/phpstan": "^1.11" }, "minimum-stability": "dev", "prefer-stable": true,