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