check_cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_callback('/(^.*$)\n(^ +return)/m', function ($match) {
  59. // don't add it if the previous line is
  60. // * {
  61. // * :
  62. // * already blank line
  63. if (preg_match('#(\{ *$|^$|^ *//|\:)#', $match[1])) {
  64. return $match[1]."\n".$match[2];
  65. }
  66. return $match[1]."\n\n".$match[2];
  67. }, $new);
  68. // [Structure] A file must always ends with a linefeed character
  69. // FIXME
  70. if ($new != $old) {
  71. $exit = 1;
  72. if ($fix) {
  73. file_put_contents($file->getRealpath(), $new);
  74. }
  75. echo $file->getRelativePathname().PHP_EOL;
  76. }
  77. }
  78. exit($exit);