Skip to content

Commit

Permalink
Merge pull request #73 from andy128k/master
Browse files Browse the repository at this point in the history
Show available formats and rulesets
  • Loading branch information
manuelpichler committed Apr 23, 2013
2 parents 5b3aac4 + 86560ce commit d83d971
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
33 changes: 33 additions & 0 deletions src/main/php/PHP/PMD/RuleSetFactory.php
Expand Up @@ -158,6 +158,18 @@ public function createSingleRuleSet($ruleSetOrFileName)
return $this->parseRuleSetNode($fileName);
}

/**
* Lists available rule-set identifiers.
*
* @return array(string)
*/
public function listAvailableRuleSets()
{
return array_merge(
self::listRuleSetsInDirectory($this->location . '/rulesets/'),
self::listRuleSetsInDirectory(getcwd() . '/rulesets/'));
}

/**
* This method creates the filename for a rule-set identifier or it returns
* the input when it is already a filename.
Expand Down Expand Up @@ -190,6 +202,27 @@ private function createRuleSetFileName($ruleSetOrFileName)
throw new PHP_PMD_RuleSetNotFoundException($ruleSetOrFileName);
}

/**
* Lists available rule-set identifiers in given directory.
*
* @param string $directory The directory to scan for rule-sets.
*
* @return array(string)
*/
private static function listRuleSetsInDirectory($directory)
{
$ruleSets = array();
if (is_dir($directory)) {
foreach (scandir($directory) as $file) {
$matches = array();
if (is_file($directory . $file) && preg_match('/^(.*)\.xml$/', $file, $matches)) {
$ruleSets[] = $matches[1];
}
}
}
return $ruleSets;
}

/**
* This method parses the rule-set definition in the given file.
*
Expand Down
15 changes: 9 additions & 6 deletions src/main/php/PHP/PMD/TextUI/Command.php
Expand Up @@ -84,8 +84,10 @@ class PHP_PMD_TextUI_Command
*
* @return integer
*/
public function run(PHP_PMD_TextUI_CommandLineOptions $opts)
{
public function run(
PHP_PMD_TextUI_CommandLineOptions $opts,
PHP_PMD_RuleSetFactory $ruleSetFactory
) {
if ($opts->hasVersion()) {
fwrite(STDOUT, 'PHPMD @package_version@ by Manuel Pichler' . PHP_EOL);
return self::EXIT_SUCCESS;
Expand All @@ -102,8 +104,7 @@ public function run(PHP_PMD_TextUI_CommandLineOptions $opts)
$renderer = $opts->createRenderer();
$renderer->setWriter(new PHP_PMD_Writer_Stream($stream));

// Create a rule set factory
$ruleSetFactory = new PHP_PMD_RuleSetFactory();
// Configure a rule set factory
$ruleSetFactory->setMinimumPriority($opts->getMinimumPriority());
if ($opts->hasStrict()) {
$ruleSetFactory->setStrict();
Expand Down Expand Up @@ -145,10 +146,12 @@ public function run(PHP_PMD_TextUI_CommandLineOptions $opts)
public static function main(array $args)
{
try {
$options = new PHP_PMD_TextUI_CommandLineOptions($args);
$ruleSetFactory = new PHP_PMD_RuleSetFactory();
$options = new PHP_PMD_TextUI_CommandLineOptions($args,
$ruleSetFactory->listAvailableRuleSets());
$command = new PHP_PMD_TextUI_Command();

$exitCode = $command->run($options);
$exitCode = $command->run($options, $ruleSetFactory);
} catch (Exception $e) {
fwrite(STDERR, $e->getMessage());
fwrite(STDERR, PHP_EOL);
Expand Down
13 changes: 12 additions & 1 deletion src/main/php/PHP/PMD/TextUI/CommandLineOptions.php
Expand Up @@ -132,16 +132,25 @@ class PHP_PMD_TextUI_CommandLineOptions
*/
private $strict = false;

/**
* List of available rule-sets.
*
* @var array(string)
*/
private $availableRuleSets = array();

/**
* Constructs a new command line options instance.
*
* @param array(string) $args The cli arguments.
*/
public function __construct(array $args)
public function __construct(array $args, array $availableRuleSets = array())
{
// Remove current file name
array_shift($args);

$this->availableRuleSets = $availableRuleSets;

$arguments = array();
while (($arg = array_shift($args)) !== null) {
switch ($arg) {
Expand Down Expand Up @@ -354,6 +363,8 @@ public function usage()
'2) A report format' . PHP_EOL .
'3) A ruleset filename or a comma-separated string of ruleset' .
'filenames' . PHP_EOL . PHP_EOL .
'Available formats: xml, text, html.' . PHP_EOL .
'Available rulesets: ' . implode(', ', $this->availableRuleSets) . '.' . PHP_EOL . PHP_EOL .
'Optional arguments that may be put after the mandatory arguments:' .
PHP_EOL .
'--minimumpriority: rule priority threshold; rules with lower ' .
Expand Down

0 comments on commit d83d971

Please sign in to comment.