Completed
Push — master ( af310e...ca9412 )
by Nikola
05:51
created

QuerySourcesIterator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 3.75 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 3
loc 80
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

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
2
3
declare(strict_types=1);
4
5
namespace RunOpenCode\Bundle\QueryResourcesLoader\Twig\CacheWarmer;
6
7
use Symfony\Component\Finder\Finder;
8
use Symfony\Component\Finder\SplFileInfo;
9
use Symfony\Component\HttpKernel\KernelInterface;
10
11
/**
12
 * Iterator for all query resources in bundles and in the application Resources/query directory.
13
 *
14
 * @implements \IteratorAggregate<string>
15
 */
16
final class QuerySourcesIterator implements \IteratorAggregate
17
{
18
    private KernelInterface $kernel;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
19
20
    private string $projectDirectory;
21
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
    }
103
}
104