Conditions | 7 |
Paths | 12 |
Total Lines | 76 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
9 | public function execute($workdir, $namespace, $composerName, $phpVersion, $mysqlConnection, $timezone) |
||
10 | { |
||
11 | $directory = new \RecursiveDirectoryIterator($workdir); |
||
12 | $filter = new \RecursiveCallbackFilterIterator($directory, function ($current/*, $key, $iterator*/) { |
||
|
|||
13 | // Skip hidden files and directories. |
||
14 | if ($current->getFilename()[0] === '.') { |
||
15 | return false; |
||
16 | } |
||
17 | if ($current->isDir()) { |
||
18 | // Only recurse into intended subdirectories. |
||
19 | return $current->getFilename() !== 'fw'; |
||
20 | } |
||
21 | // else { |
||
22 | // // Only consume files of interest. |
||
23 | // return strpos($current->getFilename(), 'wanted_filename') === 0; |
||
24 | // } |
||
25 | return true; |
||
26 | }); |
||
27 | |||
28 | |||
29 | //Replace composer name: |
||
30 | $contents = file_get_contents($workdir . '/composer.json'); |
||
31 | file_put_contents( |
||
32 | $workdir . '/composer.json', |
||
33 | str_replace('byjg/resttemplate', $composerName, $contents) |
||
34 | ); |
||
35 | |||
36 | // Replace Docker PHP Version |
||
37 | $files = [ 'docker/Dockerfile', 'docker/Dockerfile-dev' ]; |
||
38 | foreach ($files as $file) { |
||
39 | $contents = file_get_contents("$workdir/$file"); |
||
40 | $contents = str_replace('ENV TZ=America/Sao_Paulo', "ENV TZ=${timezone}", $contents); |
||
41 | file_put_contents( |
||
42 | "$workdir/$file", |
||
43 | str_replace('FROM byjg/php:7.2-fpm-nginx', "FROM byjg/php:${phpVersion}-fpm-nginx", $contents) |
||
44 | ); |
||
45 | } |
||
46 | |||
47 | // Replace MySQL Connection |
||
48 | $files = [ 'config/config-dev.php', 'config/config-homolog.php' , 'config/config-live.php', 'config/config-test.php']; |
||
49 | foreach ($files as $file) { |
||
50 | $contents = file_get_contents("$workdir/$file"); |
||
51 | file_put_contents( |
||
52 | "$workdir/$file", |
||
53 | str_replace('mysql://root:password@mysql-container/database', "$mysqlConnection", $contents) |
||
54 | ); |
||
55 | } |
||
56 | |||
57 | // Replace Namespace |
||
58 | $objects = new \RecursiveIteratorIterator($filter); |
||
59 | foreach ($objects as $name => $object) { |
||
60 | $contents = file_get_contents($name); |
||
61 | if (strpos($contents, 'RestTemplate') !== false) { |
||
62 | echo "$name\n"; |
||
63 | |||
64 | // Replace inside Quotes |
||
65 | $contents = preg_replace( |
||
66 | "/([\'\"])RestTemplate(.*?[\'\"])/", |
||
67 | '$1' . str_replace('\\', '\\\\\\\\', $namespace) . '$2', |
||
68 | $contents |
||
69 | ); |
||
70 | |||
71 | // Replace reserved name |
||
72 | $contents = str_replace('RestTemplate', $namespace, $contents); |
||
73 | |||
74 | // Replace reserved name |
||
75 | $contents = str_replace( |
||
76 | 'resttemplate', |
||
77 | str_replace('/', '', $composerName), |
||
78 | $contents |
||
79 | ); |
||
80 | |||
81 | // Save it |
||
82 | file_put_contents( |
||
83 | $name, |
||
84 | $contents |
||
85 | ); |
||
111 |