Skip to content

Engine accepts paths to custom ruleset #6

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

Merged
merged 3 commits into from
Dec 7, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class Runner
{
const RULESETS = 'cleancode,codesize,controversial,design,naming,unusedcode';

private $config;
private $server;

Expand All @@ -20,7 +22,7 @@ public function __construct($config, $server)

public function queueDirectory($dir, $prefix = '')
{
if(isset($this->config['include_paths'])) {
if (isset($this->config['include_paths'])) {
$this->queueWithIncludePaths();
} else {
$this->queuePaths($dir, $prefix, $this->config['exclude_paths']);
Expand All @@ -29,21 +31,21 @@ public function queueDirectory($dir, $prefix = '')
$this->server->process_work(false);
}

public function queueWithIncludePaths() {
public function queueWithIncludePaths()
{
foreach ($this->config['include_paths'] as $f) {
if ($f !== '.' and $f !== '..') {

if (is_dir("/code$f")) {
$this->queuePaths("/code$f", "$f/");
continue;
}

$this->server->addwork(array("/code/$f"));
}
}
}

public function queuePaths($dir, $prefix = '', $exclusions = []) {
public function queuePaths($dir, $prefix = '', $exclusions = [])
{
$dir = rtrim($dir, '\\/');

foreach (scandir($dir) as $f) {
Expand All @@ -63,6 +65,20 @@ public function queuePaths($dir, $prefix = '', $exclusions = []) {
}
}

public function prefixCodeDirectory($configRulesets)
{
$officialPhpRulesets = explode(',', Runner::RULESETS);
$configRulesets = explode(',', $configRulesets);

foreach ($configRulesets as &$r) {
if (!in_array($r, $officialPhpRulesets) and $r[0] != "/") {
$r = "/code/$r";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be worth it to detect the case where someone enters an absolute path (ie. don't prepend /code if the string starts with /)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
}

return implode(',', $configRulesets);
}

public function run($files)
{
$resultFile = tempnam(sys_get_temp_dir(), 'phpmd');
Expand All @@ -78,10 +94,12 @@ public function run($files)
$phpmd->setFileExtensions(explode(',', $this->config['config']['file_extensions']));
}

$rulesets = "cleancode,codesize,controversial,design,naming,unusedcode";
$rulesets = Runner::RULESETS;

if (isset($this->config['config']['rulesets'])) {
$rulesets = $this->config['config']['rulesets'];
$rulesets = $this->prefixCodeDirectory(
$this->config['config']['rulesets']
);
}

$phpmd->processFiles(
Expand Down