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

Add trace of the error when debug verbosity #1023

Merged
merged 16 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Unit Tests with coverage
name: Coverage

on:
push:
Expand All @@ -17,7 +17,7 @@ jobs:

strategy:
matrix:
php: [7.4]
php: [8.2]
dependency-version: [prefer-stable]

name: P${{ matrix.php }}
Expand All @@ -30,23 +30,28 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
tools: composer:v2
coverage: xdebug

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php }}-${{ matrix.dependency-version }}-${{ hashFiles('**/composer.json') }}
key: coverage-${{ runner.os }}-php-${{ matrix.php }}-${{ matrix.dependency-version }}-${{ hashFiles('**/composer.json') }}
restore-keys: |
${{ runner.os }}-php-${{ matrix.php }}-${{ matrix.dependency-version }}
coverage-${{ runner.os }}-php-${{ matrix.php }}-${{ matrix.dependency-version }}

- name: Install dependencies
run: composer install --prefer-dist --no-progress
run: |
composer require phpunit/phpunit:^5.7.27 --no-update --no-interaction --dev
composer update --prefer-dist --no-progress --${{ matrix.dependency-version }} --ignore-platform-req=php

- name: Fix PHP compatibility
run: php src/test/php/fix-php-compatibility.php

- name: Execute Unit Tests
run: phpdbg -qrr vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml --coverage-html=coverage_html
run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml --coverage-html=coverage_html

- name: Archive code coverage results
uses: codecov/codecov-action@v3
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/phpunit.yml → .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Unit Tests
name: Tests

on:
push:
Expand All @@ -17,7 +17,7 @@ jobs:

strategy:
matrix:
php: [5.3, 5.4, 5.5, 5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2]
php: [5.3, 5.4, 5.5, 5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2, 8.3]
dependency-version: [prefer-lowest, prefer-stable]
exclude:
- dependency-version: prefer-lowest
Expand All @@ -27,7 +27,7 @@ jobs:
- dependency-version: prefer-lowest
php: 7.4

name: P${{ matrix.php }} - ${{ matrix.dependency-version }}
name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }}

steps:
- name: Checkout code
Expand Down
4 changes: 2 additions & 2 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0"?>
<ruleset name="PHPMD">
<description>The coding standard for PHPMD.</description>
<file>./src/main/php</file>
<file>./src/test/php</file>
<file>./src/main/php/PHPMD</file>
<file>./src/test/php/PHPMD</file>
<!-- Include Slevomat standard -->

<!-- <config name="installed_paths" value="../../slevomat/coding-standard,../../../slevomat/coding-standard,../vendor/slevomat/coding-standard"/>-->
Expand Down
29 changes: 24 additions & 5 deletions src/main/php/PHPMD/TextUI/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace PHPMD\TextUI;

use Exception;
use PHPMD\Baseline\BaselineFileFinder;
use PHPMD\Baseline\BaselineMode;
use PHPMD\Baseline\BaselineSetFactory;
Expand All @@ -25,6 +26,7 @@
use PHPMD\Cache\ResultCacheKeyFactory;
use PHPMD\Cache\ResultCacheStateFactory;
use PHPMD\Console\Output;
use PHPMD\Console\OutputInterface;
use PHPMD\Console\StreamOutput;
use PHPMD\PHPMD;
use PHPMD\Renderer\RendererFactory;
Expand Down Expand Up @@ -80,7 +82,7 @@ public function run(CommandLineOptions $opts, RuleSetFactory $ruleSetFactory)
}

// Create a report stream
$stream = $opts->getReportFile() ? $opts->getReportFile() : STDOUT;
$stream = $opts->getReportFile() ?: STDOUT;

