Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 83 lines (67 sloc) 2.564 kb
2d52bf9 @brikou fixed shebash declaration
brikou authored
1 #!/usr/bin/env php
e62804a @brikou added a script to clean up the code and follow CS
brikou authored
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
13 require_once __DIR__.'/autoload.php.dist';
14
15 use Symfony\Component\Finder\Finder;
16
5f8a0f5 @fabpot made some tweaks to the check_cs script
fabpot authored
17 $fix = isset($argv[1]) && 'fix' == $argv[1];
18
e62804a @brikou added a script to clean up the code and follow CS
brikou authored
19 $finder = new Finder();
20 $finder
21 ->files()
22 ->name('*.md')
23 ->name('*.php')
24 ->name('*.php.dist')
25 ->name('*.twig')
26 ->name('*.xml')
27 ->name('*.xml.dist')
28 ->name('*.yml')
5f8a0f5 @fabpot made some tweaks to the check_cs script
fabpot authored
29 ->in(__DIR__.'/src', __DIR__.'/tests')
e62804a @brikou added a script to clean up the code and follow CS
brikou authored
30 ->notName(basename(__FILE__))
31 ->exclude('.git')
32 ->exclude('vendor')
33 ;
34
5f8a0f5 @fabpot made some tweaks to the check_cs script
fabpot authored
35 $exit = 0;
36 foreach ($finder as $file) {
e62804a @brikou added a script to clean up the code and follow CS
brikou authored
37 // These files are skipped because tests would break
38 if (in_array($file->getRelativePathname(), array(
39 'tests/Symfony/Tests/Component/ClassLoader/ClassCollectionLoaderTest.php',
40 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/container9.php',
41 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/includes/foo.php',
42 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php',
43 'tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/services9.yml',
44 'tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php',
45 'tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher2.php',
46 'tests/Symfony/Tests/Component/Yaml/Fixtures/sfTests.yml',
47 ))) {
48 continue;
49 }
50
51 $old = file_get_contents($file->getRealpath());
52
53 $new = $old;
54
55 // [Structure] Never use short tags (<?);
56 $new = str_replace('<? ', '<?php ', $new);
57
58 // [Structure] Indentation is done by steps of four spaces (tabs are never allowed);
59 $new = preg_replace_callback('/^( *)(\t+)/m', function ($matches) use ($new) {
5f8a0f5 @fabpot made some tweaks to the check_cs script
fabpot authored
60 return $matches[1].str_repeat(' ', strlen($matches[2]));
e62804a @brikou added a script to clean up the code and follow CS
brikou authored
61 }, $new);
62
63 // [Structure] Use the linefeed character (0x0A) to end lines;
64 $new = str_replace("\r\n", "\n", $new);
65
66 // [Structure] Don't add trailing spaces at the end of lines;
67 $new = preg_replace('/[ \t]*$/m', '', $new);
68
69 // [Structure] Add a blank line before return statements;
70 $new = preg_replace('/([^ {|\n]$)(\n return .+?$\n \}$)/m', '$1'."\n".'$2', $new);
71
72 if ($new != $old) {
5f8a0f5 @fabpot made some tweaks to the check_cs script
fabpot authored
73 $exit = 1;
74 if ($fix) {
75 file_put_contents($file->getRealpath(), $new);
76 }
77
78 echo $file->getRelativePathname().PHP_EOL;
e62804a @brikou added a script to clean up the code and follow CS
brikou authored
79 }
80 }
5f8a0f5 @fabpot made some tweaks to the check_cs script
fabpot authored
81
82 exit($exit);
Something went wrong with that request. Please try again.