Skip to content
Newer
Older
100755 96 lines (78 sloc) 2.84 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
8d262bf @fabpot made some enhancement to check_cs
fabpot authored
55 // [Structure] Never use short tags (<?)
e62804a @brikou added a script to clean up the code and follow CS
brikou authored
56 $new = str_replace('<? ', '<?php ', $new);
57
8d262bf @fabpot made some enhancement to check_cs
fabpot authored
58 // [Structure] Indentation is done by steps of four spaces (tabs are never allowed)
e62804a @brikou added a script to clean up the code and follow CS
brikou authored
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
8d262bf @fabpot made some enhancement to check_cs
fabpot authored
63 // [Structure] Use the linefeed character (0x0A) to end lines
e62804a @brikou added a script to clean up the code and follow CS
brikou authored
64 $new = str_replace("\r\n", "\n", $new);
65
8d262bf @fabpot made some enhancement to check_cs
fabpot authored
66 // [Structure] Don't add trailing spaces at the end of lines
e62804a @brikou added a script to clean up the code and follow CS
brikou authored
67 $new = preg_replace('/[ \t]*$/m', '', $new);
68
8d262bf @fabpot made some enhancement to check_cs
fabpot authored
69 // [Structure] Add a blank line before return statements
70 $new = preg_replace_callback('/(^.*$)\n(^ +return)/m', function ($match) {
71 // don't add it if the previous line is
72 // * {
73 // * :
74 // * already blank line
75 if (preg_match('#(\{ *$|^$|^ *//|\:)#', $match[1])) {
76 return $match[1]."\n".$match[2];
77 }
78
79 return $match[1]."\n\n".$match[2];
80 }, $new);
81
82 // [Structure] A file must always ends with a linefeed character
83 // FIXME
e62804a @brikou added a script to clean up the code and follow CS
brikou authored
84
85 if ($new != $old) {
5f8a0f5 @fabpot made some tweaks to the check_cs script
fabpot authored
86 $exit = 1;
87 if ($fix) {
88 file_put_contents($file->getRealpath(), $new);
89 }
90
91 echo $file->getRelativePathname().PHP_EOL;
e62804a @brikou added a script to clean up the code and follow CS
brikou authored
92 }
93 }
5f8a0f5 @fabpot made some tweaks to the check_cs script
fabpot authored
94
95 exit($exit);
Something went wrong with that request. Please try again.