Skip to content

Commit

Permalink
Add ignore files library and testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruli committed Feb 27, 2016
1 parent a1ebee5 commit 5734db3
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 39 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"symfony/config": "~3.0",
"symfony/yaml": "~3.0",
"fiunchinho/phpunit-randomizer": "~2.0",
"seld/jsonlint": "1.3.*"
"seld/jsonlint": "1.3.*",
"bruli/ignore-files": "dev-master"
},
"require-dev": {
"composer/composer": "^1.0@dev"
Expand Down
47 changes: 45 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@
<argument type="service" id="pre.commit.config"/>
<argument type="service" id="php.cs.fixer.handler"/>
</service>
<service id="ignore.files" class="IgnoreFiles\IgnoreFiles">
</service>
<service id="php.cs.fixer.handler" class="PhpGitHooks\Infrastructure\PhpCsFixer\PhpCsFixerHandler">
<argument type="service" id="output.handler" />
<argument type="service" id="ignore.files"/>
</service>
<service id="check.code.style.code.sniffer.pre.commit.executor" class="PhpGitHooks\Application\CodeSniffer\CheckCodeStyleCodeSnifferPreCommitExecutor">
<argument type="service" id="pre.commit.config"/>
Expand Down Expand Up @@ -100,5 +103,8 @@
<service id="json.lint.handler" class="PhpGitHooks\Infrastructure\JsonLint\JsonLintHandler">
<argument type="service" id="output.handler" />
</service>
<service id="file.reader" class="IgnoreFiles\Infrastructure\Disk\FileReader">
<argument type="string">ignore_files</argument>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion src/PhpGitHooks/Command/OutputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getTitle()
$text = $this->title;
$length = $this->getLength();

for ($i = 0; $i < $length; $i++) {
for ($i = 0; $i < $length; ++$i) {
$text .= self::TITLE_SEPARATOR;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function write($messages, $newline = true)
*
* @param string|array $messages The message as an array of lines or a single string
* @param bool $newline Whether to add a newline or not
* @param integer $size The size of line
* @param int $size The size of line
*/
public function overwrite($messages, $newline = true, $size = null)
{
Expand Down Expand Up @@ -125,7 +125,7 @@ public function askConfirmation($question, $default = true)
*
* @param string|array $question The question to ask
* @param callback $validator A PHP callback
* @param bool|integer $attempts Max number of times to ask before giving
* @param bool|int $attempts Max number of times to ask before giving
* up (false by default, which means infinite)
* @param string $default The default answer if none is given by the user
*
Expand Down Expand Up @@ -162,7 +162,7 @@ public function getAuthentications()
*
* @param string $repositoryName The unique name of repository
*
* @return boolean
* @return bool
*/
public function hasAuthentication($repositoryName)
{
Expand Down Expand Up @@ -214,7 +214,7 @@ public function writeError($messages, $newline = true)
*
* @param string|array $messages The message as an array of lines or a single string
* @param bool $newline Whether to add a newline or not
* @param integer $size The size of line
* @param int $size The size of line
*/
public function overwriteError($messages, $newline = true, $size = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getFile()
}

/**
* @param boolean $exists
* @param bool $exists
*/
public function setExists($exists)
{
Expand Down
8 changes: 4 additions & 4 deletions src/PhpGitHooks/Infrastructure/Git/HooksFileCopier.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ class HooksFileCopier

/**
* @param string $hook
* @param bool $enabled
* @param bool $enabled
*/
public function copy($hook, $enabled)
{
if (true === $enabled) {
if (false === file_exists(self::GIT_HOOKS_PATH . $hook)) {
if (false === file_exists(self::GIT_HOOKS_PATH.$hook)) {
$copy = new Process(
'cp ' . __DIR__ . '/../../../../hooks/' . $hook . ' ' . self::GIT_HOOKS_PATH . $hook
'cp '.__DIR__.'/../../../../hooks/'.$hook.' '.self::GIT_HOOKS_PATH.$hook
);
$copy->run();

$permissions = new Process('chmod 775 ' . self::GIT_HOOKS_PATH . $hook);
$permissions = new Process('chmod 775 '.self::GIT_HOOKS_PATH.$hook);
$permissions->run();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class InMemoryMergeValidator implements MergeValidatorInterface
private $merge;

/**
* @param boolean $merge
* @param bool $merge
*/
public function setMerge($merge)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace PhpGitHooks\Infrastructure\PhpCsFixer;

/**
* Class PhpCsFixerException.
*/
class PhpCsFixerException extends \Exception
{
protected $message = "There are some PhpCsFixer styling errors!.";
protected $message = 'There are some PhpCsFixer styling errors!.';
}
56 changes: 38 additions & 18 deletions src/PhpGitHooks/Infrastructure/PhpCsFixer/PhpCsFixerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace PhpGitHooks\Infrastructure\PhpCsFixer;

use IgnoreFiles\IgnoreFiles;
use PhpGitHooks\Application\Message\MessageConfigData;
use PhpGitHooks\Command\OutputHandlerInterface;
use PhpGitHooks\Infrastructure\Common\InteractiveToolInterface;
use PhpGitHooks\Infrastructure\Common\ToolHandler;
use Symfony\Component\Process\ProcessBuilder;
Expand All @@ -15,6 +17,22 @@ class PhpCsFixerHandler extends ToolHandler implements InteractiveToolInterface,
private $filesToAnalyze;
/** @var array */
private $levels = [];
/**
* @var IgnoreFiles
*/
private $ignoreFiles;

/**
* PhpCsFixerHandler constructor.
*
* @param OutputHandlerInterface $outputHandler
* @param IgnoreFiles $ignoreFiles
*/
public function __construct(OutputHandlerInterface $outputHandler, IgnoreFiles $ignoreFiles)
{
parent::__construct($outputHandler);
$this->ignoreFiles = $ignoreFiles;
}

/**
* @throws PhpCsFixerException
Expand All @@ -29,28 +47,30 @@ public function run(array $messages)
$errors = array();

foreach ($this->files as $file) {
$srcFile = preg_match($this->filesToAnalyze, $file);
if (false === $this->ignoreFiles->isIgnored($file)) {
$srcFile = preg_match($this->filesToAnalyze, $file);

if (!$srcFile) {
continue;
}
if (!$srcFile) {
continue;
}

$processBuilder = new ProcessBuilder(
array(
'php',
'bin/php-cs-fixer',
'--dry-run',
'fix',
$file,
'--level='.$level,
)
);
$processBuilder = new ProcessBuilder(
array(
'php',
'bin/php-cs-fixer',
'--dry-run',
'fix',
$file,
'--level='.$level,
)
);

$phpCsFixer = $processBuilder->getProcess();
$phpCsFixer->run();
$phpCsFixer = $processBuilder->getProcess();
$phpCsFixer->run();

if (false === $phpCsFixer->isSuccessful()) {
$errors[] = $phpCsFixer->getOutput();
if (false === $phpCsFixer->isSuccessful()) {
$errors[] = $phpCsFixer->getOutput();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
*/
class PhpLintException extends \Exception
{
protected $message = "There are some PHP syntax errors!.";
protected $message = 'There are some PHP syntax errors!.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function setUp()
->setMethods(['run'])
->disableOriginalConstructor()
->getMock();
$this->checkCodeStyleCodeSnifferPreCommitExecutor = new CheckCodeStyleCodeSnifferPreCommitExecutor(
$this->checkCodeStyleCodeSnifferPreCommitExecutor = new CheckCodeStyleCodeSnifferPreCommitExecutor(
$this->preCommitConfig,
$this->codeSnifferHandler
);
Expand All @@ -55,7 +55,7 @@ public function idDisabled()
*/
public function isEnabled()
{
$this->preCommitConfig->setExtraOptions(['enabled' => true, 'standard' => 'PSR2' ]);
$this->preCommitConfig->setExtraOptions(['enabled' => true, 'standard' => 'PSR2']);

$this->checkCodeStyleCodeSnifferPreCommitExecutor->run(
$this->outputInterface,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function setUp()
$this->extractCommitMessage = new InMemoryFileExtractInterface();
$this->configFile = \Mockery::mock(ConfigFile::class);
$this->configFile->shouldReceive('getMessageCommitConfiguration')
->andReturn(array('regular-expression' => '#[0-9]{2,7}', 'enabled' =>true));
->andReturn(array('regular-expression' => '#[0-9]{2,7}', 'enabled' => true));

$this->commitMessageValidator = new CommitMessageValidator(
$this->outputHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getCommitMsgConfigurationWithoutRegExpThrowsException()
{
$this->setExpectedException(InvalidConfigStructureException::class);
$this->configFileReader->fileContents = ['commit-msg' => [
'enabled' => true
'enabled' => true,
]];

$this->configFile->getMessageCommitConfiguration();
Expand Down
2 changes: 1 addition & 1 deletion src/PhpGitHooks/Tests/Command/OutputHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function titleRetunsValidLength()
{
$this->outputTitleHandler->setTitle('title test');

$this->assertSame(OutputHandler::MAX_LENGTH+13, strlen($this->outputTitleHandler->getTitle()));
$this->assertSame(OutputHandler::MAX_LENGTH + 13, strlen($this->outputTitleHandler->getTitle()));
}

/**
Expand Down

0 comments on commit 5734db3

Please sign in to comment.