Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

php-cs-fixer #11

Merged
merged 21 commits into from Apr 17, 2015
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
143 changes: 143 additions & 0 deletions Bin/Cs.php
@@ -0,0 +1,143 @@
<?php

/**
* Hoa
*
*
* @license
*
* New BSD License
*
* Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Hoa nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

namespace Hoa\Devtools\Bin;

use Hoa\Console;
use Hoa\Core;

/**
* Class \Hoa\Devtools\Bin\Cs.
*
* Wrapper around `php-cs-fixer`.
*
* @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
* @copyright Copyright © 2007-2015 Ivan Enderlin.
* @license New BSD License
*/

class Cs extends Console\Dispatcher\Kit {

Choose a reason for hiding this comment

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

forget ?

Copy link
Member Author

Choose a reason for hiding this comment

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

We didn't not apply the CS fixer on these files yet.


/**
* Options description.
*
* @var \Hoa\Devtools\Bin\Cs array
*/
protected $options = [
['help', Console\GetOption::NO_ARGUMENT, 'h'],
['help', Console\GetOption::NO_ARGUMENT, '?']
];



/**
* The entry method.
*
* @access public
* @return int
*/
public function main ( ) {

Choose a reason for hiding this comment

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

forget ?


while(false !== $c = $this->getOption($v)) switch($c) {

case '__ambiguous':
$this->resolveOptionAmbiguity($v);
break;

case 'h':
case '?':
default:
return $this->usage();
break;
}

$this->parser->listInputs($command, $path);

if(empty($command) || empty($path))
return $this->usage();

$phpCsFixer = Console\Processus::locate('php-cs-fixer');
$configurationFile = resolve(
'hoa://Library/Devtools/Resource/PHPCSFixer/ConfigurationFile.php'
);

if(empty($phpCsFixer))
throw new Console\Exception(
'php-cs-fixer binary is not found.', 0);

$processus = new Console\Processus(
$phpCsFixer,
[
$command,
'--config-file' => $configurationFile,
$path
]
);
$processus->on('input', function ( ) {

return false;
});
$processus->on('output', function ( Core\Event\Bucket $bucket ) {

echo $bucket->getData()['line'], "\n";

return;
});
$processus->run();

return;
}

/**
* The command usage.
*
* @access public
* @return int
*/
public function usage ( ) {

echo 'Usage : devtools:cs <options> command path', "\n",
'Options :', "\n",
$this->makeUsageOptionsList([
'help' => 'This help.'
]), "\n";

return;
}
}

__halt_compiler();
Wrapper around `php-cs-fixer`.
50 changes: 50 additions & 0 deletions Resource/PHPCSFixer/ConfigurationFile.php
@@ -0,0 +1,50 @@
<?php

// Hoa defined fixers.
$fixers = [
'Author',
'ControlFlowStatement',
'Copyright',
'OpeningTag',
'PhpdocAccess',
'PhpdocThrows',
'PhpdocVar'
];

$out = Symfony\CS\Config\Config::create();
$out->level(Symfony\CS\FixerInterface::PSR2_LEVEL);

foreach ($fixers as $fixer) {
require
__DIR__ . DIRECTORY_SEPARATOR .
'Fixer' . DIRECTORY_SEPARATOR .
$fixer . '.php';

$classname = 'Hoa\Devtools\Resource\PHPCsFixer\Fixer\\' . $fixer;
$out->addCustomFixer(new $classname());
}

return
$out->fixers([
'align_double_arrow',
'align_equals',
'concat_with_spaces',
'no_blank_lines_after_class_opening',
'ordered_use',
'remove_leading_slash_use',
'remove_leading_slash_uses',
'self_accessor',
'short_array_syntax',
'spaces_cast',
'unused_use',
'whitespacy_lines',

// Hoa defined
'author',
'control_flow_statement',
'copyright',
'opening_tag',
'phpdoc_access',
'phpdoc_throws',
'phpdoc_var'
]);
54 changes: 54 additions & 0 deletions Resource/PHPCSFixer/Fixer/Author.php
@@ -0,0 +1,54 @@
<?php

namespace Hoa\Devtools\Resource\PHPCsFixer\Fixer;

use Symfony\CS\AbstractFixer;
use Symfony\CS\DocBlock\DocBlock;
use Symfony\CS\FixerInterface;
use Symfony\CS\Tokenizer\Tokens;
use SplFileInfo;

