check_cs 3.2 KB

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