Skip to content

Commit

Permalink
Merge --verbose and -v, -vv, -vvv
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Aug 19, 2023
1 parent c9e8d62 commit aff7cd9
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 31 deletions.
12 changes: 5 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,12 @@ Command line options

- The command line interface also accepts the following optional arguments:

- ``-v, -vv, -vvv`` - The output verbosity level. Will print more information
- ``--verbose, -v, -vv, -vvv`` - The output verbosity level. Will print more information
what is being processed or cached. Will be send to ``STDERR`` to not interfere
with report output.
with report output. ``text`` output will also have under each error a link
to the documentation of the rule and format the location in a way that most
IDEs will convert into a link to open the file at the line of the error
when clicked.

- ``--minimumpriority`` - The rule priority threshold; rules with lower
priority than they will not be used.
Expand Down Expand Up @@ -160,11 +163,6 @@ Command line options
- ``--baseline-file`` - the filepath to a custom baseline xml file. If absent will
default to ``phpmd.baseline.xml``

- ``--verbose`` - get a more verbose output, for instance text renderer
will add under each error a link to the documentation of the rule
and format the location in a way that most IDEs will convert into
a link that will open the file at the line of the error when clicked.

- ``--color`` - enable color in output, for instance text renderer
will show rule name in yellow and error description in red.

Expand Down
7 changes: 4 additions & 3 deletions src/main/php/PHPMD/Renderer/TextRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace PHPMD\Renderer;

