Skip to content

Commit

Permalink
Add support for lintable paths and other custom params
Browse files Browse the repository at this point in the history
  • Loading branch information
Augustas Berneckas committed Jul 9, 2018
1 parent 437abf8 commit 61fc0aa
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
9 changes: 9 additions & 0 deletions composer/ArcConfigParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ private static function createOrUpdateArcLint($parsedConfig)
if (!isset($arcLint['linters']['php-cs-fixer']['bin'])) {
$arcLint['linters']['php-cs-fixer']['bin'] = $parsedConfig['lint.php_cs_fixer.php_cs_binary'];
}
if (!isset($arcLint['linters']['php-cs-fixer']['fix_paths'])) {
$arcLint['linters']['php-cs-fixer']['fix_paths'] = $parsedConfig['lint.php_cs_fixer.fix_paths'];
}
if (!isset($arcLint['linters']['php-cs-fixer']['php_cs_file'])) {
$arcLint['linters']['php-cs-fixer']['php_cs_file'] = $parsedConfig['lint.php_cs_fixer.php_cs_file'];
}
if (!isset($arcLint['linters']['php-cs-fixer']['unified_diff_format'])) {
$arcLint['linters']['php-cs-fixer']['unified_diff_format'] = $parsedConfig['lint.php_cs_fixer.unified_diff_format'];
}

file_put_contents(self::LINT_FILE, stripslashes(json_encode($arcLint, JSON_PRETTY_PRINT)));
}
Expand Down
77 changes: 77 additions & 0 deletions src/Lint/Linter/PhpCsFixerLinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

class PhpCsFixerLinter extends \ArcanistExternalLinter
{
/**
* @var array
*/
private $folderExclusions = [
'Tests/',
'Test/',
];

/**
* @var array
*/
Expand Down Expand Up @@ -98,6 +106,44 @@ public function getDefaultFlags()
);
}

public function getLinterConfigurationOptions()
{
$options = [
'fix_paths' => [
'type' => 'optional string | list<string>',
'help' => pht('Paths that needs to be linted'),
],
'php_cs_file' => [
'type' => 'optional string',
'help' => pht('Path to config file'),
],
'unified_diff_format' => [
'type' => 'optional bool',
'help' => pht('Unified diff format'),
],
];

return $options + parent::getLinterConfigurationOptions();
}

public function setLinterConfigurationValue($key, $value)
{
switch ($key) {
case 'fix_paths':
$this->setPaths($this->resolveLintableFiles($value));
$this->getEngine()->setPaths($this->getPaths());
return;
case 'php_cs_file':
$this->configuration->setPhpCsFile($value);
return;
case 'unified_diff_format':
$this->configuration->setUnifiedDiffFormat($value);
return;
}

return parent::setLinterConfigurationValue($key, $value);
}

public function getDefaultBinary()
{
return $this->configuration->getBinaryFile();
Expand Down Expand Up @@ -140,4 +186,35 @@ protected function getPathArgumentForLinterFuture($path)

return str_replace($root . '/', '', $path);
}

private function resolveLintableFiles(array $lintablePaths)
{
$paths = $this->getEngine()->getPaths();

$properPaths = [];

foreach ($paths as $key => $path) {
if (!file_exists($this->getEngine()->getFilePathOnDisk($path))) {
unset($paths[$key]);
}

if (
preg_match('#' . implode('|', $this->pregQuotePaths($lintablePaths)) . '#i', $path)
&& preg_match('#\.(php)$#', $path)
&& preg_match('#' . implode('|', $this->pregQuotePaths($this->folderExclusions)) . '#i', $path) === 0
) {
$properPaths[] = $path;
}
}
return array_merge($this->configuration->getPaths(), $properPaths);
}

private function pregQuotePaths(array $paths)
{
foreach ($paths as $key => $path) {
$paths[$key] = preg_quote($path, '#');
}

return $paths;
}
}

0 comments on commit 61fc0aa

Please sign in to comment.