1
|
|
|
<?php |
|
|
|
|
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
$file_patterns = [ |
5
|
|
|
'app/bootstrap.php', |
6
|
|
|
'migrations/*.php', |
7
|
|
|
'src/**/*.php', |
8
|
|
|
'tests/**/*.php', |
9
|
|
|
]; |
10
|
|
|
|
11
|
|
View Code Duplication |
if ( ! function_exists('glob_recursive')) |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
// Does not support flag GLOB_BRACE |
14
|
|
|
|
15
|
|
|
function glob_recursive($pattern, $flags = 0) |
16
|
|
|
{ |
17
|
|
|
$files = glob($pattern, $flags); |
18
|
|
|
|
19
|
|
|
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) |
20
|
|
|
{ |
21
|
|
|
$files = array_merge($files, glob_recursive($dir . '/' . basename($pattern), $flags)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return $files; |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
function get_text_to_replace($tokens) |
29
|
|
|
{ |
30
|
|
|
$output = ''; |
31
|
|
|
|
32
|
|
|
// Tokens have the follow structure if arrays: |
33
|
|
|
// [0] => token type constant |
34
|
|
|
// [1] => raw sytax parsed to that token |
35
|
|
|
// [2] => line number |
|
|
|
|
36
|
|
|
foreach($tokens as $token) |
37
|
|
|
{ |
38
|
|
|
// Since we only care about opening docblocks, |
39
|
|
|
// bail out when we get to the namespace token |
40
|
|
|
if (is_array($token) && $token[0] === T_NAMESPACE) |
41
|
|
|
{ |
42
|
|
|
break; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (is_array($token)) |
46
|
|
|
{ |
47
|
|
|
$token = $token[1]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$output .= $token; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $output; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
function get_tokens($source) |
57
|
|
|
{ |
58
|
|
|
return token_get_all($source); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
function replace_files(array $files, $template) |
62
|
|
|
{ |
63
|
|
|
foreach ($files as $file) |
64
|
|
|
{ |
65
|
|
|
$source = file_get_contents($file); |
66
|
|
|
|
67
|
|
|
if (stripos($source, 'namespace') === FALSE) |
68
|
|
|
{ |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$tokens = get_tokens($source); |
73
|
|
|
$text_to_replace = get_text_to_replace($tokens); |
74
|
|
|
|
75
|
|
|
$header = file_get_contents(__DIR__ . $template); |
76
|
|
|
$new_text = "<?php declare(strict_types=1);\n{$header}"; |
77
|
|
|
|
78
|
|
|
$new_source = str_replace($text_to_replace, $new_text, $source); |
79
|
|
|
file_put_contents($file, $new_source); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
foreach ($file_patterns as $glob) |
84
|
|
|
{ |
85
|
|
|
$files = glob_recursive($glob); |
86
|
|
|
replace_files($files, '/header_comment.txt'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
echo "Successfully updated headers \n"; |
|
|
|
|
90
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.