Passed
Push — master ( ce34db...aabc2a )
by
unknown
13:08
created

ComposerDeficitDetector   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 29
c 1
b 0
f 0
dl 0
loc 63
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidExtensionKey() 0 4 2
A getExtensionsWithComposerDeficit() 0 21 5
B checkExtensionComposerDeficit() 0 22 7
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Core\Package;
19
20
use Symfony\Component\Finder\Finder;
21
use TYPO3\CMS\Core\Core\Environment;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
/**
25
 * Detects extensions with composer deficits, e.g. missing
26
 * composer.json file or missing extension-key property.
27
 */
28
class ComposerDeficitDetector
29
{
30
    public const EXTENSION_COMPOSER_MANIFEST_VALID = 0;
31
    public const EXTENSION_COMPOSER_MANIFEST_MISSING = 1;
32
    public const EXTENSION_KEY_MISSING = 2;
33
34
    /**
35
     * Get all extensions with composer deficit
36
     */
37
    public function getExtensionsWithComposerDeficit(): array
38
    {
39
        $finder = Finder::create()->directories()->depth(0)->in(Environment::getExtensionsPath());
40
        $extensionsWithDeficit = [];
41
42
        if ($finder->hasResults()) {
43
            foreach ($finder as $extensionFolder) {
44
                $extensionKey = $extensionFolder->getFilename();
45
                try {
46
                    $extensionComposerDeficit = $this->checkExtensionComposerDeficit($extensionKey);
47
                } catch (\InvalidArgumentException $e) {
48
                    // Skip invalid extensions
49
                    continue;
50
                }
51
                if ($extensionComposerDeficit !== self::EXTENSION_COMPOSER_MANIFEST_VALID) {
52
                    $extensionsWithDeficit[$extensionKey] = $extensionComposerDeficit;
53
                }
54
            }
55
        }
56
57
        return $extensionsWithDeficit;
58
    }
59
60
    /**
61
     * Check an extension key for composer deficits like invalid or missing composer.json
62
     */
63
    public function checkExtensionComposerDeficit(string $extensionKey): int
64
    {
65
        if (!$this->isValidExtensionKey($extensionKey)) {
66
            throw new \InvalidArgumentException('Extension key ' . $extensionKey . ' is not valid.', 1619446378);
67
        }
68
69
        $composerManifestPath = Environment::getExtensionsPath() . '/' . $extensionKey . '/composer.json';
70
71
        if (!file_exists($composerManifestPath) || !($composerManifest = file_get_contents($composerManifestPath))) {
72
            return self::EXTENSION_COMPOSER_MANIFEST_MISSING;
73
        }
74
75
        $composerManifest = json_decode($composerManifest, true) ?? [];
76
77
        if (!is_array($composerManifest) || $composerManifest === []) {
78
            // Treat empty or invalid composer.json as missing
79
            return self::EXTENSION_COMPOSER_MANIFEST_MISSING;
80
        }
81
82
        return empty($composerManifest['extra']['typo3/cms']['extension-key'])
83
            ? self::EXTENSION_KEY_MISSING
84
            : self::EXTENSION_COMPOSER_MANIFEST_VALID;
85
    }
86
87
    protected function isValidExtensionKey(string $extensionKey): bool
88
    {
89
        return preg_match('/^[0-9a-z._\-]+$/i', $extensionKey)
90
            && GeneralUtility::isAllowedAbsPath(Environment::getExtensionsPath() . '/' . $extensionKey);
91
    }
92
}
93