/**
* Remove `@author`.
*/
class Author extends AbstractFixer
{
public function fix(SplFileInfo $file, $content)
{
$tokens = Tokens::fromCode($content);

foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) {

$docBlock = new DocBlock($token->getContent());
$annotations = $docBlock->getAnnotationsOfType('author');

if (empty($annotations)) {
continue;
}

foreach ($annotations as $annotation) {
$annotation->remove();
}

$token->setContent($docBlock->getContent());

}

return $tokens->generateCode();
}

public function getDescription()
{
return 'Remove `@author`.';
}

public function getName()
{
return 'author';
}

public function getLevel()
{
return FixerInterface::CONTRIB_LEVEL;
}
}
69 changes: 69 additions & 0 deletions Resource/PHPCSFixer/Fixer/ControlFlowStatement.php
@@ -0,0 +1,69 @@
<?php

namespace Hoa\Devtools\Resource\PHPCsFixer\Fixer;

use Symfony\CS\AbstractFixer;
use Symfony\CS\FixerInterface;
use Symfony\CS\Tokenizer\Tokens;
use SplFileInfo;

/**
* Add a newline before `return`, `break` and `continue` if needed.
* Inspired by the `ReturnFixer` class, provided with `php-cs-fixer`.
*/
class ControlFlowStatement extends AbstractFixer
{
public function fix(\SplFileInfo $file, $content)
{
$tokens = Tokens::fromCode($content);

for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
$token = $tokens[$index];

if (!$token->isGivenKind([T_RETURN, T_BREAK, T_CONTINUE])) {
continue;
}

$prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($index)];

if (!$prevNonWhitespaceToken->equalsAny([';', '}'])) {
continue;
}

$prevToken = $tokens[$index - 1];

if ($prevToken->isWhitespace()) {
$parts = explode("\n", $prevToken->getContent());
$countParts = count($parts);

if (1 === $countParts) {
$prevToken->setContent(rtrim($prevToken->getContent(), " \t") . "\n\n");
} elseif (count($parts) <= 2) {
$prevToken->setContent("\n" . $prevToken->getContent());
}
} else {
$tokens->insertAt($index, new Token([T_WHITESPACE, "\n\n"]));

++$index;
++$limit;
}
}

return $tokens->generateCode();
}

public function getDescription()
{
return 'Add a newline before `return`, `break` and `continue` if needed.';
}

public function getName()
{
return 'control_flow_statement';
}

public function getLevel()
{
return FixerInterface::CONTRIB_LEVEL;
}
}
73 changes: 73 additions & 0 deletions Resource/PHPCSFixer/Fixer/Copyright.php
@@ -0,0 +1,73 @@
<?php

namespace Hoa\Devtools\Resource\PHPCsFixer\Fixer;

use Symfony\CS\AbstractFixer;
use Symfony\CS\DocBlock\DocBlock;
use Symfony\CS\FixerInterface;
use Symfony\CS\Tokenizer\Tokens;
use SplFileInfo;

/**
* Keep copyright up-to-date.
*/
class Copyright extends AbstractFixer
{
public function fix(SplFileInfo $file, $content)
{
$tokens = Tokens::fromCode($content);
$thisYear = date('Y');

foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) {

$token->setContent(
preg_replace_callback(
'/Copyright © (?<firstYear>\d{4})-\d{4}, [^\.]+/',
function($matches) use($thisYear) {

return
'Copyright © ' .
$matches['firstYear'] . '-' . $thisYear . ', ' .
'Hoa community';
},
$token->getContent()
)
);

$docBlock = new DocBlock($token->getContent());
$annotations = $docBlock->getAnnotationsOfType('copyright');

if (empty($annotations)) {
continue;
}

foreach ($annotations as $annotation) {
$line = $docBlock->getLine($annotation->getStart());
$line->setContent(
' * @copyright Copyright © 2007-' . $thisYear .
' Hoa community' . "\n"
);
}

$token->setContent($docBlock->getContent());

}

return $tokens->generateCode();
}

public function getDescription()
{
return 'Keep copyright up-to-date.';
}

public function getName()
{
return 'copyright';
}

public function getLevel()
{
return FixerInterface::CONTRIB_LEVEL;
}
}