// Create renderer and configure output
$renderer = $opts->createRenderer();
Expand Down Expand Up @@ -203,15 +205,32 @@ private function getVersion()
*/
public static function main(array $args)
{
$options = null;

try {
$ruleSetFactory = new RuleSetFactory();
$options = new CommandLineOptions($args, $ruleSetFactory->listAvailableRuleSets());
$output = new StreamOutput(STDERR, $options->getVerbosity());
$command = new Command($output);
$errorFile = $options->getErrorFile();
$errorStream = new StreamWriter($errorFile ?: STDERR);
$output = new StreamOutput($errorStream->getStream(), $options->getVerbosity());
$command = new self($output);

foreach ($options->getDeprecations() as $deprecation) {
$output->write($deprecation . PHP_EOL . PHP_EOL);
}

$exitCode = $command->run($options, $ruleSetFactory);
} catch (\Exception $e) {
fwrite(STDERR, $e->getMessage() . PHP_EOL);
unset($errorStream);
} catch (Exception $e) {
$file = $options ? $options->getErrorFile() : null;
$writer = new StreamWriter($file ?: STDERR);
$writer->write($e->getMessage() . PHP_EOL);

if ($options && $options->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
$writer->write($e->getFile() . ':' . $e->getLine() . PHP_EOL);
$writer->write($e->getTraceAsString() . PHP_EOL);
}

$exitCode = self::EXIT_EXCEPTION;
}

Expand Down
63 changes: 50 additions & 13 deletions src/main/php/PHPMD/TextUI/CommandLineOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,27 @@ class CommandLineOptions
*/
protected $reportFile;

/**
* An optional filename to collect errors.
*
* @var string
*/
protected $errorFile;

/**
* Additional report files.
*
* @var array
*/
protected $reportFiles = array();

/**
* List of deprecations.
*
* @var array
*/
protected $deprecations = array();

/**
* A ruleset filename or a comma-separated string of ruleset filenames.
*
Expand Down Expand Up @@ -238,6 +252,10 @@ public function __construct(array $args, array $availableRuleSets = array())
case '--reportfile':
$this->reportFile = array_shift($args);
break;
case '--error-file':
case '--errorfile':
$this->errorFile = array_shift($args);
break;
case '--input-file':
case '--inputfile':
array_unshift($arguments, $this->readInputFile(array_shift($args)));
Expand Down Expand Up @@ -355,6 +373,27 @@ public function getReportFile()
return $this->reportFile;
}

/**
* Returns the output filename for the errors or <b>null</b> when
* the report should be displayed in STDERR.
*
* @return string
*/
public function getErrorFile()
{
return $this->errorFile;
}

/**
* Return the list of deprecations raised when parsing options.
*
* @return list<string>
*/
public function getDeprecations()
{
return $this->deprecations;
}

/**
* Returns a hash with report files specified for different renderers. The
* key represents the report format and the value the report file location.
Expand Down Expand Up @@ -655,7 +694,7 @@ protected function createJsonRenderer()
}

/**
* @return \PHPMD\Renderer\JSONRenderer
* @return \PHPMD\Renderer\CheckStyleRenderer
*/
protected function createCheckStyleRenderer()
{
Expand Down Expand Up @@ -715,6 +754,7 @@ protected function createCustomRenderer()
public function usage()
{
$availableRenderers = $this->getListOfAvailableRenderers();
$noRenderers = ($availableRenderers === null);

return 'Mandatory arguments:' . \PHP_EOL .
'1) A php source code filename or directory. Can be a comma-' .
Expand All @@ -723,14 +763,17 @@ public function usage()
'3) A ruleset filename or a comma-separated string of ruleset' .
'filenames' . \PHP_EOL . \PHP_EOL .
'Example: phpmd /path/to/source format ruleset' . \PHP_EOL . \PHP_EOL .
'Available formats: ' . $availableRenderers . '.' . \PHP_EOL .
($noRenderers ? 'No available formats' : 'Available formats: ' . $availableRenderers) . '.' . \PHP_EOL .
'Available rulesets: ' . implode(', ', $this->availableRuleSets) . '.' . \PHP_EOL . \PHP_EOL .
'Optional arguments that may be put after the mandatory arguments:' .
\PHP_EOL .
'--verbose, -v, -vv, -vvv: Show debug information.' . \PHP_EOL .
'--minimumpriority: rule priority threshold; rules with lower ' .
'--minimum-priority: 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' .
'--report-file: send report output to a file; default to STDOUT' .
\PHP_EOL .
'--error-file: send errors (other than reported violations) ' .
'output to a file; default to STDERR' .
\PHP_EOL .
'--suffixes: comma-separated string of valid source code ' .
'filename extensions, e.g. php,phtml' . \PHP_EOL .
Expand Down Expand Up @@ -760,7 +803,7 @@ public function usage()
/**
* Get a list of available renderers
*
* @return string The list of renderers found.
* @return string|null The list of renderers found separated by comma, or null if none.
*/
protected function getListOfAvailableRenderers()
{
Expand All @@ -776,11 +819,7 @@ protected function getListOfAvailableRenderers()

sort($renderers);

if (count($renderers) > 1) {
return implode(', ', $renderers);
}

return array_pop($renderers);
return implode(', ', $renderers) ?: null;
}

/**
Expand All @@ -792,13 +831,11 @@ protected function getListOfAvailableRenderers()
*/
protected function logDeprecated($deprecatedName, $newName)
{
$message = sprintf(
$this->deprecations[] = sprintf(
'The --%s option is deprecated, please use --%s instead.',
$deprecatedName,
$newName
);

fwrite(STDERR, $message . PHP_EOL . PHP_EOL);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/PHPMD/Writer/StreamWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct($streamResourceOrUri)
*/
public function __destruct()
{
if ($this->stream !== STDOUT && is_resource($this->stream) === true) {
if ($this->stream !== STDOUT && $this->stream !== STDERR && is_resource($this->stream) === true) {
@fclose($this->stream);
}
$this->stream = null;
Expand Down
2 changes: 2 additions & 0 deletions src/site/rst/documentation/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Command line options
- ``--reportfile`` ``--report-file`` - Sends the report output to the specified file,
instead of the default output target ``STDOUT``.

- ``--error-file`` - send errors (other than reported violations) output to a file; default to STDERR
kylekatarnls marked this conversation as resolved.
Show resolved Hide resolved

- ``--suffixes`` - Comma-separated string of valid source code filename
extensions, e.g. php, phtml.

Expand Down