Completed
Push — master ( 959e79...e6dd1a )
by
unknown
12:24
created

ConstraintsViewHelper::getVersionString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 13
rs 10
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\Extensionmanager\ViewHelpers;
19
20
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
21
use TYPO3\CMS\Extensionmanager\Domain\Model\Dependency;
22
use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
23
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
24
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
25
26
/**
27
 * Returns the grouped constraints of an extension
28
 *
29
 * @internal
30
 */
31
class ConstraintsViewHelper extends AbstractViewHelper
32
{
33
    public function initializeArguments(): void
34
    {
35
        $this->registerArgument('extension', Extension::class, 'extension to process', true);
36
    }
37
38
    public static function renderStatic(
39
        array $arguments,
40
        \Closure $renderChildrenClosure,
41
        RenderingContextInterface $renderingContext
42
    ): array {
43
        $groupedConstraints = [];
44
45
        foreach ($arguments['extension']->getDependencies() as $dependency) {
46
            $groupedConstraints[$dependency->getType()][self::getTransformedIdentifier($dependency->getIdentifier())] = [
47
                'version' => self::getVersionString($dependency->getLowestVersion(), $dependency->getHighestVersion()),
48
                'versionCompatible' => self::isVersionCompatible($dependency)
49
            ];
50
        }
51
52
        return $groupedConstraints;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $groupedConstraints returns the type array which is incompatible with the return type mandated by TYPO3Fluid\Fluid\Core\Vi...terface::renderStatic() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
53
    }
54
55
    protected static function getTransformedIdentifier(string $identifier): string
56
    {
57
        return in_array($identifier, Dependency::$specialDependencies, true)
58
            ? strtoupper($identifier)
59
            : strtolower($identifier);
60
    }
61
62
    protected static function getVersionString(string $lowestVersion, string $highestVersion): string
63
    {
64
        $version = '';
65
66
        if ($lowestVersion !== '') {
67
            $version .= '(' . $lowestVersion;
68
            if ($highestVersion !== '') {
69
                $version .= ' - ' . $highestVersion;
70
            }
71
            $version .= ')';
72
        }
73
74
        return $version;
75
    }
76
77
    protected static function isVersionCompatible(Dependency $dependency): bool
78
    {
79
        if ($dependency->getIdentifier() === 'typo3') {
80
            return $dependency->isVersionCompatible(VersionNumberUtility::getNumericTypo3Version());
81
        }
82
        if ($dependency->getIdentifier() === 'php') {
83
            return $dependency->isVersionCompatible(PHP_VERSION);
84
        }
85
        return true;
86
    }
87
}
88