use PHPMD\AbstractRenderer;
use PHPMD\Console\OutputInterface;
use PHPMD\Renderer\Option\Color;
use PHPMD\Renderer\Option\Verbose;
use PHPMD\Report;
Expand All @@ -30,7 +31,7 @@ class TextRenderer extends AbstractRenderer implements Verbose, Color
{
protected $columnSpacing = 2;

protected $verbosityLevel = 0;
protected $verbosityLevel = OutputInterface::VERBOSITY_NORMAL;

protected $colored = false;

Expand Down Expand Up @@ -63,7 +64,7 @@ public function renderReport(Report $report)
foreach ($violations as $data) {
list($violation, $location, $ruleName, $ruleSet, $locationLength, $ruleNameLength) = $data;

if ($this->verbosityLevel < 1) {
if ($this->verbosityLevel < OutputInterface::VERBOSITY_VERBOSE) {
$writer->write($location);
$writer->write(str_repeat(' ', $longestLocationLength + $this->columnSpacing - $locationLength));
}
Expand All @@ -72,7 +73,7 @@ public function renderReport(Report $report)
$writer->write(str_repeat(' ', $longestRuleNameLength + $this->columnSpacing - $ruleNameLength));
$writer->write($this->applyColor($violation->getDescription(), 'red'));

if ($this->verbosityLevel >= 1) {
if ($this->verbosityLevel >= OutputInterface::VERBOSITY_VERBOSE) {
$writer->write(PHP_EOL);
$writer->write('📁 in ' . preg_replace('/:(\d+)$/', ' on line $1', $location) . PHP_EOL);
$set = preg_replace('/rules$/', '', strtolower(str_replace(' ', '', $ruleSet)));
Expand Down
16 changes: 3 additions & 13 deletions src/main/php/PHPMD/TextUI/CommandLineOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,6 @@ class CommandLineOptions
*/
protected $cacheStrategy;

/**
* The level of verbosity as per set in CLI options.
*
* @var int
*/
protected $verbosityLevel = 0;

/**
* Either the output should be colored.
*
Expand All @@ -221,6 +214,7 @@ public function __construct(array $args, array $availableRuleSets = array())
$arguments = array();
while (($arg = array_shift($args)) !== null) {
switch ($arg) {
case '--verbose':
case '-v':
$this->verbosity = OutputInterface::VERBOSITY_VERBOSE;
break;
Expand Down Expand Up @@ -270,9 +264,6 @@ public function __construct(array $args, array $availableRuleSets = array())
case '--color':
$this->colored = true;
break;
case '--verbose':
$this->verbosityLevel = 1;
break;
case '--version':
$this->version = true;

Expand Down Expand Up @@ -564,7 +555,7 @@ public function createRenderer($reportFormat = null)
$renderer = $this->createRendererWithoutOptions($reportFormat);

if ($renderer instanceof Verbose) {
$renderer->setVerbosityLevel($this->verbosityLevel);
$renderer->setVerbosityLevel($this->verbosity);
}

if ($renderer instanceof Color) {
Expand Down Expand Up @@ -736,7 +727,7 @@ public function usage()
'Available rulesets: ' . implode(', ', $this->availableRuleSets) . '.' . \PHP_EOL . \PHP_EOL .
'Optional arguments that may be put after the mandatory arguments:' .
\PHP_EOL .
'-v, -vv, -vvv: Show debug information.' . \PHP_EOL .
'--verbose, -v, -vv, -vvv: Show debug information.' . \PHP_EOL .
'--minimumpriority: rule priority threshold; rules with lower ' .
'priority than this will not be used' . \PHP_EOL .
'--reportfile: send report output to a file; default to STDOUT' .
Expand All @@ -763,7 +754,6 @@ public function usage()
'to the first ruleset file location' . \PHP_EOL .
'--update-baseline: will remove any non-existing violations from the phpmd.baseline.xml' . \PHP_EOL .
'--baseline-file: a custom location of the baseline file' . \PHP_EOL .
'--verbose: will return a more verbose output' . \PHP_EOL .
'--color: enable color in output' . \PHP_EOL;
}

Expand Down
12 changes: 5 additions & 7 deletions src/site/rst/documentation/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ Command line options

- The command line interface also accepts the following optional arguments:

- ``-v, -vv, -vvv`` - The output verbosity level. Will print more information
- ``--verbose, -v, -vv, -vvv`` - The output verbosity level. Will print more information
what is being processed or cached. Will be send to ``STDERR`` to not interfere
with report output.
with report output. ``text`` output will also have under each error a link
to the documentation of the rule and format the location in a way that most
IDEs will convert into a link to open the file at the line of the error
when clicked.

- ``--minimumpriority`` ``--min-priority`` ``--minimum-priority`` - The rule priority threshold; rules with lower
priority than they will not be used.
Expand Down Expand Up @@ -88,11 +91,6 @@ Command line options
- ``--baseline-file`` - the filepath to a custom baseline xml file. If absent will
default to ``phpmd.baseline.xml``

- ``--verbose`` - get a more verbose output, for instance text renderer
will add under each error a link to the documentation of the rule
and format the location in a way that most IDEs will convert into
a link that will open the file at the line of the error when clicked.

- ``--color`` - enable color in output, for instance text renderer
will show rule name in yellow and error description in red.

Expand Down
3 changes: 2 additions & 1 deletion src/test/php/PHPMD/Renderer/TextRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use ArrayIterator;
use PHPMD\AbstractTest;
use PHPMD\Console\OutputInterface;
use PHPMD\ProcessingError;
use PHPMD\Stubs\RuleStub;
use PHPMD\Stubs\WriterStub;
Expand Down Expand Up @@ -85,7 +86,7 @@ public function testRendererSupportVerbose()

$renderer = new TextRenderer();
$renderer->setWriter($writer);
$renderer->setVerbosityLevel(1);
$renderer->setVerbosityLevel(OutputInterface::VERBOSITY_VERBOSE);

$violations = array(
$this->getRuleViolationMock('/bar.php', 1, 42, $rule, $rule->getDescription()),
Expand Down
37 changes: 37 additions & 0 deletions src/test/php/PHPMD/TextUI/CommandLineOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPMD\Baseline\BaselineMode;
use PHPMD\Console\OutputInterface;
use PHPMD\Rule;
use ReflectionProperty;

/**
* Test case for the {@link \PHPMD\TextUI\CommandLineOptions} class.
Expand Down Expand Up @@ -61,6 +62,42 @@ public function testAssignsInputArgumentToInputProperty()
self::assertEquals(__FILE__, $opts->getInputPath());
}

/**
* @return void
* @since 2.14.0
*/
public function testVerbose()
{
$args = array('foo.php', __FILE__, 'text', 'design', '-vvv');
$opts = new CommandLineOptions($args);
$renbderer = $opts->createRenderer();

$verbosityExtractor = new ReflectionProperty('PHPMD\\Renderer\\TextRenderer', 'verbosityLevel');
$verbosityExtractor->setAccessible(true);

$verbosityLevel = $verbosityExtractor->getValue($renbderer);

self::assertSame(OutputInterface::VERBOSITY_VERY_VERBOSE, $verbosityLevel);
}

/**
* @return void
* @since 2.14.0
*/
public function testColored()
{
$args = array('foo.php', __FILE__, 'text', 'design', '--color');
$opts = new CommandLineOptions($args);
$renbderer = $opts->createRenderer();

$coloredExtractor = new ReflectionProperty('PHPMD\\Renderer\\TextRenderer', 'colored');
$coloredExtractor->setAccessible(true);

$colored = $coloredExtractor->getValue($renbderer);

self::assertTrue($colored);
}

/**
* testAssignsFormatArgumentToReportFormatProperty
*
Expand Down

0 comments on commit aff7cd9

Please sign in to comment.