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

Added the option to the command line to set the number of extra lines in code snippets #1029

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ Command line options

- ``--color`` - enable color in output, for instance text renderer
will show rule name in yellow and error description in red.
- ``--extra-line-in-excerpt`` - specify how many extra lines are added to a code snippet in html format

An example command line: ::

Expand Down
16 changes: 15 additions & 1 deletion src/main/php/PHPMD/Renderer/HTMLRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ class HTMLRenderer extends AbstractRenderer

protected static $compiledHighlightRegex = null;

/**
* Specify how many extra lines are added to a code snippet
* By default 2
* @var int
*/
protected $extraLineInExcerpt = 2;

public function __construct($extraLineInExcerpt = null)
{
if ($extraLineInExcerpt && is_int($extraLineInExcerpt)) {
$this->extraLineInExcerpt = $extraLineInExcerpt;
}
}

/**
* This method will be called on all renderers before the engine starts the
* real report processing.
Expand Down Expand Up @@ -362,7 +376,7 @@ class='info-lnk blck'
$excerpt = self::getLineExcerpt(
$violation->getFileName(),
$violation->getBeginLine(),
2
$this->extraLineInExcerpt
);

foreach ($excerpt as $line => $code) {
Expand Down
24 changes: 22 additions & 2 deletions src/main/php/PHPMD/TextUI/CommandLineOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ class CommandLineOptions
*/
protected $colored = false;

/**
* Specify how many extra lines are added to a code snippet
* @var int|null
*/
protected $extraLineInExcerpt;

/**
* Constructs a new command line options instance.
*
Expand Down Expand Up @@ -327,6 +333,9 @@ public function __construct(array $args, array $availableRuleSets = array())
preg_match('(^\-\-reportfile\-(checkstyle|github|gitlab|html|json|sarif|text|xml)$)', $arg, $match);
$this->reportFiles[$match[1]] = array_shift($args);
break;
case '--extra-line-in-excerpt':
$this->extraLineInExcerpt = (int)array_shift($args);
break;
default:
$arguments[] = $arg;
break;
Expand Down Expand Up @@ -577,6 +586,15 @@ public function ignoreViolationsOnExit()
return $this->ignoreViolationsOnExit;
}

/**
* Specify how many extra lines are added to a code snippet
*
* @return int|null
*/
public function extraLineInExcerpt()
{
return $this->extraLineInExcerpt;
}
/**
* Creates a report renderer instance based on the user's command line
* argument.
Expand Down Expand Up @@ -686,7 +704,7 @@ protected function createGitHubRenderer()
*/
protected function createHtmlRenderer()
{
return new HTMLRenderer();
return new HTMLRenderer($this->extraLineInExcerpt);
}

/**
Expand Down Expand Up @@ -801,7 +819,9 @@ 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 .
'--color: enable color in output' . \PHP_EOL;
'--color: enable color in output' . \PHP_EOL .
'--extra-line-in-excerpt: Specify how many extra lines are added ' .
'to a code snippet in html format' . \PHP_EOL;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/php/PHPMD/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ protected function getRuleViolationMock(
}

/**
* Creates a mocked rul violation instance.
* Creates a mocked rule violation instance.
*
* @param string $file
* @param string $message
Expand Down
3 changes: 2 additions & 1 deletion src/test/php/PHPMD/Renderer/HTMLRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public function testRendererCreatesExpectedHtmlTableRow()
->method('getRuleViolations')
->will($this->returnValue(new \ArrayIterator($violations)));

$renderer = new HTMLRenderer();
$extraLineInExcerpt = 2;
$renderer = new HTMLRenderer($extraLineInExcerpt);
$renderer->setWriter($writer);

$renderer->start();
Expand Down
10 changes: 10 additions & 0 deletions src/test/php/PHPMD/TextUI/CommandLineOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,16 @@ public function testGetReportFiles(array $options, array $expected)
$this->assertEquals($expected, $opts->getReportFiles());
}

/**
* @return void
*/
public function testCliOptionExtraLineInExcerptShouldBeWithNumber()
{
$args = array(__FILE__, __FILE__, 'text', 'codesize', '--extra-line-in-excerpt', '5');
$opts = new CommandLineOptions($args);
static::assertSame(5, $opts->extraLineInExcerpt());
}

public function dataProviderGetReportFiles()
{
return array(
Expand Down