check_cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * Coding Standards (a.k.a. CS)
  5. *
  6. * This script is designed to clean up the source files and thus follow coding
  7. * conventions.
  8. *
  9. * @see http://symfony.com/doc/2.0/contributing/code/standards.html
  10. *
  11. */
  12. require_once __DIR__.'/autoload.php.dist';
  13. use Symfony\Component\Finder\Finder;
  14. $fix = isset($argv[1]) && 'fix' == $argv[1];
  15. $finder = new Finder();
  16. $finder
  17. ->files()
  18. ->name('*.md')
  19. ->name('*.php')
  20. ->name('*.php.dist')
  21. ->name('*.twig')
  22. ->name('*.xml')
  23. ->name('*.xml.dist')
  24. ->name('*.yml')
  25. ->in(__DIR__.'/src', __DIR__.'/tests')
  26. ->notName(basename(__FILE__))
  27. ->exclude('.git')
  28. ->exclude('vendor')
  29. ;
  30. $exit = 0;
  31. foreach ($finder as $file) {
  32. // These files are skipped because tests would break
  33. if (in_array($file->getRelativePathname(), array(
  34. 'tests/Symfony/Tests/Component/ClassLoader/ClassCollectionLoaderTest.php',
  35. 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/container9.php',
  36. 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/includes/foo.php',
  37. 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php',
  38. 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/services9.yml',
  39. 'tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php',
  40. 'tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher2.php',
  41. 'tests/Symfony/Tests/Component/Yaml/Fixtures/sfTests.yml',
  42. ))) {
  43. continue;
  44. }
  45. $old = file_get_contents($file->getRealpath());
  46. $new = $old;
  47. // [Structure] Never use short tags (<?);
  48. $new = str_replace('<? ', '<?php ', $new);
  49. // [Structure] Indentation is done by steps of four spaces (tabs are never allowed);
  50. $new = preg_replace_callback('/^( *)(\t+)/m', function ($matches) use ($new) {
  51. return $matches[1].str_repeat(' ', strlen($matches[2]));
  52. }, $new);
  53. // [Structure] Use the linefeed character (0x0A) to end lines;
  54. $new = str_replace("\r\n", "\n", $new);
  55. // [Structure] Don't add trailing spaces at the end of lines;
  56. $new = preg_replace('/[ \t]*$/m', '', $new);
  57. // [Structure] Add a blank line before return statements;
  58. $new = preg_replace('/([^ {|\n]$)(\n return .+?$\n \}$)/m', '$1'."\n".'$2', $new);
  59. if ($new != $old) {
  60. $exit = 1;
  61. if ($fix) {
  62. file_put_contents($file->getRealpath(), $new);
  63. }
  64. echo $file->getRelativePathname().PHP_EOL;
  65. }
  66. }
  67. exit($exit);