findTableAndModelInformationForExtension()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 4
nc 3
nop 1
crap 20
1
<?php
2
3
/**
4
 * ContextSensitiveHelp (CSH) based on smart objects.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Loader;
9
10
use HDNET\Autoloader\Loader;
11
use HDNET\Autoloader\LoaderInterface;
12
use HDNET\Autoloader\Localization\LanguageHandler;
13
use HDNET\Autoloader\Service\SmartObjectInformationService;
14
use HDNET\Autoloader\SmartObjectRegister;
15
use HDNET\Autoloader\Utility\ClassNamingUtility;
16
use HDNET\Autoloader\Utility\ModelUtility;
17
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20
/**
21
 * ContextSensitiveHelp (CSH) based on smart objects.
22
 */
23
class ContextSensitiveHelps implements LoaderInterface
24
{
25
    /**
26
     * Get all the complex data for the loader.
27
     * This return value will be cached and stored in the database
28
     * There is no file monitoring for this cache.
29
     */
30
    public function prepareLoader(Loader $loader, int $type): array
31
    {
32
        if (LoaderInterface::EXT_TABLES !== $type) {
33
            return [];
34
        }
35
        $modelInformation = $this->findTableAndModelInformationForExtension($loader->getExtensionKey());
36
37
        $loaderInformation = [];
38
        foreach ($modelInformation as $information) {
39
            $table = $information['table'];
40
            $path = $this->checkCshValues(
41
                $loader->getExtensionKey(),
42
                $information['table'],
43
                $information['properties']
44
            );
45
            if (null !== $path) {
46
                $loaderInformation[$table] = $path;
47
            }
48
        }
49
50
        return $loaderInformation;
51
    }
52
53
    /**
54
     * Run the loading process for the ext_tables.php file.
55
     */
56
    public function loadExtensionTables(Loader $loader, array $loaderInformation): void
57
    {
58
        foreach ($loaderInformation as $table => $path) {
59
            ExtensionManagementUtility::addLLrefForTCAdescr($table, $path);
60
        }
61
    }
62
63
    /**
64
     * Run the loading process for the ext_localconf.php file.
65
     *
66
     * @internal param \HDNET\Autoloader\Loader $autoLoader
67
     */
68
    public function loadExtensionConfiguration(Loader $loader, array $loaderInformation): void
69
    {
70
    }
71
72
    /**
73
     * Find table and model information for the given extension key.
74
     *
75
     * @param string $extensionKey
76
     *
77
     * @return array
78
     */
79
    protected function findTableAndModelInformationForExtension($extensionKey)
80
    {
81
        $information = [];
82
        $register = SmartObjectRegister::getRegister();
83
        foreach ($register as $class) {
84
            $parts = ClassNamingUtility::explodeObjectModelName($class);
85
            if (GeneralUtility::camelCaseToLowerCaseUnderscored($parts['extensionName']) === $extensionKey && '' === ModelUtility::getTableNameByModelReflectionAnnotation($class)) {
86
                $modelInformation = SmartObjectInformationService::getInstance()
87
                    ->getCustomModelFieldTca($class)
88
                ;
89
90
                $information[] = [
91
                    'table' => ModelUtility::getTableNameByModelName($class),
92
                    'properties' => array_keys($modelInformation),
93
                ];
94
            }
95
        }
96
97
        return $information;
98
    }
99
100
    /**
101
     * Check if the given file is already existing.
102
     *
103
     * @param string $extensionKey
104
     * @param string $table
105
     *
106
     * @return string|null
107
     */
108
    protected function checkCshValues($extensionKey, $table, array $properties)
109
    {
110
        $baseFileName = 'locallang_csh_' . $table;
111
        /** @var LanguageHandler $languageHandler */
112
        $languageHandler = GeneralUtility::makeInstance(LanguageHandler::class);
113
        foreach ($properties as $property) {
114
            $default = '';
115
            $languageHandler->handle($property . '.alttitle', $extensionKey, $default, null, $baseFileName);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
116
        }
117
118
        $checkPath = ['xlf', 'xml'];
119
        foreach ($checkPath as $extension) {
120
            $path = 'EXT:' . $extensionKey . '/Resources/Private/Language/' . $baseFileName . '.' . $extension;
121
            if (is_file(GeneralUtility::getFileAbsFileName($path))) {
122
                return $path;
123
            }
124
        }
125
    }
126
}
127