| Total Complexity | 42 |
| Total Lines | 374 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Generator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Generator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | abstract class Generator |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * The filesystem instance. |
||
| 18 | * |
||
| 19 | * @var \Illuminate\Filesystem\Filesystem |
||
| 20 | */ |
||
| 21 | protected $filesystem; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The array of options. |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $options; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The shortname of stub. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $stub; |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * The shortname of domain. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $domain; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The base path for domain. |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $domainPath; |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * Create new instance of this class. |
||
| 55 | * |
||
| 56 | * @param array $options |
||
| 57 | */ |
||
| 58 | public function __construct(array $options = [],string $domain = null, string $domainPath = null) |
||
| 59 | { |
||
| 60 | $this->filesystem = new Filesystem; |
||
| 61 | $this->options = $options; |
||
| 62 | $this->domain = $domain; |
||
| 63 | $this->domainPath = $domainPath; |
||
| 64 | } |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Get the filesystem instance. |
||
| 69 | * |
||
| 70 | * @return \Illuminate\Filesystem\Filesystem |
||
| 71 | */ |
||
| 72 | public function getFilesystem() |
||
| 73 | { |
||
| 74 | return $this->filesystem; |
||
| 75 | } |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * Set the filesystem instance. |
||
| 80 | * |
||
| 81 | * @param \Illuminate\Filesystem\Filesystem $filesystem |
||
| 82 | * |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | public function setFilesystem(Filesystem $filesystem) |
||
| 86 | { |
||
| 87 | $this->filesystem = $filesystem; |
||
| 88 | |||
| 89 | return $this; |
||
| 90 | } |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Get stub template for generated file. |
||
| 95 | * |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function getStub() |
||
| 99 | { |
||
| 100 | $path = config('domains.paths.repo-generator.stubsOverridePath', __DIR__); |
||
| 101 | |||
| 102 | if(!file_exists($path . '/Stubs/' . $this->stub . '.stub')){ |
||
| 103 | $path = __DIR__; |
||
| 104 | } |
||
| 105 | |||
| 106 | return (new Stub($path . '/Stubs/' . $this->stub . '.stub', $this->getReplacements()))->render(); |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * Get template replacements. |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function getReplacements() |
||
| 116 | { |
||
| 117 | return [ |
||
| 118 | 'class' => $this->getClass(), |
||
| 119 | 'namespace' => $this->getNamespace(), |
||
| 120 | 'root_namespace' => $this->getRootNamespace() |
||
| 121 | ]; |
||
| 122 | } |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Get base path of destination file. |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getBasePath() |
||
| 131 | { |
||
| 132 | return base_path(); |
||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * Get destination path for generated file. |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function getPath() |
||
| 144 | } |
||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * Get name input. |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function getName() |
||
| 153 | { |
||
| 154 | $name = $this->name; |
||
|
|
|||
| 155 | if (Str::contains($this->name, '\\')) { |
||
| 156 | $name = str_replace('\\', '/', $this->name); |
||
| 157 | } |
||
| 158 | if (Str::contains($this->name, '/')) { |
||
| 159 | $name = str_replace('/', '/', $this->name); |
||
| 160 | } |
||
| 161 | |||
| 162 | return Str::studly(str_replace(' ', '/', ucwords(str_replace('/', ' ', $name)))); |
||
| 163 | } |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * Get application namespace |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getAppNamespace() |
||
| 172 | { |
||
| 173 | return 'Domains\\'.$this->domain.'\\'; |
||
| 174 | } |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * Get class name. |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function getClass() |
||
| 183 | { |
||
| 184 | return Str::studly(class_basename($this->getName())); |
||
| 185 | } |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * Get paths of namespace. |
||
| 190 | * |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function getSegments() |
||
| 194 | { |
||
| 195 | return explode('/', $this->getName()); |
||
| 196 | } |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * Get root namespace. |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function getRootNamespace() |
||
| 205 | { |
||
| 206 | return $this->getAppNamespace(); |
||
| 207 | } |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * Get class-specific output paths. |
||
| 212 | * |
||
| 213 | * @param $class |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getConfigGeneratorClassPath($class, $directoryPath = false) |
||
| 218 | { |
||
| 219 | switch ($class) { |
||
| 220 | case ('models' === $class): |
||
| 221 | $path = config('domains.paths.repo-generator.paths.models', 'Entities'); |
||
| 222 | break; |
||
| 223 | case ('relations' === $class): |
||
| 224 | $path = config('domains.paths.repo-generator.paths.relations', 'Entities/Traits/Relations'); |
||
| 225 | break; |
||
| 226 | case ('repositories' === $class): |
||
| 227 | $path = config('domains.paths.repo-generator.paths.repositories', 'Repositories'); |
||
| 228 | break; |
||
| 229 | case ('interfaces' === $class): |
||
| 230 | $path = config('domains.paths.repo-generator.paths.interfaces', 'Repositories'); |
||
| 231 | break; |
||
| 232 | case ('presenters' === $class): |
||
| 233 | $path = config('domains.paths.repo-generator.paths.presenters', 'Presenters'); |
||
| 234 | break; |
||
| 235 | case ('transformers' === $class): |
||
| 236 | $path = config('domains.paths.repo-generator.paths.transformers', 'Transformers'); |
||
| 237 | break; |
||
| 238 | case ('validators' === $class): |
||
| 239 | $path = config('domains.paths.repo-generator.paths.validators', 'Validators'); |
||
| 240 | break; |
||
| 241 | case ('controllers' === $class): |
||
| 242 | $path = config('domains.paths.repo-generator.paths.controllers', 'Http\Controllers'); |
||
| 243 | break; |
||
| 244 | case ('provider' === $class): |
||
| 245 | $path = config('domains.paths.repo-generator.paths.provider', 'RepositoryServiceProvider'); |
||
| 246 | break; |
||
| 247 | case ('criteria' === $class): |
||
| 248 | $path = config('domains.paths.repo-generator.paths.criteria', 'Criteria'); |
||
| 249 | break; |
||
| 250 | default: |
||
| 251 | $path = ''; |
||
| 252 | } |
||
| 253 | |||
| 254 | if ($directoryPath) { |
||
| 255 | $path = str_replace('\\', '/', $path); |
||
| 256 | } else { |
||
| 257 | $path = str_replace('/', '\\', $path); |
||
| 258 | } |
||
| 259 | |||
| 260 | |||
| 261 | return $path; |
||
| 262 | } |
||
| 263 | |||
| 264 | |||
| 265 | abstract public function getPathConfigNode(); |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * Get class namespace. |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getNamespace() |
||
| 274 | { |
||
| 275 | $segments = $this->getSegments(); |
||
| 276 | array_pop($segments); |
||
| 277 | $rootNamespace = $this->getRootNamespace(); |
||
| 278 | if ($rootNamespace == false) { |
||
| 279 | return null; |
||
| 280 | } |
||
| 281 | |||
| 282 | return 'namespace ' . rtrim($rootNamespace . '\\' . implode('\\', $segments), '\\') . ';'; |
||
| 283 | } |
||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * Setup some hook. |
||
| 288 | * |
||
| 289 | * @return void |
||
| 290 | */ |
||
| 291 | public function setUp() |
||
| 292 | { |
||
| 293 | // |
||
| 294 | } |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * Run the generator. |
||
| 299 | * |
||
| 300 | * @return int |
||
| 301 | * @throws FileAlreadyExistsException |
||
| 302 | */ |
||
| 303 | public function run() |
||
| 304 | { |
||
| 305 | $this->setUp(); |
||
| 306 | if ($this->filesystem->exists($path = $this->getPath()) && !$this->force) { |
||
| 307 | throw new FileAlreadyExistsException($path); |
||
| 308 | } |
||
| 309 | if (!$this->filesystem->isDirectory($dir = dirname($path))) { |
||
| 310 | $this->filesystem->makeDirectory($dir, 0777, true, true); |
||
| 311 | } |
||
| 312 | |||
| 313 | return $this->filesystem->put($path, $this->getStub()); |
||
| 314 | } |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Get options. |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function getOptions() |
||
| 325 | } |
||
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * Determinte whether the given key exist in options array. |
||
| 330 | * |
||
| 331 | * @param string $key |
||
| 332 | * |
||
| 333 | * @return boolean |
||
| 334 | */ |
||
| 335 | public function hasOption($key) |
||
| 336 | { |
||
| 337 | return array_key_exists($key, $this->options); |
||
| 338 | } |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * Get value from options by given key. |
||
| 343 | * |
||
| 344 | * @param string $key |
||
| 345 | * @param string|null $default |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function getOption($key, $default = null) |
||
| 350 | { |
||
| 351 | if (!$this->hasOption($key)) { |
||
| 352 | return $default; |
||
| 353 | } |
||
| 354 | |||
| 355 | return $this->options[$key] ?: $default; |
||
| 356 | } |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * Helper method for "getOption". |
||
| 361 | * |
||
| 362 | * @param string $key |
||
| 363 | * @param string|null $default |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function option($key, $default = null) |
||
| 368 | { |
||
| 369 | return $this->getOption($key, $default); |
||
| 370 | } |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Handle call to __get method. |
||
| 375 | * |
||
| 376 | * @param string $key |
||
| 377 | * |
||
| 378 | * @return string|mixed |
||
| 379 | */ |
||
| 380 | public function __get($key) |
||
| 387 | } |
||
| 388 | } |