Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion php-git-hooks.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pre-commit:
enabled: true
random-mode: true
phplint: true
phpcs: true
phpcs:
enabled: true
standard: PSR2
phpmd: true
commit-msg:
enabled: true
Expand Down
3 changes: 2 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
syntaxCheck="true"
stopOnError="false"
stopOnFailure="false"
backupGlobals="false">
backupGlobals="false"
bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="PhpGitHooks Unit Tests">
<directory>./src/PhpGitHooks/Tests/</directory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ public function __construct(HookConfigInterface $preCommitConfig, CodeSnifferHan
*/
public function run(OutputInterface $output, array $files, $needle)
{
if ($this->isEnabled()) {
$data = $this->preCommitConfig->extraOptions($this->commandName());

if (true === $data['enabled']) {
$this->codeSnifferHandler->setOutput($output);
$this->codeSnifferHandler->setFiles($files);
$this->codeSnifferHandler->setNeddle($needle);
$this->codeSnifferHandler->setStandard($data['standard']);
$this->codeSnifferHandler->run();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PhpGitHooks\Application\CodeSniffer;

final class InvalidPhpCsConfigDataException extends \Exception
{
protected $message = "Invalid entry for phpcs in your php-git-hooks.php file.\n
'Please remove phpcs entry and execute composer install.";
}
87 changes: 87 additions & 0 deletions src/PhpGitHooks/Application/CodeSniffer/PhpCsConfigData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace PhpGitHooks\Application\CodeSniffer;

use PhpGitHooks\Application\Config\AbstractToolConfigData;

final class PhpCsConfigData extends AbstractToolConfigData
{
const TOOL = 'phpcs';
/** @var string */
private $standard = 'PSR2';

/**
* @param array $data
*
* @return array
*
* @throws InvalidPhpCsConfigDataException
*/
public function createConfigData(array $data)
{
$this->configData = $data;
$this->setEnabled();
$this->setStandard();

return $this->configData[self::TOOL];
}

/**
* @throws InvalidPhpCsConfigDataException
*/
private function checkConfigData()
{
$configData = $this->configData[self::TOOL];

if (false === is_array($configData)) {
throw new InvalidPhpCsConfigDataException();
}

if (false === isset($configData['enabled'])) {
throw new InvalidPhpCsConfigDataException();
}
}

/**
* @return string
*/
protected function getToolName()
{
return self::TOOL;
}

/**
* @throws InvalidPhpCsConfigDataException
*/
private function setEnabled()
{
if (!isset($this->configData[self::TOOL])) {
$answer = $this->setQuestion(
sprintf('Do you want enable %s tool: ', strtoupper(self::TOOL)),
'Y',
'[Y/n]'
);
$answer = 'Y' === strtoupper($answer) ? true : false;
} else {
$this->checkConfigData();
$answer = $this->configData[self::TOOL]['enabled'];
}

$this->enableTool($answer);
}

/**
* @throws InvalidPhpCsConfigDataException
*/
private function setStandard()
{
if (!isset($this->configData[self::TOOL]['standard'])) {
$answer = $this->setQuestion(
sprintf('Which standard do you want to use for %s tool: ', strtoupper(self::TOOL)),
$this->standard,
'[PSR2/PHPCS/MySource/Zend/Squiz/PSR1/PEAR]'
);
$this->configData[self::TOOL]['standard'] = $answer;
}
}
}
27 changes: 19 additions & 8 deletions src/PhpGitHooks/Application/Composer/PreCommitProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace PhpGitHooks\Application\Composer;

use PhpGitHooks\Application\CodeSniffer\PhpCsConfigData;
use PhpGitHooks\Application\PhpCsFixer\PhpCsFixerConfigData;
use PhpGitHooks\Application\PhpUnit\PhpUnitConfigData;

final class PreCommitProcessor extends Processor
{
private $simpleTools = ['phpcs', 'jsonlint', 'phplint', 'phpmd'];
private $simpleTools = ['jsonlint', 'phplint', 'phpmd'];

/**
* @param array $configData
Expand Down Expand Up @@ -68,10 +69,20 @@ private function configSimpleTools(array $execute)
*/
private function configComplexTools(array $execute)
{
$this->configPhpCs($execute);
$this->configPhpCsFixer($execute);
$this->configPhpUnit($execute);
}

/**
* @param array $execute
*/
private function configPhpCs(array $execute)
{
$phpCsConfig = new PhpCsConfigData($this->io);
$this->configData['pre-commit']['execute'][PhpCsConfigData::TOOL] = $phpCsConfig
->createConfigData($execute);
}

/**
* @param array $execute
Expand All @@ -83,6 +94,13 @@ private function configPhpCsFixer(array $execute)
->createConfigData($execute);
}

private function configPhpUnit(array $execute)
{
$phpUnitconfig = new PhpUnitConfigData($this->io);
$this->configData['pre-commit']['execute'][PhpUnitConfigData::TOOL] = $phpUnitconfig
->createConfigData($execute);
}

/**
* @param string $tool
*
Expand All @@ -92,11 +110,4 @@ private function setQuestionTool($tool)
{
return $this->setQuestion(sprintf('Do you want enable %s tool?', strtoupper($tool)), '[Y/n]', 'Y');
}

private function configPhpUnit(array $execute)
{
$phpUnitconfig = new PhpUnitConfigData($this->io);
$this->configData['pre-commit']['execute'][PhpUnitConfigData::TOOL] = $phpUnitconfig
->createConfigData($execute);
}
}
13 changes: 11 additions & 2 deletions src/PhpGitHooks/Infrastructure/CodeSniffer/CodeSnifferHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
*/
class CodeSnifferHandler extends ToolHandler
{
const STANDARD = 'PSR2';
/** @var array */
private $files;
/** @var string */
private $neddle;
/** @var string */
private $standard = 'PSR2';

/**
* @throws InvalidCodingStandardException
Expand All @@ -31,7 +32,7 @@ public function run()
continue;
}

$processBuilder = new ProcessBuilder(array('php', 'bin/phpcs', '--standard='.self::STANDARD.'', $file));
$processBuilder = new ProcessBuilder(array('php', 'bin/phpcs', '--standard='.$this->standard, $file));
/** @var Process $phpCs */
$phpCs = $processBuilder->getProcess();
$phpCs->run();
Expand Down Expand Up @@ -63,4 +64,12 @@ public function setFiles($files)
{
$this->files = $files;
}

/**
* @param array $standard
*/
public function setStandard($standard)
{
$this->standard = $standard;
}
}
4 changes: 3 additions & 1 deletion src/PhpGitHooks/Infrastructure/Git/HooksFileCopier.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public function copy($hook, $enabled)
{
if (true === $enabled) {
if (false === file_exists(self::GIT_HOOKS_PATH . $hook)) {
$copy = new Process('cp ' . __DIR__ . '/../../../../hooks/' . $hook . self::GIT_HOOKS_PATH . $hook);
$copy = new Process(
'cp ' . __DIR__ . '/../../../../hooks/' . $hook . ' ' . self::GIT_HOOKS_PATH . $hook
);
$copy->run();

$permissions = new Process('chmod 775 ' . self::GIT_HOOKS_PATH . $hook);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ protected function setUp()
{
$this->outputInterface = new InMemoryOutputInterface();
$this->preCommitConfig = new InMemoryHookConfig();
$this->codeSnifferHandler = \Mockery::mock(CodeSnifferHandler::class);
$this->codeSnifferHandler = $this->getMockBuilder('\PhpGitHooks\Infrastructure\CodeSniffer\CodeSnifferHandler')
->setMethods(['run'])
->disableOriginalConstructor()
->getMock();
$this->checkCodeStyleCodeSnifferPreCommitExecutor = new CheckCodeStyleCodeSnifferPreCommitExecutor(
$this->preCommitConfig,
$this->codeSnifferHandler
Expand All @@ -36,33 +39,28 @@ protected function setUp()
/**
* @test
*/
public function isDisabled()
public function idDisabled()
{
$this->preCommitConfig->setEnabled(false);
$this->preCommitConfig->setExtraOptions(['enabled' => false, 'standard' => '']);

$this->checkCodeStyleCodeSnifferPreCommitExecutor->run(
$this->outputInterface,
array(),
'needle'
'neddle'
);
}

/**
* @test
*/
public function isEnable()
public function isEnabled()
{
$this->preCommitConfig->setEnabled(true);

$this->codeSnifferHandler->shouldReceive('setOutput');
$this->codeSnifferHandler->shouldReceive('setFiles');
$this->codeSnifferHandler->shouldReceive('setNeddle');
$this->codeSnifferHandler->shouldReceive('run');
$this->preCommitConfig->setExtraOptions(['enabled' => true, 'standard' => 'PSR2' ]);

$this->checkCodeStyleCodeSnifferPreCommitExecutor->run(
$this->outputInterface,
array(),
'needle'
'neddle'
);
}
}
Loading