| Conditions | 16 |
| Paths | 2610 |
| Total Lines | 158 |
| Code Lines | 86 |
| 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 |
||
| 38 | public function handle() |
||
| 39 | { |
||
| 40 | // Check repository folder permissions. |
||
| 41 | $this->checkRepositoryPermissions(); |
||
| 42 | |||
| 43 | // Create repository folder if it's necessary. |
||
| 44 | $this->createFolder(config('repository-generator.repository_directory')); |
||
| 45 | |||
| 46 | // Check interface folder permissions. |
||
| 47 | $this->checkInterfacePermissions(); |
||
| 48 | |||
| 49 | // Create interface folder if it's necessary. |
||
| 50 | $this->createFolder(config('repository-generator.interface_directory')); |
||
| 51 | |||
| 52 | // Get all model file names. |
||
| 53 | $models = $this->getModels(); |
||
| 54 | |||
| 55 | // Check model files. |
||
| 56 | if (count($models) === 0) { |
||
| 57 | $this->noModelsMessage(); |
||
| 58 | } |
||
| 59 | |||
| 60 | // Get existing repository file names. |
||
| 61 | $existingRepositoryFiles = glob($this->repositoryPath('*.php')); |
||
| 62 | |||
| 63 | // Remove main repository file name from array |
||
| 64 | $existingRepositoryFiles = array_diff( |
||
| 65 | $existingRepositoryFiles, |
||
| 66 | [$this->repositoryPath(config('repository-generator.main_repository_file'))] |
||
| 67 | ); |
||
| 68 | |||
| 69 | // Ask for overriding, If there are files in repository directory. |
||
| 70 | if (count($existingRepositoryFiles) > 0) { |
||
| 71 | if ($this->confirm('Do you want to overwrite the existing files? (Yes/No):')) { |
||
| 72 | $this->override = true; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | // Get existing interface file names. |
||
| 77 | $existingInterfaceFiles = glob($this->interfacePath('*.php')); |
||
| 78 | |||
| 79 | // Remove main interface file from array |
||
| 80 | $existingInterfaceFiles = array_diff( |
||
| 81 | $existingInterfaceFiles, |
||
| 82 | [$this->repositoryPath(config('repository-generator.main_interface_file'))] |
||
| 83 | ); |
||
| 84 | |||
| 85 | // Ask for overriding, If there are files in interface repository. |
||
| 86 | // It could be already asked while checking repository files. |
||
| 87 | // If so, we won't show this confirm question again. |
||
| 88 | if (count($existingInterfaceFiles) > 0 && ! $this->override) { |
||
| 89 | if ($this->confirm('Do you want to overwrite the existing files? (Yes/No):')) { |
||
| 90 | $this->override = true; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | // Get stub file templates. |
||
| 95 | $repositoryStub = $this->getStub('Repository'); |
||
| 96 | $interfaceStub = $this->getStub('Interface'); |
||
| 97 | |||
| 98 | // Repository stub values those should be changed by command. |
||
| 99 | $repositoryStubValues = [ |
||
| 100 | '__USE_STATEMENT_FOR_REPOSITORY__', |
||
| 101 | '__REPOSITORY_NAMESPACE__', |
||
| 102 | '__MAIN_REPOSITORY__', |
||
| 103 | '__REPOSITORY__', |
||
| 104 | '__MODEL_NAMESPACE_', |
||
| 105 | '__MODEL__', |
||
| 106 | '__INTERFACE_NAMESPACE_', |
||
| 107 | '__INTERFACE__', |
||
| 108 | ]; |
||
| 109 | |||
| 110 | // Interface stub values those should be changed by command. |
||
| 111 | $interfaceStubValues = [ |
||
| 112 | '__USE_STATEMENT_FOR_INTERFACE__', |
||
| 113 | '__INTERFACE_NAMESPACE__', |
||
| 114 | '__MAIN_INTERFACE__', |
||
| 115 | '__INTERFACE__', |
||
| 116 | ]; |
||
| 117 | |||
| 118 | foreach ($models as $model) { |
||
| 119 | |||
| 120 | // Add suffixes |
||
| 121 | $repository = suffix($model, 'Repository'); |
||
| 122 | $interface = suffix($model, 'RepositoryInterface'); |
||
| 123 | |||
| 124 | // Current repository file name |
||
| 125 | $repositoryFile = $this->repositoryPath($repository.'.php'); |
||
| 126 | |||
| 127 | // Check main repository file's path to add use |
||
| 128 | $useStatementForRepository = false; |
||
| 129 | if (dirname($repositoryFile) !== dirname(config('repository-generator.main_repository_file')) |
||
| 130 | ) { |
||
| 131 | $mainRepository = config('repository-generator.main_repository_class'); |
||
| 132 | $useStatementForRepository = 'use '.$mainRepository.';'; |
||
| 133 | } |
||
| 134 | |||
| 135 | // Fillable repository values for generating real files |
||
| 136 | $repositoryValues = [ |
||
| 137 | $useStatementForRepository ? $useStatementForRepository : '', |
||
| 138 | config('repository-generator.repository_namespace'), |
||
| 139 | str_replace('.php', '', config('repository-generator.main_repository_file')), |
||
| 140 | $repository, |
||
| 141 | config('repository-generator.model_namespace'), |
||
| 142 | $model, |
||
| 143 | config('repository-generator.interface_namespace'), |
||
| 144 | $interface, |
||
| 145 | ]; |
||
| 146 | |||
| 147 | // Generate body of the repository file |
||
| 148 | $repositoryContent = str_replace( |
||
| 149 | $repositoryStubValues, |
||
| 150 | $repositoryValues, |
||
| 151 | $repositoryStub); |
||
| 152 | |||
| 153 | if (in_array($repositoryFile, $existingRepositoryFiles)) { |
||
| 154 | if ($this->override) { |
||
| 155 | $this->writeFile($repositoryFile, $repositoryContent); |
||
| 156 | $this->info('Overridden repository file: '.$repository); |
||
| 157 | } |
||
| 158 | } else { |
||
| 159 | $this->writeFile($repositoryFile, $repositoryContent); |
||
| 160 | $this->info('Created repository file: '.$repository); |
||
| 161 | } |
||
| 162 | |||
| 163 | // Current interface file name |
||
| 164 | $interfaceFile = $this->interfacePath($interface.'.php'); |
||
| 165 | |||
| 166 | // Check main repository file's path to add use |
||
| 167 | $useStatementForInterface = false; |
||
| 168 | if (dirname($interfaceFile) !== dirname(config('repository-generator.main_interface_file')) |
||
| 169 | ) { |
||
| 170 | $mainInterface = config('repository-generator.main_interface_class'); |
||
| 171 | $useStatementForInterface = 'use '.$mainInterface.';'; |
||
| 172 | } |
||
| 173 | |||
| 174 | // Fillable interface values for generating real files |
||
| 175 | $interfaceValues = [ |
||
| 176 | $useStatementForInterface ? $useStatementForInterface : '', |
||
| 177 | config('repository-generator.interface_namespace'), |
||
| 178 | str_replace('.php', '', config('repository-generator.main_interface_file')), |
||
| 179 | $interface, |
||
| 180 | ]; |
||
| 181 | |||
| 182 | // Generate body of the interface file |
||
| 183 | $interfaceContent = str_replace( |
||
| 184 | $interfaceStubValues, |
||
| 185 | $interfaceValues, |
||
| 186 | $interfaceStub); |
||
| 187 | |||
| 188 | if (in_array($interfaceFile, $existingInterfaceFiles)) { |
||
| 189 | if ($this->override) { |
||
| 190 | $this->writeFile($interfaceFile, $interfaceContent); |
||
| 191 | $this->info('Overridden interface file: '.$interface); |
||
| 192 | } |
||
| 193 | } else { |
||
| 194 | $this->writeFile($interfaceFile, $interfaceContent); |
||
| 195 | $this->info('Created interface file: '.$interface); |
||
| 196 | } |
||
| 348 |