Skip to content

Commit 3386a4c

Browse files
committed
style: format codes. add strict params types define
1 parent 79f6b85 commit 3386a4c

File tree

12 files changed

+63
-59
lines changed

12 files changed

+63
-59
lines changed

.github/workflows/php.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ jobs:
1010
strategy:
1111
fail-fast: true
1212
matrix:
13-
php: [7.1, 7.2, 7.3, 7.4] #
13+
php: [7.3, 7.4, 8.0]
1414
os: [ubuntu-latest, macOS-latest] # windows-latest,
15+
include:
16+
- os: 'ubuntu-latest'
17+
php: '7.2'
18+
phpunit: '8.5.13'
19+
- os: 'ubuntu-latest'
20+
php: '7.1'
21+
phpunit: '7.5.20'
1522

1623
steps:
1724
- name: Checkout

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
[![Latest Stable Version](http://img.shields.io/packagist/v/toolkit/cli-utils.svg)](https://packagist.org/packages/toolkit/cli-utils)
66
[![Github Actions Status](https://github.com/php-toolkit/cli-utils/workflows/Unit-tests/badge.svg)](https://github.com/php-toolkit/cli-utils/actions)
77

8-
Provide some useful utils for the php CLI.
8+
Provide some useful utils for the php CLI application.
99

1010
- Parse CLI arguments and options
1111
- Console color render
1212
- CLI code highlighter
1313
- Build simple CLI application
14-
- CLI env information helper
14+
- CLI ENV information helper
1515

1616
## Install
1717

src/App.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ protected function doHandle(): int
207207
/**
208208
* @param int $code
209209
*/
210-
public function stop($code = 0): void
210+
public function stop(int $code = 0): void
211211
{
212212
exit((int)$code);
213213
}

src/Cli.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
use const JSON_PRETTY_PRINT;
2727
use const JSON_UNESCAPED_SLASHES;
2828
use const PHP_EOL;
29-
use const PHP_WINDOWS_VERSION_BUILD;
30-
use const PHP_WINDOWS_VERSION_MAJOR;
31-
use const PHP_WINDOWS_VERSION_MINOR;
3229
use const STDOUT;
3330

3431
/**
@@ -62,7 +59,7 @@ public static function style(): Style
6259
}
6360

6461
/**
65-
* @param string $text
62+
* @param string $text
6663
* @param string|int|array|null $style
6764
*
6865
* @return string
@@ -102,7 +99,7 @@ public static function clog(string $msg, array $data = [], string $type = 'info'
10299
}
103100
}
104101

105-
$optString = $userOpts ? ' ' . implode(' ', $userOpts) : '';
102+
$optString = $userOpts ? ' ' . implode(' ', $userOpts) : '';
106103
$dataString = $data ? PHP_EOL . json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) : '';
107104

108105
self::writef('%s [%s]%s %s %s', date('Y/m/d H:i:s'), $type, $optString, trim($msg), $dataString);
@@ -147,14 +144,14 @@ public static function isSupportColor(): bool
147144
}
148145

149146
// fix for "Undefined constant STDOUT" error
150-
if (!\defined('STDOUT')) {
147+
if (!defined('STDOUT')) {
151148
return false;
152149
}
153-
150+
154151
$stream = STDOUT;
155152
if (\DIRECTORY_SEPARATOR === '\\') {
156153
return (\function_exists('sapi_windows_vt100_support')
157-
&& @sapi_windows_vt100_support($stream))
154+
&& @sapi_windows_vt100_support($stream))
158155
|| false !== getenv('ANSICON')
159156
|| 'ON' === getenv('ConEmuANSI')
160157
|| 'xterm' === getenv('TERM');
@@ -191,7 +188,7 @@ public static function isAnsiSupport(): bool
191188
/**
192189
* Returns if the file descriptor is an interactive terminal or not.
193190
*
194-
* @param int|resource $fileDescriptor
191+
* @param int|resource|mixed $fileDescriptor
195192
*
196193
* @return boolean
197194
*/

src/ColorCode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class ColorCode
130130
* @return ColorCode
131131
* @throws InvalidArgumentException
132132
*/
133-
public static function make($fg = '', $bg = '', array $options = [], bool $extra = false): ColorCode
133+
public static function make(string $fg = '', string $bg = '', array $options = [], bool $extra = false): ColorCode
134134
{
135135
return new self($fg, $bg, $options, $extra);
136136
}
@@ -144,7 +144,7 @@ public static function make($fg = '', $bg = '', array $options = [], bool $extra
144144
* @throws InvalidArgumentException
145145
* @throws RuntimeException
146146
*/
147-
public static function fromString(string $string)
147+
public static function fromString(string $string): ColorCode
148148
{
149149
$extra = false;
150150
$options = [];
@@ -188,7 +188,7 @@ public static function fromString(string $string)
188188
*
189189
* @throws InvalidArgumentException
190190
*/
191-
public function __construct($fg = '', $bg = '', array $options = [], bool $extra = false)
191+
public function __construct(string $fg = '', string $bg = '', array $options = [], bool $extra = false)
192192
{
193193
if ($fg) {
194194
if (!isset(self::KNOWN_COLORS[$fg])) {

src/Flags.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Flags
4545
public static function simpleParseArgv(array $argv): array
4646
{
4747
$args = $opts = [];
48-
foreach ($argv as $key => $value) {
48+
foreach ($argv as $value) {
4949
// opts
5050
if (strpos($value, '-') === 0) {
5151
$value = trim($value, '-');
@@ -278,7 +278,7 @@ public static function parseString(string $string): void
278278
*
279279
* @return bool|mixed
280280
*/
281-
public static function filterBool($val, $enable = true)
281+
public static function filterBool($val, bool $enable = true)
282282
{
283283
if ($enable) {
284284
if (is_bool($val) || is_numeric($val)) {

src/Style.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static function instance(): Style
102102
* @param string $bg 背景色
103103
* @param array $options 其它选项
104104
*/
105-
public function __construct($fg = '', $bg = '', array $options = [])
105+
public function __construct(string $fg = '', string $bg = '', array $options = [])
106106
{
107107
if ($fg || $bg || $options) {
108108
$this->add('base', $fg, $bg, $options);
@@ -118,7 +118,7 @@ public function __construct($fg = '', $bg = '', array $options = [])
118118
* @return mixed|string
119119
* @throws InvalidArgumentException
120120
*/
121-
public function __call($method, array $args)
121+
public function __call(string $method, array $args)
122122
{
123123
if (isset($args[0]) && $this->hasStyle($method)) {
124124
return $this->format(sprintf('<%s>%s</%s>', $method, $args[0], $method));
@@ -253,7 +253,7 @@ public static function stripColor(string $string)
253253
*
254254
* @return $this
255255
*/
256-
public function add(string $name, $fg = '', $bg = '', array $options = [], bool $extra = false): self
256+
public function add(string $name, $fg = '', string $bg = '', array $options = [], bool $extra = false): self
257257
{
258258
if (is_array($fg)) {
259259
return $this->addByArray($name, $fg);
@@ -373,9 +373,9 @@ public static function isNoColor(): bool
373373
/**
374374
* Method to set property noColor
375375
*
376-
* @param $noColor
376+
* @param bool $noColor
377377
*/
378-
public static function setNoColor($noColor = true): void
378+
public static function setNoColor(bool $noColor = true): void
379379
{
380380
Color::setNoColor($noColor);
381381
}

src/Traits/WriteMessageTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static function printf(string $format, ...$args): int
7373
*
7474
* @return int
7575
*/
76-
public static function writeRaw($message, $nl = true, $quit = false, array $opts = []): int
76+
public static function writeRaw($message, bool $nl = true, $quit = false, array $opts = []): int
7777
{
7878
$opts['color'] = false;
7979
return self::write($message, $nl, $quit, $opts);
@@ -138,7 +138,7 @@ public static function print($message, $quit = false, array $opts = []): int
138138
*
139139
* @return int
140140
*/
141-
public static function write($messages, $nl = true, $quit = false, array $opts = []): int
141+
public static function write($messages, bool $nl = true, $quit = false, array $opts = []): int
142142
{
143143
if (is_array($messages)) {
144144
$messages = implode($nl ? PHP_EOL : '', $messages);
@@ -263,7 +263,7 @@ public static function clearBuffer(): void
263263
* @return string If flush = False, will return all buffer text.
264264
* @see write()
265265
*/
266-
public static function stopBuffer($flush = true, $nl = false, $quit = false, array $opts = []): string
266+
public static function stopBuffer(bool $flush = true, bool $nl = false, bool $quit = false, array $opts = []): string
267267
{
268268
self::$buffering = false;
269269

@@ -290,7 +290,7 @@ public static function stopBuffer($flush = true, $nl = false, $quit = false, arr
290290
*
291291
* @see write()
292292
*/
293-
public static function flushBuffer($nl = false, $quit = false, array $opts = []): void
293+
public static function flushBuffer(bool $nl = false, bool $quit = false, array $opts = []): void
294294
{
295295
self::stopBuffer(true, $nl, $quit, $opts);
296296
}

src/Util/Download.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Toolkit\Cli\Util;
1111

1212
use RuntimeException;
13+
use Toolkit\Cli\Cli;
1314
use function basename;
1415
use function error_get_last;
1516
use function fclose;
@@ -90,7 +91,7 @@ public static function file(string $url, string $saveAs = '', string $type = sel
9091
* @param string $saveAs
9192
* @param string $type
9293
*/
93-
public function __construct(string $url, string $saveAs = '', $type = self::PROGRESS_TEXT)
94+
public function __construct(string $url, string $saveAs = '', string $type = self::PROGRESS_TEXT)
9495
{
9596
$this->setUrl($url);
9697
$this->setSaveAs($saveAs);
@@ -152,7 +153,7 @@ public function start(): self
152153
* @param int $transferredBytes Have been transferred bytes
153154
* @param int $maxBytes Target max length bytes
154155
*/
155-
public function progressShow($notifyCode, $severity, $message, $messageCode, $transferredBytes, $maxBytes): void
156+
public function progressShow(int $notifyCode, $severity, string $message, $messageCode, int $transferredBytes, int $maxBytes): void
156157
{
157158
$msg = '';
158159

@@ -214,7 +215,7 @@ public function showProgressByType($transferredBytes): string
214215
if ($size === null) {
215216
printf("\rUnknown file size... %2d kb done..", $tfKb);
216217
} else {
217-
$length = ceil(($transferredBytes / $size) * 100); // ■ =
218+
$length = (int)ceil(($transferredBytes / $size) * 100); // ■ =
218219
printf("\r[%-100s] %d%% (%2d/%2d kb)", str_repeat('=', $length) . '>', $length, $tfKb, $size / 1024);
219220
}
220221
} else {

src/Util/Highlighter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@ public function snippet(string $source, int $lineNumber, int $linesBefore = 2, i
173173
*
174174
* @return string
175175
*/
176-
public function highlightSnippet(string $source, int $lineNumber, $linesBefore = 2, $linesAfter = 2): string
176+
public function highlightSnippet(string $source, int $lineNumber, int $linesBefore = 2, int $linesAfter = 2): string
177177
{
178178
$tokenLines = $this->getHighlightedLines($source);
179179

180180
$offset = $lineNumber - $linesBefore - 1;
181181
$offset = max($offset, 0);
182182
$length = $linesAfter + $linesBefore + 1;
183183

184-
$tokenLines = array_slice($tokenLines, $offset, $length, $preserveKeys = true);
184+
$tokenLines = array_slice($tokenLines, $offset, $length, true);
185185

186186
$lines = $this->colorLines($tokenLines);
187187

0 commit comments

Comments
 (0)