| Conditions | 10 |
| Paths | 12 |
| Total Lines | 53 |
| 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 |
||
| 71 | public function extract($resource, MessageCatalogue $catalog): void |
||
| 72 | { |
||
| 73 | // Ensure it runs only once. |
||
| 74 | if ($this->hasRun) { |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | $this->hasRun = true; |
||
| 78 | |||
| 79 | $finder = new Finder(); |
||
| 80 | |||
| 81 | foreach ($this->paths as $dir => $settings) { |
||
| 82 | // Normalize namespace. |
||
| 83 | $namespace = rtrim($settings['namespace'], '\\') . '\\'; |
||
| 84 | |||
| 85 | /** @var SplFileInfo $file */ |
||
| 86 | foreach ($finder->files()->name($this->fileNamePattern)->in($dir) as $file) { |
||
| 87 | foreach ($this->ignore as $ignore) { |
||
| 88 | if (fnmatch($ignore, $file->getPathname())) { |
||
| 89 | continue 2; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | // Get file pathinfo and clear dirname. |
||
| 94 | $path = pathinfo($file->getRelativePathname()); |
||
| 95 | if ($path['dirname'] === '.') { |
||
| 96 | $path['dirname'] = ''; |
||
| 97 | } else { |
||
| 98 | $path['dirname'] .= '/'; |
||
| 99 | } |
||
| 100 | |||
| 101 | // Build class name and check if it's a ReadableEnum instance. |
||
| 102 | /** @var ReadableEnum $class */ |
||
| 103 | $class = $namespace . strtr($path['dirname'] . $path['filename'], ['/' => '\\']); |
||
| 104 | if (!is_a($class, ReadableEnum::class, true)) { |
||
| 105 | continue; |
||
| 106 | } |
||
| 107 | |||
| 108 | $readables = $class::readables(); |
||
| 109 | foreach ($readables as $k => $enum) { |
||
| 110 | $enum = (string) $enum; |
||
| 111 | if ('' === $enum) { |
||
| 112 | continue; |
||
| 113 | } |
||
| 114 | |||
| 115 | $catalog->set($enum, $this->prefix . $enum, $this->domain); |
||
| 116 | $metadata = $catalog->getMetadata($enum, $this->domain) ?? []; |
||
| 117 | $normalizedFilename = preg_replace('{[\\\\/]+}', '/', $file->getPathName()); |
||
| 118 | $metadata['sources'][] = $normalizedFilename . ':' . $k; |
||
| 119 | $catalog->setMetadata($enum, $metadata, $this->domain); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 |