check_cs.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/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. $finder = new Finder();
  15. $finder
  16. ->files()
  17. ->name('*.md')
  18. ->name('*.php')
  19. ->name('*.php.dist')
  20. ->name('*.twig')
  21. ->name('*.xml')
  22. ->name('*.xml.dist')
  23. ->name('*.yml')
  24. ->in(__DIR__)
  25. ->notName(basename(__FILE__))
  26. ->exclude('.git')
  27. ->exclude('vendor')
  28. ;
  29. foreach ($finder as $file) { /* @var $file Symfony\Component\Finder\SplFileInfo */
  30. // These files are skipped because tests would break
  31. if (in_array($file->getRelativePathname(), array(
  32. 'tests/Symfony/Tests/Component/ClassLoader/ClassCollectionLoaderTest.php',
  33. 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/container9.php',
  34. 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/includes/foo.php',
  35. 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php',
  36. 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/services9.yml',
  37. 'tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php',
  38. 'tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher2.php',
  39. 'tests/Symfony/Tests/Component/Yaml/Fixtures/sfTests.yml',
  40. ))) {
  41. continue;
  42. }
  43. $old = file_get_contents($file->getRealpath());
  44. $new = $old;
  45. // [Structure] Never use short tags (<?);
  46. $new = str_replace('<? ', '<?php ', $new);
  47. // [Structure] Indentation is done by steps of four spaces (tabs are never allowed);
  48. $new = preg_replace_callback('/^( *)(\t+)/m', function ($matches) use ($new) {
  49. return $matches[1] . str_repeat(' ', strlen($matches[2]));
  50. }, $new);
  51. // [Structure] Use the linefeed character (0x0A) to end lines;
  52. $new = str_replace("\r\n", "\n", $new);
  53. // [Structure] Don't add trailing spaces at the end of lines;
  54. $new = preg_replace('/[ \t]*$/m', '', $new);
  55. // [Structure] Add a blank line before return statements;
  56. $new = preg_replace('/([^ {|\n]$)(\n return .+?$\n \}$)/m', '$1'."\n".'$2', $new);
  57. if ($new != $old) {
  58. file_put_contents($file->getRealpath(), $new);
  59. echo $file->getRelativePathname() . PHP_EOL;
  60. }
  61. }