Passed
Push — main ( c1e5b8...0797d2 )
by Axel
04:05
created

Scanner::setTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\CoreBundle\Composer;
15
16
use Symfony\Component\Finder\Finder;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Finder\Finder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Symfony\Component\Finder\SplFileInfo;
18
use Symfony\Contracts\Translation\TranslatorInterface;
19
use function Symfony\Component\String\s;
20
use const JSON_ERROR_NONE;
21
22
class Scanner
23
{
24
    private array $jsons = [];
25
26
    private array $invalid = [];
27
28
    private TranslatorInterface $translator;
29
30
    /**
31
     * Scans and loads composer.json files.
32
     */
33
    public function scan(array $paths = [], int $depth = 3, Finder $finder = null): void
34
    {
35
        $finder = $finder ?? new Finder();
36
        $finder->files()
37
            ->in($paths)
38
            ->notPath('docs')
39
            ->notPath('vendor')
40
            ->notPath('Resources')
41
            ->ignoreDotFiles(true)
42
            ->ignoreVCS(true)
43
            ->followLinks()
44
            ->depth('<' . $depth)
45
            ->name('composer.json')
46
        ;
47
48
        /** @var SplFileInfo $file */
49
        foreach ($finder as $file) {
50
            $json = $this->decode($file->getRealPath());
51
            if (false !== $json) {
52
                $this->jsons[$json['name']] = $json;
53
            } else {
54
                $this->invalid[] = $file->getRelativePath();
55
            }
56
        }
57
    }
58
59
    /**
60
     * Decodes a json string.
61
     *
62
     * @return bool|mixed
63
     */
64
    public function decode(string $jsonFilePath)
65
    {
66
        $base = str_replace('\\', '/', dirname($jsonFilePath));
67
        $srcPath = dirname(__DIR__, 4) . '/src/';
68
        $base = s($base)->slice(mb_strlen($srcPath))->toString();
69
70
        $json = json_decode(file_get_contents($jsonFilePath), true);
71
        if (JSON_ERROR_NONE === json_last_error()) {
72
            // calculate PSR-4 autoloading path for this namespace
73
            $class = $json['extra']['zikula']['class'];
74
            $ns = s($class)->beforeLast('\\', true)->toString();
75
            if (false === isset($json['autoload']['psr-4'][$ns])) {
76
                return false;
77
            }
78
            $json['autoload']['psr-4'][$ns] = $base;
79
80
            return $json;
81
        }
82
83
        return false;
84
    }
85
86
    public function getExtensionsMetaData(): array
87
    {
88
        $array = [];
89
        foreach ($this->jsons as $json) {
90
            $array[$json['name']] = new MetaData($json);
91
            $array[$json['name']]->setTranslator($this->translator);
92
        }
93
94
        return $array;
95
    }
96
97
    public function getInvalid(): array
98
    {
99
        return $this->invalid;
100
    }
101
102
    public function setTranslator(TranslatorInterface $translator): void
103
    {
104
        $this->translator = $translator;
105
    }
106
}
107