-
Notifications
You must be signed in to change notification settings - Fork 11
add parallel phpmd processing and polish #2
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
Changes from all commits
dfbafe8
b7c97e6
ba7a4b3
d82a32d
3bef1a4
66a672f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,36 @@ | ||
| # Code Climate PHP Mess Detector (PHPMD) Engine | ||
|
|
||
| `codeclimate-phpmd` is a Code Climate Engine that wraps the PHP Mess | ||
| Detector (PHPMD) static analysis tool. | ||
| `codeclimate-phpmd` is a Code Climate Engine that wraps the PHP Mess Detector (PHPMD) static analysis tool. | ||
|
|
||
| ### Installation | ||
|
|
||
| 1. If you haven't already, [install the Code Climate CLI](https://github.com/codeclimate/codeclimate). | ||
| 2. Run `codeclimate engines:enable phpmd`. This command both installs the engine and enables it in your `.codeclimate.yml` file. | ||
| 3. You're ready to analyze! Browse into your project's folder and run `codeclimate analyze`. | ||
|
|
||
| ###Config Options | ||
|
|
||
| Format the values for these config options per the [PHPMD documentation](http://phpmd.org/documentation/index.html). | ||
|
|
||
| * file_extensions - This is where you can configure the file extensions for the files that you want PHPMD to analyze. | ||
| * rulesets - This is the list of rulesets that you want PHPMD to use while analyzing your files. | ||
|
|
||
| ###Sample Config | ||
|
|
||
| exclude_paths: | ||
| - "/examples/**/*" | ||
| engines: | ||
| phpmd: | ||
| enabled: true | ||
| config: | ||
| file_extensions: "php" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably set up the file_extensions:
- php
- php5
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I believe I just copied this from the codeclimate-phpcodesniffer repo. Maybe we should double check and then address it separately?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, separate sounds good! |
||
| rulesets: "unusedcode" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, my gut is we should allow either a single ruleset like this ( rulesets:
- unusedcode
- design
- namingThe engine can implode the array.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I believe I just copied this from the codeclimate-phpcodesniffer repo. Maybe we should double check and then address it separately? |
||
| ratings: | ||
| paths: | ||
| - "**.php" | ||
|
|
||
| ### Need help? | ||
|
|
||
| For help with PHPMD, [check out their documentation](http://phpmd.org/documentation/index.html). | ||
|
|
||
| If you're running into a Code Climate issue, first look over this project's [GitHub Issues](https://github.com/phpmd/phpmd/issues), as your question may have already been covered. If not, [go ahead and open a support ticket with us](https://codeclimate.com/help). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| namespace CodeClimate\PHPMD; | ||
|
|
||
| use PHPMD\PHPMD; | ||
| use PHPMD\RuleSetFactory; | ||
| use PHPMD\Writer\StreamWriter; | ||
| use PHPMD\Renderer\JSONRenderer; | ||
|
|
||
| class Runner | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice extraction here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| { | ||
| private $config; | ||
| private $server; | ||
|
|
||
| public function __construct($config, $server) | ||
| { | ||
| $this->config = $config; | ||
| $this->server = $server; | ||
| } | ||
|
|
||
| public function queueDirectory($dir, $prefix = '') | ||
| { | ||
| $dir = rtrim($dir, '\\/'); | ||
|
|
||
| foreach (scandir($dir) as $f) { | ||
| if (in_array("$prefix$f", $this->config["exclude_paths"])) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($f !== '.' and $f !== '..') { | ||
| if (is_dir("$dir/$f")) { | ||
| $this->queueDirectory("$dir/$f", "$prefix$f/"); | ||
| continue; | ||
| } | ||
|
|
||
| $this->server->addwork(array("/code/$prefix$f")); | ||
| } | ||
| } | ||
|
|
||
| $this->server->process_work(false); | ||
| } | ||
|
|
||
| public function run($files) | ||
| { | ||
| $resultFile = tempnam(sys_get_temp_dir(), 'phpmd'); | ||
|
|
||
| $renderer = new JSONRenderer(); | ||
| $renderer->setWriter(new StreamWriter($resultFile)); | ||
|
|
||
| $ruleSetFactory = new RuleSetFactory(); | ||
|
|
||
| $phpmd = new PHPMD(); | ||
|
|
||
| if (isset($this->config['config']['file_extensions'])) { | ||
| $phpmd->setFileExtensions(explode(',', $this->config['config']['file_extensions'])); | ||
| } | ||
|
|
||
| $rulesets = "cleancode,codesize,controversial,design,naming,unusedcode"; | ||
|
|
||
| if (isset($this->config['config']['rulesets'])) { | ||
| $rulesets = $this->config['config']['rulesets']; | ||
| } | ||
|
|
||
| $phpmd->processFiles( | ||
| implode(",", $files), | ||
| $rulesets, | ||
| array($renderer), | ||
| $ruleSetFactory | ||
| ); | ||
|
|
||
| return $resultFile; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| { | ||
| "require": { | ||
| "phpmd/phpmd": "2.2.3" | ||
| "phpmd/phpmd": "2.2.3", | ||
| "barracudanetworks/forkdaemon-php": "1.0.*" | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,70 +1,40 @@ | ||
| <?php | ||
|
|
||
| declare(ticks=1); | ||
|
|
||
| namespace PHPMD\TextUI; | ||
|
|
||
| use CodeClimate\PHPMD\Runner; | ||
|
|
||
| error_reporting(E_ERROR | E_PARSE | E_NOTICE); | ||
| date_default_timezone_set('UTC'); | ||
| ini_set('memory_limit', -1); | ||
|
|
||
| require_once __DIR__.'/vendor/autoload.php'; | ||
| require_once "JSONRenderer.php"; | ||
| require_once "Runner.php"; | ||
|
|
||
| use PHPMD\PHPMD; | ||
| use PHPMD\RuleSetFactory; | ||
| use PHPMD\Writer\StreamWriter; | ||
| use PHPMD\Renderer\JSONRenderer; | ||
|
|
||
| // obtain the config | ||
| $config = json_decode(file_get_contents('/config.json'), true); | ||
|
|
||
| $renderer = new JSONRenderer(); | ||
| $renderer->setWriter(new StreamWriter(STDOUT)); | ||
|
|
||
| $ruleSetFactory = new RuleSetFactory(); | ||
|
|
||
| $all_files = scandir_recursive("/code"); | ||
|
|
||
| if ($config["exclude_paths"]) { | ||
| $files = array(); | ||
|
|
||
| foreach ($all_files as $file) { | ||
| if (!in_array($file, $config["exclude_paths"])) { | ||
| $files[] = "/code/".$file; | ||
| } | ||
| } | ||
| } else { | ||
| foreach ($all_files as $file) { | ||
| if (!in_array($file, $ignorePatterns)) { | ||
| $files[] = "/code/".$file; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $phpmd = new PHPMD(); | ||
|
|
||
| // if ($extensions !== null) { | ||
| // $phpmd->setFileExtensions(explode(',', $extensions)); | ||
| // } | ||
|
|
||
| $phpmd->processFiles( | ||
| implode(",", $files), | ||
| "cleancode,codesize,controversial,design,naming,unusedcode", | ||
| array($renderer), | ||
| $ruleSetFactory | ||
| ); | ||
| // setup forking daemon | ||
| $server = new \fork_daemon(); | ||
| $server->max_children_set(3); | ||
| $server->max_work_per_child_set(50); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please help me understand what these two values do? (20 and 50)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, |
||
| $server->store_result_set(true); | ||
| $runner = new Runner($config, $server); | ||
| $server->register_child_run(array($runner, "run")); | ||
|
|
||
| function scandir_recursive($dir, $prefix = '') { | ||
| $dir = rtrim($dir, '\\/'); | ||
| $result = array(); | ||
| $runner->queueDirectory("/code"); | ||
|
|
||
| foreach (scandir($dir) as $f) { | ||
| if ($f !== '.' and $f !== '..') { | ||
| if (is_dir("$dir/$f")) { | ||
| $result = array_merge($result, scandir_recursive("$dir/$f", "$prefix$f/")); | ||
| } else { | ||
| $result[] = $prefix.$f; | ||
| } | ||
| } | ||
| } | ||
| $server->process_work(true); | ||
|
|
||
| return $result; | ||
| foreach ($server->get_all_results() as $result_file) { | ||
| echo file_get_contents($result_file); | ||
| unlink($result_file); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where do result files get stored? (We'll need to ensure it's within
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just saw the reference to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You got it. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some references to PHP_CodeSniffer here that probably shouldn't be here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good old copy and paste, good catch, will fix.