| Conditions | 28 |
| Paths | 16386 |
| Total Lines | 119 |
| Code Lines | 80 |
| Lines | 95 |
| Ratio | 79.83 % |
| 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 |
||
| 5 | public function run($request) { |
||
| 6 | $args = $request->getVar('args'); |
||
| 7 | $dryRun = $args && in_array('--dry-run', $args); |
||
| 8 | |||
| 9 | View Code Duplication | $log = function($message) { |
|
| 10 | $message = sprintf('[%s] ', date('Y-m-d H:i:s')) . $message; |
||
| 11 | echo $message . PHP_EOL; |
||
| 12 | }; |
||
| 13 | |||
| 14 | if (!Director::is_cli()) { |
||
| 15 | $log('This task must be run under the command line'); |
||
| 16 | return; |
||
| 17 | } |
||
| 18 | |||
| 19 | if ($dryRun) { |
||
| 20 | $log('Running in dry-run mode. No data will be deleted'); |
||
| 21 | } |
||
| 22 | |||
| 23 | $count = 0; |
||
| 24 | |||
| 25 | View Code Duplication | foreach (DNEnvironment::get() as $environment) { |
|
| 26 | $project = $environment->Project(); |
||
| 27 | if (!$project || !$project->exists()) { |
||
| 28 | $log(sprintf( |
||
| 29 | 'Environment (ID %s, Name: %s, Created: %s) is linked to a non-existent project. Deleting', |
||
| 30 | $environment->ID, |
||
| 31 | $environment->Name, |
||
| 32 | $environment->Created |
||
| 33 | )); |
||
| 34 | if (!$dryRun) { |
||
| 35 | $environment->delete(); |
||
| 36 | $environment->destroy(); |
||
| 37 | } |
||
| 38 | $count++; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | View Code Duplication | foreach (DNDeployment::get() as $deployment) { |
|
| 43 | $environment = $deployment->Environment(); |
||
| 44 | if (!$environment || !$environment->exists()) { |
||
| 45 | $log(sprintf( |
||
| 46 | 'Deployment (ID %s, Created: %s) is linked to a non-existent environment. Deleting', |
||
| 47 | $deployment->ID, |
||
| 48 | $deployment->Created |
||
| 49 | )); |
||
| 50 | if (!$dryRun) { |
||
| 51 | $deployment->delete(); |
||
| 52 | $deployment->destroy(); |
||
| 53 | } |
||
| 54 | $count++; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | View Code Duplication | foreach (DNDataTransfer::get() as $transfer) { |
|
| 59 | $environment = $transfer->Environment(); |
||
| 60 | if (!$environment || !$environment->exists()) { |
||
| 61 | $log(sprintf( |
||
| 62 | 'Data transfer (ID %s, Created: %s) is linked to a non-existent environment. Deleting', |
||
| 63 | $transfer->ID, |
||
| 64 | $transfer->Created |
||
| 65 | )); |
||
| 66 | if (!$dryRun) { |
||
| 67 | $transfer->delete(); |
||
| 68 | $transfer->destroy(); |
||
| 69 | } |
||
| 70 | $count++; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | View Code Duplication | foreach (DNDataArchive::get() as $archive) { |
|
| 75 | $environment = $archive->Environment(); |
||
| 76 | if (!$environment || !$environment->exists()) { |
||
| 77 | $log(sprintf( |
||
| 78 | 'Archive (ID %s, Created: %s) is linked to a non-existent environment. Deleting', |
||
| 79 | $archive->ID, |
||
| 80 | $archive->Created |
||
| 81 | )); |
||
| 82 | if (!$dryRun) { |
||
| 83 | $archive->delete(); |
||
| 84 | $archive->destroy(); |
||
| 85 | } |
||
| 86 | $count++; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | View Code Duplication | foreach (DNGitFetch::get() as $fetch) { |
|
| 91 | $project = $fetch->Project(); |
||
| 92 | if (!$project || !$project->exists()) { |
||
| 93 | $log(sprintf( |
||
| 94 | 'Git fetch (ID %s, Created: %s) is linked to a non-existent project. Deleting', |
||
| 95 | $fetch->ID, |
||
| 96 | $fetch->Created |
||
| 97 | )); |
||
| 98 | if (!$dryRun) { |
||
| 99 | $fetch->delete(); |
||
| 100 | $fetch->destroy(); |
||
| 101 | } |
||
| 102 | $count++; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | View Code Duplication | foreach (DNPing::get() as $ping) { |
|
| 107 | $environment = $ping->Environment(); |
||
| 108 | if (!$environment || !$environment->exists()) { |
||
| 109 | $log(sprintf( |
||
| 110 | 'Ping (ID %s, Created: %s) is linked to a non-existent environment. Deleting', |
||
| 111 | $ping->ID, |
||
| 112 | $ping->Created |
||
| 113 | )); |
||
| 114 | if (!$dryRun) { |
||
| 115 | $ping->delete(); |
||
| 116 | $ping->destroy(); |
||
| 117 | } |
||
| 118 | $count++; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | $log(sprintf('Finished. Processed %s records', $count)); |
||
| 123 | } |
||
| 124 | |||
| 126 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.