Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6b2df6
commit 5a0d6be
Showing
6 changed files
with
151 additions
and
53 deletions.
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 |
---|---|---|
|
@@ -16,3 +16,5 @@ disabled: | |
finder: | ||
not-name: | ||
- "*.php.twig" | ||
not-path: | ||
- "src/StyleCI/Fixers.php" |
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
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
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,45 @@ | ||
<?php | ||
|
||
namespace SLLH\StyleCIBridge\Console\Command; | ||
|
||
use SLLH\StyleCIBridge\StyleCI\FixersGenerator; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @author Sullivan Senechal <soullivaneuh@gmail.com> | ||
*/ | ||
class StyleCIConfigCheckCommand extends Command | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('styleci:config:check') | ||
->setDescription('Check if StyleCI fixers config is up to date.') | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$fixersClass = file_get_contents(__DIR__.'/../../StyleCI/Fixers.php'); | ||
|
||
$generator = new FixersGenerator(); | ||
|
||
if ($fixersClass === $generator->getFixersClass()) { | ||
$output->writeln('StyleCI fixers are up to date.'); | ||
} else { | ||
$output->writeln('<error>StyleCI fixers are out to date. Run styleci:config:update command to fix it.</error>'); | ||
|
||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
} |
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
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,96 @@ | ||
<?php | ||
|
||
namespace SLLH\StyleCIBridge\StyleCI; | ||
|
||
use Symfony\CS\Tokenizer\Token; | ||
use Symfony\CS\Tokenizer\Tokens; | ||
|
||
/** | ||
* @author Sullivan Senechal <soullivaneuh@gmail.com> | ||
*/ | ||
final class FixersGenerator | ||
{ | ||
const STYLE_CI_CLASS_FILE = 'https://github.com/StyleCI/Config/raw/master/src/Config.php'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $fixersTab = array(); | ||
|
||
/** | ||
* Generate Fixers.php file. | ||
*/ | ||
public function generate() | ||
{ | ||
file_put_contents(__DIR__.'/../StyleCI/Fixers.php', $this->getFixersClass()); | ||
} | ||
|
||
/** | ||
* Generate Fixers.php content. | ||
* | ||
* @return string | ||
*/ | ||
public function getFixersClass() | ||
{ | ||
$twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__.'/..')); | ||
|
||
return $twig->render('StyleCI/Fixers.php.twig', array('fixersTab' => $this->getFixersTab())); | ||
} | ||
|
||
/** | ||
* Returns fixers tab from StyleCI Config ckass. | ||
* | ||
* @return array | ||
*/ | ||
public function getFixersTab() | ||
{ | ||
$this->makeFixersTab(); | ||
|
||
return $this->fixersTab; | ||
} | ||
|
||
private function makeFixersTab() | ||
{ | ||
$configClass = file_get_contents('https://github.com/StyleCI/Config/raw/master/src/Config.php'); | ||
|
||
/** @var Tokens|Token[] $tokens */ | ||
$tokens = Tokens::fromCode($configClass); | ||
/* | ||
* @var int | ||
* @var Token | ||
*/ | ||
foreach ($tokens->findGivenKind(T_CONST) as $index => $token) { | ||
if ('[' === $tokens[$index + 6]->getContent()) { | ||
$name = strtolower($tokens[$index + 2]->getContent()); | ||
$fixers = array(); | ||
for ($i = $index + 7; ']' !== $tokens[$i]->getContent(); ++$i) { | ||
if ($tokens[$i]->isGivenKind(T_CONSTANT_ENCAPSED_STRING) && ',' === $tokens[$i + 1]->getContent()) { | ||
// Simple array management | ||
array_push($fixers, array('name' => $this->getString($tokens[$i]->getContent()))); | ||
} elseif ($tokens[$i]->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) { | ||
// Double arrow management | ||
$key = $this->getString($tokens[$i]->getContent()); | ||
for (++$i; $tokens[$i]->isGivenKind(T_DOUBLE_ARROW); ++$i) { | ||
} | ||
$i += 3; | ||
array_push($fixers, array( | ||
'key' => $key, | ||
'name' => $this->getString($tokens[$i]->getContent()), | ||
)); | ||
} | ||
} | ||
$this->fixersTab[$name] = $fixers; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param string $tokenContent | ||
* | ||
* @return string | ||
*/ | ||
private function getString($tokenContent) | ||
{ | ||
return str_replace(array('"', "'"), '', $tokenContent); | ||
} | ||
} |