| Conditions | 28 |
| Paths | 16386 |
| Total Lines | 118 |
| Code Lines | 79 |
| Lines | 94 |
| Ratio | 79.66 % |
| 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, Created: %s) is linked to a non-existent project. Deleting', |
||
| 30 | $environment->ID, |
||
| 31 | $environment->Created |
||
| 32 | )); |
||
| 33 | if (!$dryRun) { |
||
| 34 | $environment->delete(); |
||
| 35 | $environment->destroy(); |
||
| 36 | } |
||
| 37 | $count++; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | View Code Duplication | foreach (DNDeployment::get() as $deployment) { |
|
| 42 | $environment = $deployment->Environment(); |
||
| 43 | if (!$environment || !$environment->exists()) { |
||
| 44 | $log(sprintf( |
||
| 45 | 'Deployment (ID %s, Created: %s) is linked to a non-existent environment. Deleting', |
||
| 46 | $deployment->ID, |
||
| 47 | $deployment->Created |
||
| 48 | )); |
||
| 49 | if (!$dryRun) { |
||
| 50 | $deployment->delete(); |
||
| 51 | $deployment->destroy(); |
||
| 52 | } |
||
| 53 | $count++; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | View Code Duplication | foreach (DNDataTransfer::get() as $transfer) { |
|
| 58 | $environment = $transfer->Environment(); |
||
| 59 | if (!$environment || !$environment->exists()) { |
||
| 60 | $log(sprintf( |
||
| 61 | 'Data transfer (ID %s, Created: %s) is linked to a non-existent environment. Deleting', |
||
| 62 | $transfer->ID, |
||
| 63 | $transfer->Created |
||
| 64 | )); |
||
| 65 | if (!$dryRun) { |
||
| 66 | $transfer->delete(); |
||
| 67 | $transfer->destroy(); |
||
| 68 | } |
||
| 69 | $count++; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | View Code Duplication | foreach (DNDataArchive::get() as $archive) { |
|
| 74 | $environment = $archive->Environment(); |
||
| 75 | if (!$environment || !$environment->exists()) { |
||
| 76 | $log(sprintf( |
||
| 77 | 'Archive (ID %s, Created: %s) is linked to a non-existent environment. Deleting', |
||
| 78 | $archive->ID, |
||
| 79 | $archive->Created |
||
| 80 | )); |
||
| 81 | if (!$dryRun) { |
||
| 82 | $archive->delete(); |
||
| 83 | $archive->destroy(); |
||
| 84 | } |
||
| 85 | $count++; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | View Code Duplication | foreach (DNGitFetch::get() as $fetch) { |
|
| 90 | $project = $fetch->Project(); |
||
| 91 | if (!$project || !$project->exists()) { |
||
| 92 | $log(sprintf( |
||
| 93 | 'Git fetch (ID %s, Created: %s) is linked to a non-existent project. Deleting', |
||
| 94 | $fetch->ID, |
||
| 95 | $fetch->Created |
||
| 96 | )); |
||
| 97 | if (!$dryRun) { |
||
| 98 | $fetch->delete(); |
||
| 99 | $fetch->destroy(); |
||
| 100 | } |
||
| 101 | $count++; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | View Code Duplication | foreach (DNPing::get() as $ping) { |
|
| 106 | $environment = $ping->Environment(); |
||
| 107 | if (!$environment || !$environment->exists()) { |
||
| 108 | $log(sprintf( |
||
| 109 | 'Ping (ID %s, Created: %s) is linked to a non-existent environment. Deleting', |
||
| 110 | $ping->ID, |
||
| 111 | $ping->Created |
||
| 112 | )); |
||
| 113 | if (!$dryRun) { |
||
| 114 | $ping->delete(); |
||
| 115 | $ping->destroy(); |
||
| 116 | } |
||
| 117 | $count++; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | $log(sprintf('Finished. Processed %s records', $count)); |
||
| 122 | } |
||
| 123 | |||
| 125 |
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.