Skip to content
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

PHPCS Configuration management #1

Merged
merged 2 commits into from
Nov 1, 2016
Merged
Changes from 1 commit
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
64 changes: 34 additions & 30 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Symfony\Component\Process\Exception\LogicException;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;

/**
* PHP_CodeSniffer standard installation manager.
Expand All @@ -35,22 +35,22 @@ class Plugin implements PluginInterface, EventSubscriberInterface
/**
* @var Composer
*/
protected $composer;
private $composer;

/**
* @var IOInterface
*/
protected $io;
private $io;

/**
* @var array
*/
private $installedPaths;

/**
* @var string
* @var ProcessBuilder
*/
private $phpCodeSnifferBin;
private $processBuilder;

/**
* {@inheritDoc}
Expand All @@ -65,7 +65,9 @@ public function activate(Composer $composer, IOInterface $io)
$this->composer = $composer;
$this->io = $io;
$this->installedPaths = [];
$this->phpCodeSnifferBin = $composer->getConfig()->get('bin-dir') . DIRECTORY_SEPARATOR . 'phpcs';

$this->processBuilder = new ProcessBuilder();
$this->processBuilder->setPrefix($composer->getConfig()->get('bin-dir') . DIRECTORY_SEPARATOR . 'phpcs');

$this->loadInstalledPaths();
}
Expand All @@ -77,10 +79,10 @@ public static function getSubscribedEvents()
{
return [
ScriptEvents::POST_INSTALL_CMD => [
['onScriptPost', 0],
['onDependenciesChangedEvent', 0],
],
ScriptEvents::POST_UPDATE_CMD => [
['onScriptPost', 0],
['onDependenciesChangedEvent', 0],
],
];
}
Expand All @@ -92,16 +94,15 @@ public static function getSubscribedEvents()
* @throws LogicException
* @throws ProcessFailedException
*/
public function onScriptPost()
public function onDependenciesChangedEvent()
{
// Ensure PHP_CodeSniffer is installed
if ($this->isPHPCodeSnifferInstalled() === false) {
return;
}
if ($this->isPHPCodeSnifferInstalled() === true ) {
$installPathCleaned = $this->cleanInstalledPaths();
$installPathUpdated = $this->updateInstalledPaths();

// Clean and update. Trigger a save when something changed.
if ($this->cleanInstalledPaths() === true || $this->updateInstalledPaths() === true) {
$this->saveInstalledPaths();
if ($installPathCleaned === true || $installPathUpdated === true) {
$this->saveInstalledPaths();
}
}
}

Expand All @@ -114,10 +115,14 @@ public function onScriptPost()
*/
private function loadInstalledPaths()
{
$phpcs = new Process($this->phpCodeSnifferBin . ' --config-show installed_paths');
$phpcs->mustRun();

$phpcsInstalledPaths = str_replace('installed_paths: ', '', $phpcs->getOutput());
$output = $this->processBuilder
->setArguments(['--config-show', 'installed_paths'])
->getProcess()
->mustRun()
->getOutput();

$phpcsInstalledPaths = str_replace('installed_paths: ', '', $output);
$phpcsInstalledPaths = trim($phpcsInstalledPaths);

if ($phpcsInstalledPaths !== '') {
Expand All @@ -134,19 +139,18 @@ private function loadInstalledPaths()
*/
private function saveInstalledPaths()
{
// In case we have no installed paths anymore, ensure the config key is deleted, else update it.
if (count($this->installedPaths) === 0) {
$phpcs = new Process(
$this->phpCodeSnifferBin . ' --config-delete installed_paths'
);
} else {
$phpcsInstalledPaths = implode(',', $this->installedPaths);
$phpcs = new Process(
$this->phpCodeSnifferBin . ' --config-set installed_paths "' . $phpcsInstalledPaths . '"'
);
// By default we delete the installed paths
$arguments = ['--config-delete', 'installed_paths'];

// This changes in case we do have installed_paths
if (count($this->installedPaths) !== 0) {
$arguments = ['--config-set', 'installed_paths', implode(',', $this->installedPaths)];
Copy link
Member

@Potherca Potherca Nov 1, 2016

Choose a reason for hiding this comment

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

Is there a specific reason for not using array_push ($argument, implode(',', $this->installedPaths)); instead of defining the entire array again?

Oh, wait. Nevermind.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only the middle element is the same...

}

$phpcs->run();
$this->processBuilder
->setArguments($arguments)
->getProcess()
->mustRun();
}

/**
Expand Down