Page MenuHomePhabricator

D658.id2044.diff
No OneTemporary

D658.id2044.diff

diff --git a/.gitignore b/.gitignore
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
/vendor/
+/nbproject/private/
diff --git a/composer.json b/composer.json
--- a/composer.json
+++ b/composer.json
@@ -9,8 +9,17 @@
"email": "cesar@magic3w.com"
}
],
+
+ "scripts": {
+ "test": [
+ "./vendor/bin/phpstan analyse ./src --level 7"
+ ]
+ },
"require": {
"ext-posix" : "*",
"spitfire/collection": "0.1"
+ },
+ "require-dev": {
+ "phpstan/phpstan": "^0.12.82"
}
}
diff --git a/composer.lock b/composer.lock
--- a/composer.lock
+++ b/composer.lock
@@ -4,11 +4,11 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
- "content-hash": "c7fccaedf054a4ca0516b9ddcd7a158f",
+ "content-hash": "b33b6e05a267e8bdbbc05feaddf4a405",
"packages": [
{
"name": "spitfire/collection",
- "version": "dev-master",
+ "version": "0.1.0",
"source": {
"type": "git",
"url": "https://phabricator.magic3w.com/source/spitfire-collection.git",
@@ -17,7 +17,6 @@
"require-dev": {
"phpunit/phpunit": "^9.4"
},
- "default-branch": true,
"type": "library",
"autoload": {
"psr-4": {
@@ -38,12 +37,71 @@
"time": "2020-10-19T09:26:22+00:00"
}
],
- "packages-dev": [],
+ "packages-dev": [
+ {
+ "name": "phpstan/phpstan",
+ "version": "0.12.82",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/phpstan/phpstan.git",
+ "reference": "3920f0fb0aff39263d3a4cb0bca120a67a1a6a11"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/phpstan/phpstan/zipball/3920f0fb0aff39263d3a4cb0bca120a67a1a6a11",
+ "reference": "3920f0fb0aff39263d3a4cb0bca120a67a1a6a11",
+ "shasum": ""
+ },
+ "require": {
+ "php": "^7.1|^8.0"
+ },
+ "conflict": {
+ "phpstan/phpstan-shim": "*"
+ },
+ "bin": [
+ "phpstan",
+ "phpstan.phar"
+ ],
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "0.12-dev"
+ }
+ },
+ "autoload": {
+ "files": [
+ "bootstrap.php"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "description": "PHPStan - PHP Static Analysis Tool",
+ "support": {
+ "issues": "https://github.com/phpstan/phpstan/issues",
+ "source": "https://github.com/phpstan/phpstan/tree/0.12.82"
+ },
+ "funding": [
+ {
+ "url": "https://github.com/ondrejmirtes",
+ "type": "github"
+ },
+ {
+ "url": "https://www.patreon.com/phpstan",
+ "type": "patreon"
+ },
+ {
+ "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan",
+ "type": "tidelift"
+ }
+ ],
+ "time": "2021-03-19T06:08:17+00:00"
+ }
+ ],
"aliases": [],
"minimum-stability": "stable",
- "stability-flags": {
- "spitfire/collection": 20
- },
+ "stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
diff --git a/nbproject/project.properties b/nbproject/project.properties
new file mode 100644
--- /dev/null
+++ b/nbproject/project.properties
@@ -0,0 +1,7 @@
+include.path=${php.global.include.path}
+php.version=PHP_74
+source.encoding=UTF-8
+src.dir=src
+tags.asp=false
+tags.short=false
+web.root=.
diff --git a/nbproject/project.xml b/nbproject/project.xml
new file mode 100644
--- /dev/null
+++ b/nbproject/project.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://www.netbeans.org/ns/project/1">
+ <type>org.netbeans.modules.php.project</type>
+ <configuration>
+ <data xmlns="http://www.netbeans.org/ns/php-project/1">
+ <name>cli</name>
+ </data>
+ </configuration>
+</project>
diff --git a/phpstan.neon b/phpstan.neon
new file mode 100644
--- /dev/null
+++ b/phpstan.neon
@@ -0,0 +1,4 @@
+parameters:
+ level: 7
+ paths:
+ - src/
\ No newline at end of file
diff --git a/src/CLIColor.php b/src/CLIColor.php
--- a/src/CLIColor.php
+++ b/src/CLIColor.php
@@ -40,8 +40,13 @@
const RESET = '0';
-
- public function color($color) {
+ /**
+ *
+ * @param string $color
+ * @return string
+ */
+ public function color(string $color) : string
+ {
return "\033[{$color}m";
}
}
diff --git a/src/Console.php b/src/Console.php
--- a/src/Console.php
+++ b/src/Console.php
@@ -27,22 +27,52 @@
class Console
{
+ /**
+ * The STDOUT stream.
+ *
+ * @var Stream
+ */
private $stdout;
+
+ /**
+ * The STDERR stream.
+ *
+ * @var Stream
+ */
private $stderr;
+ /**
+ * @var CLIColor
+ */
private $colors;
- private $current = null;
+ /**
+ * The currently active stream.
+ *
+ * @var Stream
+ */
+ private $current;
- public function __construct() {
- $this->stdout = new Stream(STDOUT);
+ /**
+ * Instances a new console interface. This allows the application to output data to the command
+ * line, providing utils for printing labels and progress indicators.
+ */
+ public function __construct()
+ {
+ $this->stdout = $this->current = new Stream(STDOUT);
$this->stderr = new Stream(STDERR);
$this->colors = new CLIColor();
}
- public function error($msg) {
- $this->current = $this->stdout;
+ /**
+ *
+ * @param string $msg The message to print.
+ * @return Console
+ */
+ public function error(string $msg) : Console
+ {
+ $this->current = $this->stderr;
$out = str_replace(PHP_EOL, PHP_EOL . ' ', trim($msg));
$this->current
@@ -56,6 +86,13 @@
return $this;
}
+
+ /**
+ * Prints a message labeled as information
+ *
+ * @param string $msg The message to print.
+ * @return Console
+ */
public function info($msg) {
$this->current = $this->stdout;
$out = str_replace(PHP_EOL, PHP_EOL . ' ', trim($msg));
@@ -71,6 +108,12 @@
return $this;
}
+ /**
+ * Prints a success message.
+ *
+ * @param string $msg The message to print.
+ * @return Console
+ */
public function success($msg) {
$this->current = $this->stdout;
$out = str_replace(PHP_EOL, PHP_EOL . ' ', trim($msg));
@@ -86,35 +129,71 @@
return $this;
}
- public function progress($msg) {
- return $this->current = new ProgressBar($msg);
+
+ /**
+ *
+ * @param string $msg The message to print.
+ * @return ProgressBar
+ */
+ public function progress($msg)
+ {
+ return new ProgressBar($msg);
}
+ /**
+ *
+ * @return Stream
+ */
public function stdout() {
return $this->stdout;
}
+ /**
+ *
+ * @return Stream
+ */
public function stderr() {
return $this->stderr;
}
+ /**
+ *
+ * @return Console
+ */
public function rewind() {
- $this->current && $this->current->rewind();
+ $this->current->rewind();
return $this;
}
- public function up($lines = 1) {
- $this->current && $this->current->up($lines);
+ /**
+ *
+ * @param int $lines
+ * @return Console
+ */
+ public function up($lines = 1)
+ {
+ $this->current->up($lines);
return $this;
}
- public function down($lines = 1) {
- $this->current && $this->current->down($lines);
+ /**
+ *
+ * @param int $lines
+ * @return Console
+ */
+ public function down(int $lines = 1)
+ {
+ $this->current->down($lines);
return $this;
}
- public function ln() {
- $this->current && $this->current->line();
+ /**
+ *
+ * @return Console
+ */
+ public function ln()
+ {
+ $this->current->line();
return $this;
}
diff --git a/src/ProgressBar.php b/src/ProgressBar.php
--- a/src/ProgressBar.php
+++ b/src/ProgressBar.php
@@ -63,6 +63,8 @@
/**
* Instances a new progress bar.
+ *
+ * @param string $msg
*/
public function __construct($msg) {
$this->stream = new Stream();
diff --git a/src/Stream.php b/src/Stream.php
--- a/src/Stream.php
+++ b/src/Stream.php
@@ -27,9 +27,17 @@
class Stream
{
+ /**
+ *
+ * @var resource
+ */
private $stream;
- public function __construct($stream = STDOUT) {
+ /**
+ * @param resource $stream
+ */
+ public function __construct($stream = STDOUT)
+ {
$this->stream = $stream;
}
@@ -43,19 +51,41 @@
return $this;
}
- public function down($lines = 1) {
+ /**
+ *
+ * @param int $lines
+ * @return Stream
+ */
+ public function down(int $lines = 1) : Stream
+ {
return $this->out("\e[${lines}B");
}
- public function up($lines = 1) {
+ /**
+ *
+ * @param int $lines
+ * @return Stream
+ */
+ public function up(int $lines = 1) : Stream
+ {
return $this->out("\e[${lines}A");
}
- public function rewind() {
+ /**
+ *
+ * @return Stream
+ */
+ public function rewind() : Stream
+ {
return $this->out("\r" . exec('tput el'));
}
- public function line() {
+ /**
+ *
+ * @return Stream
+ */
+ public function line() : Stream
+ {
return $this->out(PHP_EOL);
}
@@ -75,7 +105,8 @@
*
* @return bool
*/
- public function isInteractive() {
+ public function isInteractive() : bool
+ {
return function_exists('posix_isatty') && posix_isatty($this->stream);
}
}
diff --git a/src/arguments/CLIArguments.php b/src/arguments/CLIArguments.php
--- a/src/arguments/CLIArguments.php
+++ b/src/arguments/CLIArguments.php
@@ -28,20 +28,41 @@
class CLIArguments
{
-
+ /**
+ *
+ * @var string
+ */
private $script;
+ /**
+ *
+ * @var Collection<string>
+ */
private $arguments;
+ /**
+ *
+ * @var CLIParameters
+ */
private $parameters;
- public function __construct($script, $arguments, $parameters) {
+ /**
+ *
+ * @param string $script
+ * @param string[] $arguments
+ * @param string[] $parameters
+ */
+ public function __construct(string $script, array $arguments, array $parameters) {
$this->script = $script;
$this->arguments = new Collection($arguments);
$this->parameters = new CLIParameters($parameters);
}
- public function script() {
+ /**
+ *
+ */
+ public function script() : string
+ {
return $this->script;
}
@@ -49,11 +70,13 @@
*
* @return Collection
*/
- public function arguments() {
+ public function arguments() : Collection
+ {
return $this->arguments;
}
- public function parameters() {
+ public function parameters() : CLIParameters
+ {
return $this->parameters;
}
}
diff --git a/src/arguments/CLIParameters.php b/src/arguments/CLIParameters.php
--- a/src/arguments/CLIParameters.php
+++ b/src/arguments/CLIParameters.php
@@ -29,13 +29,23 @@
class CLIParameters
{
+ /**
+ *
+ * @var array<string,string>
+ */
private $params;
- public function __construct($params) {
+ /**
+ *
+ * @param array<string,string> $params
+ */
+ public function __construct(array $params)
+ {
$this->params = $params;
}
- public function redirect($from, $to) {
+ public function redirect(string $from, string $to) : void
+ {
if (isset($this->params[$from]) && !isset($this->params[$to])) {
$this->params[$to] = $this->params[$from];
@@ -46,12 +56,15 @@
}
}
- public function get($name) {
- return isset($this->params[$name])? $this->params[$name] : false;
+ public function get(string $name) :? string
+ {
+ return isset($this->params[$name])? $this->params[$name] : null;
}
- public function defined($name) {
+
+ public function defined(string $name) : bool
+ {
return array_key_exists($name, $this->params);
}
}
\ No newline at end of file
diff --git a/src/arguments/Parser.php b/src/arguments/Parser.php
--- a/src/arguments/Parser.php
+++ b/src/arguments/Parser.php
@@ -1,5 +1,6 @@
<?php namespace spitfire\cli\arguments;
+use spitfire\cli\arguments\extractor\ExtractorInterface;
use spitfire\cli\arguments\extractor\LongParamExtractor;
use spitfire\cli\arguments\extractor\ShortParamExtractor;
use spitfire\cli\arguments\extractor\STDINExtractor;
@@ -32,6 +33,10 @@
class Parser
{
+ /**
+ *
+ * @var ExtractorInterface[]
+ */
private $extractors;
public function __construct() {
diff --git a/src/arguments/extractor/ExtractorInterface.php b/src/arguments/extractor/ExtractorInterface.php
--- a/src/arguments/extractor/ExtractorInterface.php
+++ b/src/arguments/extractor/ExtractorInterface.php
@@ -34,6 +34,6 @@
* to indicate this extractor could not process the
* data and a string if the argument is a argument.
*/
- public function extract($argument);
+ public function extract(string $argument);
}
diff --git a/src/arguments/extractor/ShortParamExtractor.php b/src/arguments/extractor/ShortParamExtractor.php
--- a/src/arguments/extractor/ShortParamExtractor.php
+++ b/src/arguments/extractor/ShortParamExtractor.php
@@ -27,25 +27,27 @@
class ShortParamExtractor implements ExtractorInterface
{
- public function extract($argument) {
- if ($argument[0] == '-' ) {
- $pieces = explode('=', $argument, 2);
- $name = str_split(substr(array_shift($pieces), 1));
- $value = array_shift($pieces);
-
- if ($value) {
- $first = array_pop($name);
- $parameters[$first] = $value;
- }
+ public function extract($argument)
+ {
+ if ($argument[0] != '-' ) {
+ return false;
+ }
+
+ $pieces = explode('=', $argument, 2);
+ $name = str_split(substr(array_shift($pieces), 1));
+ $value = array_shift($pieces);
+ $parameters = [];
+
+ if ($value) {
+ $first = array_pop($name);
+ $parameters[$first] = $value;
+ }
- foreach ($name as $flag) {
- $parameters[$flag] = isset($parameters[$flag])? $parameters[$flag] + 1 : 1;
- }
-
- return $parameters;
+ foreach ($name as $flag) {
+ $parameters[$flag] = isset($parameters[$flag])? $parameters[$flag] + 1 : 1;
}
- return false;
+ return $parameters;
}
}
diff --git a/src/arguments/extractor/StopCommandExtractor.php b/src/arguments/extractor/StopCommandExtractor.php
--- a/src/arguments/extractor/StopCommandExtractor.php
+++ b/src/arguments/extractor/StopCommandExtractor.php
@@ -27,12 +27,20 @@
class StopCommandExtractor implements ExtractorInterface
{
+ /**
+ * @var bool
+ */
private $switch = false;
- public function extract($argument) {
+ /**
+ *
+ * @return mixed[]|bool|string
+ */
+ public function extract(string $argument)
+ {
if ($argument === '--') {
$this->switch = true;
- return null;
+ return false;
}
if ($this->switch) {
diff --git a/src/progressbar/InteractiveRenderer.php b/src/progressbar/InteractiveRenderer.php
--- a/src/progressbar/InteractiveRenderer.php
+++ b/src/progressbar/InteractiveRenderer.php
@@ -54,7 +54,7 @@
*/
private $lastredraw;
- public function __construct ($message, $stream)
+ public function __construct (string $message, Stream $stream)
{
$this->message = $message;
$this->stream = $stream;
@@ -63,9 +63,10 @@
/**
* Generates output that is sent to the stream.
*
- * @param int $progress A percent value between 0 and 1
+ * @param float $progress A percent value between 0 and 1
*/
- public function render($progress) {
+ public function render(float $progress) : void
+ {
if (time() === $this->lastredraw) { return; }
@@ -76,9 +77,9 @@
$this->stream->out(sprintf('[WAIT] %s [%s]', $this->message, 'Invalid value ' . $progress));
}
else {
- $width = exec('tput cols') - strlen($this->message) - 10;
+ $width = (int)exec('tput cols') - strlen($this->message) - 10;
$drawn = (int)($progress * $width);
$this->stream->out(sprintf('[WAIT] %s [%s%s]', $this->message, str_repeat('#', $drawn), str_repeat(' ', $width - $drawn)));
}
}
}
\ No newline at end of file
diff --git a/src/progressbar/PipeRenderer.php b/src/progressbar/PipeRenderer.php
--- a/src/progressbar/PipeRenderer.php
+++ b/src/progressbar/PipeRenderer.php
@@ -39,6 +39,7 @@
{
/**
+ *
* @var Stream
*/
private $stream;
@@ -51,31 +52,51 @@
*/
private $message;
+ /**
+ *
+ * @var bool
+ */
private $renderedMessage = false;
- private $progress = 0;
- public function __construct ($message, $stream)
+ /**
+ *
+ * @var int
+ */
+ private $cached = 0;
+
+
+ public function __construct (string $message, Stream $stream)
{
$this->message = $message;
$this->stream = $stream;
}
- public function render($progress) {
+ public function render(float $progress) : void
+ {
$percent = (int)($progress * 100);
- if ($percent <= $this->progress) { return; }
- if (!$this->renderedMessage) {
- $this->stream->out(sprintf('[WAIT] %s ', $this->message));
- $this->renderedMessage = true;
+ /**
+ * Pipes are not a buffer. So they
+ */
+ if ($percent <= $this->cached)
+ {
+ return;
+ }
+
+ if (!$this->renderedMessage)
+ {
+ $this->stream->out(sprintf('[WAIT] %s ', $this->message));
+ $this->renderedMessage = true;
}
- if ($progress < 0 || $progress > 1) {
- #Do nothing. This cannot be rendered.
+ if ($progress < 0 || $progress > 1)
+ {
+ #Do nothing. This cannot be rendered.
}
else {
- $this->stream->out(str_repeat('.', $percent - $this->progress));
- $this->progress = $percent;
+ $this->stream->out(str_repeat('.', $percent - $this->cached));
+ $this->cached = $percent;
}
}
}
\ No newline at end of file

File Metadata

Mime Type
text/plain
Expires
Wed, Apr 14, 4:52 AM (3 w, 6 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
6090
Default Alt Text
D658.id2044.diff (17 KB)

Event Timeline