check_cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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(array(__DIR__.'/src', __DIR__.'/tests'))
  26. ->notName(basename(__FILE__))
  27. ->exclude('.git')
  28. ->exclude('vendor')
  29. ;
  30. $count = 0;
  31. foreach ($finder as $file) {
  32. /* @var $file Symfony\Component\Finder\SplFileInfo */
  33. // These files are skipped because tests would break
  34. foreach(array(
  35. 'tests/Symfony/Tests/Component/ClassLoader/ClassCollectionLoaderTest.php',
  36. 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/container9.php',
  37. 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/includes/foo.php',
  38. 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php',
  39. 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/services9.yml',
  40. 'tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php',
  41. 'tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher2.php',
  42. 'tests/Symfony/Tests/Component/Yaml/Fixtures/sfTests.yml',
  43. ) as $skippedFile) {
  44. if ($skippedFile === substr($file->getRealPath(), strlen($skippedFile) * -1)) {
  45. continue(2);
  46. }
  47. }
  48. $old = file_get_contents($file->getRealpath());
  49. $new = $old;
  50. // [Structure] Never use short tags (<?)
  51. $new = str_replace('<? ', '<?php ', $new);
  52. // [Structure] Indentation is done by steps of four spaces (tabs are never allowed)
  53. $new = preg_replace_callback('/^( *)(\t+)/m', function ($matches) use ($new) {
  54. return $matches[1].str_repeat(' ', strlen($matches[2]));
  55. }, $new);
  56. // [Structure] Use the linefeed character (0x0A) to end lines
  57. $new = str_replace("\r\n", "\n", $new);
  58. // [Structure] Don't add trailing spaces at the end of lines
  59. $new = preg_replace('/[ \t]*$/m', '', $new);
  60. // [Structure] Add a blank line before return statements
  61. $new = preg_replace_callback('/(^.*$)\n(^ +return)/m', function ($match) {
  62. // don't add it if the previous line is ...
  63. if (
  64. preg_match('/\{$/m', $match[1]) || // ... ending with an opening brace
  65. preg_match('/\:$/m', $match[1]) || // ... ending with a colon (e.g. a case statement)
  66. preg_match('%^ *//%m', $match[1]) || // ... an inline comment
  67. preg_match('/^$/m', $match[1]) // ... already blank
  68. ) {
  69. return $match[1]."\n".$match[2];
  70. }
  71. return $match[1]."\n\n".$match[2];
  72. }, $new);
  73. // [Structure] A file must always ends with a linefeed character
  74. if (strlen($new) && "\n" != substr($new, -1)) {
  75. $new .= "\n";
  76. }
  77. if ($new != $old) {
  78. $count++;
  79. if ($fix) {
  80. file_put_contents($file->getRealpath(), $new);
  81. }
  82. printf('%4d) %s'.PHP_EOL, $count, $file->getRelativePathname());
  83. }
  84. }
  85. exit($count ? 1 : 0);