Skip to content

Commit

Permalink
Fixers check
Browse files Browse the repository at this point in the history
  • Loading branch information
soullivaneuh committed Sep 25, 2015
1 parent b6b2df6 commit 5a0d6be
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .styleci.yml
Expand Up @@ -16,3 +16,5 @@ disabled:
finder:
not-name:
- "*.php.twig"
not-path:
- "src/StyleCI/Fixers.php"
4 changes: 3 additions & 1 deletion .travis.yml
Expand Up @@ -42,7 +42,9 @@ before_script:
- if [ "$PHP_CS_FIXER_VERSION" != "" ]; then composer require "fabpot/php-cs-fixer:${PHP_CS_FIXER_VERSION}" --no-update; fi;
- composer update --prefer-dist --no-interaction $COMPOSER_FLAGS

script: make test
script:
- make test
- ./bridge styleci:config:check

after_script:
- php vendor/bin/coveralls -v
Expand Down
2 changes: 2 additions & 0 deletions src/Console/Application.php
Expand Up @@ -2,6 +2,7 @@

namespace SLLH\StyleCIBridge\Console;

use SLLH\StyleCIBridge\Console\Command\StyleCIConfigCheckCommand;
use SLLH\StyleCIBridge\Console\Command\StyleCIConfigUpdateCommand;
use Symfony\Component\Console\Application as BaseApplication;

Expand All @@ -18,5 +19,6 @@ public function __construct()
parent::__construct();

$this->add(new StyleCIConfigUpdateCommand());
$this->add(new StyleCIConfigCheckCommand());
}
}
45 changes: 45 additions & 0 deletions src/Console/Command/StyleCIConfigCheckCommand.php
@@ -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;
}
}
55 changes: 3 additions & 52 deletions src/Console/Command/StyleCIConfigUpdateCommand.php
Expand Up @@ -2,11 +2,10 @@

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;
use Symfony\CS\Tokenizer\Token;
use Symfony\CS\Tokenizer\Tokens;

/**
* @author Sullivan Senechal <soullivaneuh@gmail.com>
Expand All @@ -29,55 +28,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (version_compare(PHP_VERSION, '5.6', '<')) {
throw new \RuntimeException('You must run this command under PHP 5.6 or higher.');
}

$fixersTab = array();

$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()),
));
}
}
$fixersTab[$name] = $fixers;
}
}

$twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__.'/../..'));
file_put_contents(__DIR__.'/../../StyleCI/Fixers.php', $twig->render('StyleCI/Fixers.php.twig', array('fixersTab' => $fixersTab)));
}

/**
* @param string $tokenContent
*
* @return string
*/
private function getString($tokenContent)
{
return str_replace(array('"', "'"), '', $tokenContent);
$generator = new FixersGenerator();
$generator->generate();
}
}
96 changes: 96 additions & 0 deletions src/StyleCI/FixersGenerator.php
@@ -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);
}
}

0 comments on commit 5a0d6be

Please sign in to comment.