2011-06-08 10:12:44 +01:00
|
|
|
#!/usr/bin/env php
|
2011-06-08 09:34:35 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Coding Standards (a.k.a. CS)
|
|
|
|
*
|
|
|
|
* This script is designed to clean up the source files and thus follow coding
|
|
|
|
* conventions.
|
|
|
|
*
|
|
|
|
* @see http://symfony.com/doc/2.0/contributing/code/standards.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once __DIR__.'/autoload.php.dist';
|
|
|
|
|
|
|
|
use Symfony\Component\Finder\Finder;
|
|
|
|
|
2011-06-08 10:24:35 +01:00
|
|
|
$fix = isset($argv[1]) && 'fix' == $argv[1];
|
|
|
|
|
2011-06-08 09:34:35 +01:00
|
|
|
$finder = new Finder();
|
|
|
|
$finder
|
|
|
|
->files()
|
|
|
|
->name('*.md')
|
|
|
|
->name('*.php')
|
|
|
|
->name('*.php.dist')
|
|
|
|
->name('*.twig')
|
|
|
|
->name('*.xml')
|
|
|
|
->name('*.xml.dist')
|
|
|
|
->name('*.yml')
|
2011-06-08 15:21:52 +01:00
|
|
|
->in(__DIR__)
|
2011-06-08 09:34:35 +01:00
|
|
|
->notName(basename(__FILE__))
|
|
|
|
->exclude('.git')
|
|
|
|
->exclude('vendor')
|
|
|
|
;
|
|
|
|
|
2011-06-08 10:24:35 +01:00
|
|
|
$exit = 0;
|
|
|
|
foreach ($finder as $file) {
|
2011-06-08 15:21:52 +01:00
|
|
|
|
|
|
|
/* @var $file Symfony\Component\Finder\SplFileInfo */
|
|
|
|
|
2011-06-08 09:34:35 +01:00
|
|
|
// These files are skipped because tests would break
|
|
|
|
if (in_array($file->getRelativePathname(), array(
|
|
|
|
'tests/Symfony/Tests/Component/ClassLoader/ClassCollectionLoaderTest.php',
|
|
|
|
'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/container9.php',
|
|
|
|
'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/includes/foo.php',
|
|
|
|
'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php',
|
|
|
|
'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/services9.yml',
|
|
|
|
'tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php',
|
|
|
|
'tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher2.php',
|
|
|
|
'tests/Symfony/Tests/Component/Yaml/Fixtures/sfTests.yml',
|
|
|
|
))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$old = file_get_contents($file->getRealpath());
|
|
|
|
|
|
|
|
$new = $old;
|
|
|
|
|
2011-06-08 10:37:47 +01:00
|
|
|
// [Structure] Never use short tags (<?)
|
2011-06-08 09:34:35 +01:00
|
|
|
$new = str_replace('<? ', '<?php ', $new);
|
|
|
|
|
2011-06-08 10:37:47 +01:00
|
|
|
// [Structure] Indentation is done by steps of four spaces (tabs are never allowed)
|
2011-06-08 09:34:35 +01:00
|
|
|
$new = preg_replace_callback('/^( *)(\t+)/m', function ($matches) use ($new) {
|
2011-06-08 10:24:35 +01:00
|
|
|
return $matches[1].str_repeat(' ', strlen($matches[2]));
|
2011-06-08 09:34:35 +01:00
|
|
|
}, $new);
|
|
|
|
|
2011-06-08 10:37:47 +01:00
|
|
|
// [Structure] Use the linefeed character (0x0A) to end lines
|
2011-06-08 09:34:35 +01:00
|
|
|
$new = str_replace("\r\n", "\n", $new);
|
|
|
|
|
2011-06-08 10:37:47 +01:00
|
|
|
// [Structure] Don't add trailing spaces at the end of lines
|
2011-06-08 09:34:35 +01:00
|
|
|
$new = preg_replace('/[ \t]*$/m', '', $new);
|
|
|
|
|
2011-06-08 10:37:47 +01:00
|
|
|
// [Structure] Add a blank line before return statements
|
|
|
|
$new = preg_replace_callback('/(^.*$)\n(^ +return)/m', function ($match) {
|
2011-06-08 15:21:52 +01:00
|
|
|
// don't add it if the previous line is ...
|
|
|
|
if (
|
|
|
|
preg_match('/\{$/m', $match[1]) || // ... ending with an opening brace
|
|
|
|
preg_match('/\:$/m', $match[1]) || // ... ending with a colon (e.g. a case statement)
|
|
|
|
preg_match('%^ *//%m', $match[1]) || // ... an inline comment
|
|
|
|
preg_match('/^$/m', $match[1]) // ... already blank
|
|
|
|
) {
|
2011-06-08 10:37:47 +01:00
|
|
|
return $match[1]."\n".$match[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $match[1]."\n\n".$match[2];
|
|
|
|
}, $new);
|
|
|
|
|
|
|
|
// [Structure] A file must always ends with a linefeed character
|
2011-06-08 15:21:52 +01:00
|
|
|
if (strlen($new) && "\n" != substr($new, -1)) {
|
2011-06-08 11:16:48 +01:00
|
|
|
$new .= "\n";
|
|
|
|
}
|
2011-06-08 09:34:35 +01:00
|
|
|
|
|
|
|
if ($new != $old) {
|
2011-06-08 10:24:35 +01:00
|
|
|
$exit = 1;
|
|
|
|
if ($fix) {
|
|
|
|
file_put_contents($file->getRealpath(), $new);
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $file->getRelativePathname().PHP_EOL;
|
2011-06-08 09:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-08 10:24:35 +01:00
|
|
|
|
|
|
|
exit($exit);
|