Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 22 | /** |
||
| 23 | * @var string[] |
||
| 24 | */ |
||
| 25 | private array $paths; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string[] |
||
| 29 | */ |
||
| 30 | private array $queries; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param KernelInterface $kernel A KernelInterface instance |
||
| 34 | * @param string $projectDirectory The directory where global query sources can be stored |
||
| 35 | * @param array<string> $paths Additional Twig paths to warm |
||
| 36 | */ |
||
| 37 | 1 | public function __construct(KernelInterface $kernel, string $projectDirectory, array $paths = []) |
|
| 38 | { |
||
| 39 | 1 | $this->kernel = $kernel; |
|
| 40 | 1 | $this->projectDirectory = $projectDirectory; |
|
| 41 | 1 | $this->paths = $paths; |
|
| 42 | 1 | } |
|
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | 1 | public function getIterator(): \Traversable |
|
| 48 | { |
||
| 49 | 1 | if (!isset($this->queries)) { |
|
| 50 | 1 | $this->queries = $this->findQuerySourcesInDirectory($this->projectDirectory . '/query'); |
|
| 51 | 1 | $bundles = $this->kernel->getBundles(); |
|
| 52 | 1 | $bundleQueries = []; |
|
| 53 | 1 | $pathQueries = []; |
|
| 54 | |||
| 55 | 1 | foreach ($bundles as $bundle) { |
|
| 56 | 1 | $name = $bundle->getName(); |
|
| 57 | |||
| 58 | 1 | if ('Bundle' === substr($name, -6)) { |
|
| 59 | 1 | $name = substr($name, 0, -6); |
|
| 60 | } |
||
| 61 | |||
| 62 | 1 | $bundleQueries[] = $this->findQuerySourcesInDirectory($bundle->getPath() . '/Resources/query', $name); |
|
| 63 | 1 | $bundleQueries[] = $this->findQuerySourcesInDirectory($this->projectDirectory . '/bundles/query/' . $bundle->getName()); |
|
| 64 | } |
||
| 65 | |||
| 66 | 1 | foreach ($this->paths as $path => $namespace) { |
|
| 67 | 1 | $pathQueries[] = $this->findQuerySourcesInDirectory($path, $namespace); |
|
| 68 | } |
||
| 69 | |||
| 70 | 1 | $this->queries = \array_merge($this->queries, ...$bundleQueries, ...$pathQueries); |
|
| 71 | } |
||
| 72 | |||
| 73 | 1 | return new \ArrayIterator($this->queries); |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Find query sources in the given directory. |
||
| 78 | * |
||
| 79 | * @param string $dir The directory where to look for query sources |
||
| 80 | * @param string|null $namespace The query source namespace |
||
| 81 | * |
||
| 82 | * @return string[] |
||
| 83 | */ |
||
| 84 | 1 | private function findQuerySourcesInDirectory(string $dir, ?string $namespace = null): array |
|
| 85 | { |
||
| 86 | 1 | if (!\is_dir($dir)) { |
|
| 87 | 1 | return []; |
|
| 88 | } |
||
| 89 | |||
| 90 | $templates = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var SplFileInfo[] $files |
||
| 94 | */ |
||
| 95 | $files = Finder::create()->files()->followLinks()->in($dir); |
||
| 96 | |||
| 97 | foreach ($files as $file) { |
||
| 98 | $templates[] = (null !== $namespace ? '@' . $namespace . '/' : '') . str_replace('\\', '/', $file->getRelativePathname()); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $templates; |
||
| 102 | } |
||
| 104 |