This repository has been archived by the owner on Sep 20, 2021. It is now read-only.
php-cs-fixer #11
Merged
Merged
php-cs-fixer #11
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
97df6a5
Add the `phpdoc_var` fixer.
Hywan 809676f
Add the `phpdoc_access` fixer.
Hywan 049b496
Rename a variable.
Hywan 29af74c
Add the `phpdoc_throws` fixer.
Hywan b2d7e5f
Add a wrapper around `php-cs-fixer`.
Hywan 5c21904
Add the `author` fixer.
Hywan 6ced6ee
Fix CS.
Hywan e7f53ad
Add the `copyright` fixer.
Hywan cd6f59a
Use the `no_blank_lines_after_class_opening` fixer.
Hywan 0c28bb3
Remove the `return` fixer.
Hywan 2dc7bbe
We must replace `@throw` by `@throws`.
Hywan 715355b
Add the `control_flow_statement` fixer.
Hywan 7eedf88
Add the `opening_tag` fixer.
Hywan 2e2dd14
Automate the declaration of custom fixers.
Hywan b9a8350
Add new provided fixers.
Hywan 48470d6
Add `control_flow_statement` and `opening_tag`.
Hywan 92a8c2c
Use build-in fixers.
Hywan add112e
Add licenses and API documentation.
Hywan 4a84e3b
Add `--dry-run` and `--diff`. `fix` always.
Hywan fc73487
Add the `no_blank_lines_before_entity` fixer.
Hywan 9a0d361
Move to PSR-1 and PSR-2.
Hywan File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
|
||
/** | ||
* 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 ( ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forget ?
There was a problem hiding this comment.
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.