Skip to content

Commit

Permalink
added initial set of files
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 16, 2012
0 parents commit bca076a
Show file tree
Hide file tree
Showing 25 changed files with 919 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
vendor

19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2012 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
16 changes: 16 additions & 0 deletions README.md
@@ -0,0 +1,16 @@
Symfony Coding Standard Fixer
=============================

This tool analyzes the Symfony source code to fix as much coding standards
problems as possible.

Download the `symfony-cs-fixer.phar` file and execute it:

# For the Symfony 2.0 branch
php symfony-cs-fixer.phar fix /path/to/symfony/src Symfony20Finder

# For the Symfony 2.1/master branch
php symfony-cs-fixer.phar fix /path/to/symfony/src Symfony20Finder

See http://symfony.com/doc/current/contributing/code/standards.html for more
information about the Symfony Coding Standards.
41 changes: 41 additions & 0 deletions Symfony/CS/Console/Application.php
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony CS utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\CS\Console;

use Symfony\Component\Console\Application as BaseApplication;
use Symfony\CS\Console\Command\FixCommand;
use Symfony\CS\Console\Command\CompileCommand;
use Symfony\CS\Fixer;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class Application extends BaseApplication
{
/**
* Constructor.
*/
public function __construct()
{
error_reporting(-1);

parent::__construct('Symfony CS Fixer', Fixer::VERSION);

$this->add(new FixCommand());
$this->add(new CompileCommand());
}

public function getLongVersion()
{
return parent::getLongVersion().' by <comment>Fabien Potencier</comment>';
}
}
45 changes: 45 additions & 0 deletions Symfony/CS/Console/Command/CompileCommand.php
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony CS utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\CS\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\CS\Util\Compiler;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class CompileCommand extends Command
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('compile')
->setDescription('Compiles the fixer as a phar file')
;
}

/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$compiler = new Compiler();
$compiler->compile();
}
}
67 changes: 67 additions & 0 deletions Symfony/CS/Console/Command/FixCommand.php
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the Symfony CS utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\CS\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\CS\Fixer;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class FixCommand extends Command
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('fix')
->setDefinition(array(
new InputArgument('dir', InputArgument::REQUIRED, 'The Symfony dir'),
new InputArgument('finder', InputArgument::REQUIRED, 'The Finder short class name to use'),
))
->setDescription('Fixes a project')
->setHelp(<<<EOF
The <info>fix</info> command tries to fix as much coding standards
problems as possible:
<info>php fixer /path/to/symfony/src Symfony21Finder</info>
See http://symfony.com/doc/current/contributing/code/standards.html for more
information about the Symfony Coding Standards.
EOF
);
}

/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$fixer = new Fixer();
$fixer->registerBuiltInFixers();

$class = 'Symfony\\CS\\Finder\\'.$input->getArgument('finder');
$iterator = new $class($input->getArgument('dir'));

$changed = $fixer->fix($iterator);

foreach ($changed as $i => $file) {
$output->writeln(sprintf('%4d) %s', $i, $file));
}
}
}
41 changes: 41 additions & 0 deletions Symfony/CS/Finder/Symfony20Finder.php
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony CS utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\CS\Finder;

use Symfony\Component\Finder\Finder;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class Symfony20Finder extends SymfonyFinder
{
public function __construct($dir)
{
parent::__construct($dir);

$this->in($dir.'/tests');
}

protected function getFileToExclude()
{
return array(
'Symfony/Tests/Component/ClassLoader/ClassCollectionLoaderTest.php',
'Symfony/Tests/Component/DependencyInjection/Fixtures/containers/container9.php',
'Symfony/Tests/Component/DependencyInjection/Fixtures/includes/foo.php',
'Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php',
'Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/services9.yml',
'Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php',
'Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher2.php',
'Symfony/Tests/Component/Yaml/Fixtures/sfTests.yml',
);
}
}
27 changes: 27 additions & 0 deletions Symfony/CS/Finder/Symfony21Finder.php
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony CS utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\CS\Finder;

use Symfony\Component\Finder\Finder;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class Symfony21Finder extends SymfonyFinder
{
protected function getFileToExclude()
{
return array(
'Symfony/Component/Yaml/Tests/Fixtures/sfTests.yml',
);
}
}
52 changes: 52 additions & 0 deletions Symfony/CS/Finder/SymfonyFinder.php
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Symfony CS utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\CS\Finder;

use Symfony\Component\Finder\Finder;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class SymfonyFinder extends Finder
{
public function __construct($dir)
{
parent::__construct();

$files = $this->getFileToExclude();

$this
->files()
->name('*.md')
->name('*.php')
->name('*.twig')
->name('*.xml')
->name('*.yml')
->exclude('.git')
->exclude('vendor')
->filter(function (\SplFileInfo $file) use ($files) {
return !in_array($file->getRelativePathname(), $files);
})
->in($dir.'/src')
;
}

/**
* Excludes files because modifying them would break (mainly useful for fixtures in unit tests).
*
* @return array
*/
protected function getFileToExclude()
{
return array();
}
}
66 changes: 66 additions & 0 deletions Symfony/CS/Fixer.php
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the Symfony CS utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\CS;

use Symfony\Component\Finder\Finder;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class Fixer
{
const VERSION = '0.1';

protected $fixers = array();

public function registerBuiltInFixers()
{
foreach (Finder::create()->files()->in(__DIR__.'/Fixer') as $file) {
$class = 'Symfony\\CS\\Fixer\\'.basename($file, '.php');
$this->addFixer(new $class());
}
}

public function addFixer(FixerInterface $fixer)
{
$this->fixers[] = $fixer;
}

public function fix(\Traversable $iterator)
{
$changed = array();
foreach ($iterator as $file) {
if ($this->fixFile($file)) {
$changed[] = $file->getRelativePathname();
}
}

return $changed;
}

public function fixFile(\SplFileInfo $file)
{
$new = $old = file_get_contents($file->getRealpath());

foreach ($this->fixers as $fixer) {
if ($fixer->supports($file)) {
$new = $fixer->fix($file, $new);
}
}

if ($new != $old) {
file_put_contents($file->getRealpath(), $new);

return true;
}
}
}

0 comments on commit bca076a

Please sign in to comment.