mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-26 08:42:40 +01:00
PHPCS Linter
Summary: Simple wrapper for PHP_CodeSniffer. You need to have PHP_CodeSniffer and it's dependencies installed. Test Plan: - Try running it with your custom lint engine Reviewers: epriestley CC: aran, Koolvin Differential Revision: https://secure.phabricator.com/D2560
This commit is contained in:
parent
fb4f3c9446
commit
1c6f66dab3
3 changed files with 162 additions and 0 deletions
|
@ -81,6 +81,7 @@ phutil_register_library_map(array(
|
|||
'ArcanistPEP8Linter' => 'lint/linter/pep8',
|
||||
'ArcanistPasteWorkflow' => 'workflow/paste',
|
||||
'ArcanistPatchWorkflow' => 'workflow/patch',
|
||||
'ArcanistPhpcsLinter' => 'lint/linter/phpcs',
|
||||
'ArcanistPhutilModuleLinter' => 'lint/linter/phutilmodule',
|
||||
'ArcanistPhutilTestCase' => 'unit/engine/phutil/testcase',
|
||||
'ArcanistPhutilTestSkippedException' => 'unit/engine/phutil/testcase/exception',
|
||||
|
@ -172,6 +173,7 @@ phutil_register_library_map(array(
|
|||
'ArcanistPEP8Linter' => 'ArcanistLinter',
|
||||
'ArcanistPasteWorkflow' => 'ArcanistBaseWorkflow',
|
||||
'ArcanistPatchWorkflow' => 'ArcanistBaseWorkflow',
|
||||
'ArcanistPhpcsLinter' => 'ArcanistLinter',
|
||||
'ArcanistPhutilModuleLinter' => 'ArcanistLinter',
|
||||
'ArcanistPyFlakesLinter' => 'ArcanistLinter',
|
||||
'ArcanistPyLintLinter' => 'ArcanistLinter',
|
||||
|
|
140
src/lint/linter/phpcs/ArcanistPhpcsLinter.php
Normal file
140
src/lint/linter/phpcs/ArcanistPhpcsLinter.php
Normal file
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2012 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Uses "PHP_CodeSniffer" to detect checkstyle errors in php code.
|
||||
* To use this linter, you must install PHP_CodeSniffer.
|
||||
* http://pear.php.net/package/PHP_CodeSniffer.
|
||||
*
|
||||
* Optional configurations in .arcconfig:
|
||||
*
|
||||
* lint.phpcs.standard
|
||||
* lint.phpcs.options
|
||||
* lint.phpcs.bin
|
||||
*
|
||||
* @group linter
|
||||
*/
|
||||
final class ArcanistPhpcsLinter extends ArcanistLinter {
|
||||
|
||||
private $reports;
|
||||
private $stdout;
|
||||
|
||||
public function getLinterName() {
|
||||
return 'PHPCS';
|
||||
}
|
||||
|
||||
public function getLintSeverityMap() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getLintNameMap() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getPhpcsOptions() {
|
||||
$working_copy = $this->getEngine()->getWorkingCopy();
|
||||
|
||||
$options = $working_copy->getConfig('lint.phpcs.options');
|
||||
|
||||
$standard = $working_copy->getConfig('lint.phpcs.standard');
|
||||
$options .= !empty($standard) ? ' --standard=' . $standard : '';
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
private function getPhpcsPath() {
|
||||
$working_copy = $this->getEngine()->getWorkingCopy();
|
||||
$bin = $working_copy->getConfig('lint.phpcs.bin');
|
||||
|
||||
if ($bin === null) {
|
||||
$bin = 'phpcs';
|
||||
}
|
||||
|
||||
return $bin;
|
||||
}
|
||||
|
||||
public function willLintPaths(array $paths) {
|
||||
$phpcs_bin = $this->getPhpcsPath();
|
||||
$phpcs_options = $this->getPhpcsOptions();
|
||||
$futures = array();
|
||||
|
||||
foreach ($paths as $path) {
|
||||
$filepath = $this->getEngine()->getFilePathOnDisk($path);
|
||||
$this->reports[$path] = new TempFile();
|
||||
$futures[$path] = new ExecFuture('%C %C --report=xml --report-file=%s %s',
|
||||
$phpcs_bin,
|
||||
$phpcs_options,
|
||||
$this->reports[$path],
|
||||
$filepath);
|
||||
}
|
||||
|
||||
foreach (Futures($futures)->limit(8) as $path => $future) {
|
||||
$this->results[$path] = $future->resolve();
|
||||
}
|
||||
}
|
||||
|
||||
protected function loadXmlException() {
|
||||
throw new ArcanistUsageException('PHPCS Linter failed to load ' .
|
||||
'reporting file. Something happened when running phpcs. ' .
|
||||
"Output:\n$this->stdout" .
|
||||
"\nTry running lint with --trace flag to get more details.");
|
||||
}
|
||||
|
||||
public function lintPath($path) {
|
||||
list($rc, $stdout) = $this->results[$path];
|
||||
|
||||
$report = Filesystem::readFile($this->reports[$path]);
|
||||
$report_dom = new DOMDocument();
|
||||
|
||||
// Unfortunately loadXML does not have normal error reporting,
|
||||
// so we need temporary to take over error handler
|
||||
set_error_handler(array($this, 'loadXmlException'));
|
||||
$this->stdout = $stdout;
|
||||
$report_dom->loadXML($report);
|
||||
restore_error_handler();
|
||||
|
||||
$files = $report_dom->getElementsByTagName('file');
|
||||
foreach ($files as $file) {
|
||||
foreach ($file->childNodes as $child) {
|
||||
if (!($child instanceof DOMElement)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data = $this->getData($path);
|
||||
$lines = explode("\n", $data);
|
||||
$line = $lines[$child->getAttribute('line') - 1];
|
||||
$text = substr($line, $child->getAttribute('column') - 1);
|
||||
$name = $this->getLinterName() . ' - ' . $child->getAttribute('source');
|
||||
$severity = $child->tagName == 'error' ?
|
||||
ArcanistLintSeverity::SEVERITY_ERROR
|
||||
: ArcanistLintSeverity::SEVERITY_WARNING;
|
||||
|
||||
$message = new ArcanistLintMessage();
|
||||
$message->setPath($path);
|
||||
$message->setLine($child->getAttribute('line'));
|
||||
$message->setChar($child->getAttribute('column'));
|
||||
$message->setCode($child->getAttribute('severity'));
|
||||
$message->setName($name);
|
||||
$message->setDescription($child->nodeValue);
|
||||
$message->setSeverity($severity);
|
||||
$message->setOriginalText($text);
|
||||
$this->addLintMessage($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
20
src/lint/linter/phpcs/__init__.php
Normal file
20
src/lint/linter/phpcs/__init__.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is automatically generated. Lint this module to rebuild it.
|
||||
* @generated
|
||||
*/
|
||||
|
||||
|
||||
|
||||
phutil_require_module('arcanist', 'exception/usage');
|
||||
phutil_require_module('arcanist', 'lint/linter/base');
|
||||
phutil_require_module('arcanist', 'lint/message');
|
||||
phutil_require_module('arcanist', 'lint/severity');
|
||||
|
||||
phutil_require_module('phutil', 'filesystem');
|
||||
phutil_require_module('phutil', 'filesystem/tempfile');
|
||||
phutil_require_module('phutil', 'future');
|
||||
phutil_require_module('phutil', 'future/exec');
|
||||
|
||||
|
||||
phutil_require_source('ArcanistPhpcsLinter.php');
|
Loading…
Reference in a new issue