Passed
Pull Request — master (#1887)
by Janko
53:41
created

TwigHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 9
dl 0
loc 11
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Twig;
6
7
use JBBCode\Parser;
8
use Noodlehaus\ConfigInterface;
9
use Stu\Component\Building\NameAbbreviations;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Building\NameAbbreviations 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...
10
use Stu\Component\Colony\ColonyMenuEnum;
11
use Stu\Component\Game\ModuleViewEnum;
12
use Stu\Component\Ship\Crew\ShipCrewCalculatorInterface;
13
use Stu\Lib\Colony\PlanetFieldHostInterface;
14
use Stu\Lib\ModuleScreen\GradientColorInterface;
15
use Stu\Module\Colony\Lib\ColonyEpsProductionPreviewWrapper;
16
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
17
use Stu\Module\Colony\Lib\ColonyProductionPreviewWrapper;
18
use Stu\Module\Control\StuTime;
19
use Stu\Module\Ship\Lib\Battle\FightLibInterface;
20
use Stu\Module\Ship\Lib\ShipNfsItem;
21
use Stu\Module\Template\TemplateHelperInterface;
22
use Stu\Orm\Entity\AnomalyInterface;
23
use Stu\Orm\Entity\BuildingInterface;
24
use Stu\Orm\Entity\ShipInterface;
25
use Twig\Environment;
26
use Twig\TwigFilter;
27
use Twig\TwigFunction;
28
29
class TwigHelper
30
{
31 4
    public function __construct(
32
        private Environment $environment,
33
        private Parser $parser,
34
        private ConfigInterface $config,
35
        private FightLibInterface $fightLib,
36
        private ColonyLibFactoryInterface $colonyLibFactory,
37
        private ShipCrewCalculatorInterface $shipCrewCalculator,
38
        private GradientColorInterface $gradientColor,
39
        private TemplateHelperInterface $templateHelper,
40
        private StuTime $stuTime
41 4
    ) {}
42
43 4
    public function registerGlobalVariables(): void
44
    {
45 4
        $this->environment->addGlobal(
46 4
            'ASSET_PATHS',
47 4
            [
48 4
                'alliance' => $this->config->get('game.alliance_avatar_path'),
49 4
                'user' => $this->config->get('game.user_avatar_path'),
50 4
                'faction' => 'assets/rassen/',
51 4
            ]
52 4
        );
53
    }
54
55
    /**
56
     * Registers global available twig methods and filters
57
     */
58 4
    public function registerFiltersAndFunctions(): void
59
    {
60 4
        $this->registerFilters();
61 4
        $this->registerFunctions();
62
    }
63
64 4
    private function registerFilters(): void
65
    {
66 4
        $bbcode2txtFilter = new TwigFilter('bbcode2txt', fn($string): string => $this->parser->parse($string)->getAsText());
67 4
        $this->environment->addFilter($bbcode2txtFilter);
68
69 4
        $bbcodeFilter = new TwigFilter('bbcode', fn($string): string => $this->parser->parse($string)->getAsHTML(), ['is_safe' => ['html']]);
70 4
        $this->environment->addFilter($bbcodeFilter);
71
72 4
        $jsquoteFilter = new TwigFilter('jsquote', fn($string): string => $this->templateHelper->jsquote($string));
73 4
        $this->environment->addFilter($jsquoteFilter);
74
75 4
        $addPlusCharacterFilter = new TwigFilter('addPlusCharacter', function ($value): string {
76
            if (is_int($value)) {
77
                return $this->templateHelper->addPlusCharacter((string) $value);
78
            }
79
            return $this->templateHelper->addPlusCharacter($value);
80 4
        });
81 4
        $this->environment->addFilter($addPlusCharacterFilter);
82
83 4
        $formatSecondsFilter = new TwigFilter('formatSeconds', function ($value): string {
84
            if (is_int($value)) {
85
                return $this->templateHelper->formatSeconds((string) $value);
86
            }
87
            return $this->templateHelper->formatSeconds($value);
88 4
        });
89 4
        $this->environment->addFilter($formatSecondsFilter);
90
91 4
        $planetFieldTitleFilter = new TwigFilter('planetFieldTitle', fn($planetField): string => $this->templateHelper->getPlanetFieldTitle($planetField));
92 4
        $this->environment->addFilter($planetFieldTitleFilter);
93
94 4
        $planetFieldTypeDescriptionFilter = new TwigFilter('planetFieldTypeDescription', fn($id): string => $this->templateHelper->getPlanetFieldTypeDescription($id));
95 4
        $this->environment->addFilter($planetFieldTypeDescriptionFilter);
96
97 4
        $formatProductionValueFilter = new TwigFilter('formatProductionValue', fn($value): string => $this->templateHelper->formatProductionValue($value));
98 4
        $this->environment->addFilter($formatProductionValueFilter);
99
100 4
        $isPositiveFilter = new TwigFilter('isPositive', fn(int $value): bool => $value > 0);
101 4
        $this->environment->addFilter($isPositiveFilter);
102
103 4
        $stuDateTimeFilter = new TwigFilter('stuDateTime', fn($value): string => $this->stuTime->transformToStuDateTime($value));
104 4
        $this->environment->addFilter($stuDateTimeFilter);
105
106 4
        $stuDateFilter = new TwigFilter('stuDate', fn($value): string => $this->stuTime->transformToStuDate($value));
107 4
        $this->environment->addFilter($stuDateFilter);
108
109 4
        $nl2brFilter = new TwigFilter('nl2br', fn(string $value): string => nl2br($value));
110 4
        $this->environment->addFilter($nl2brFilter);
111
112 4
        $htmlSafeFilter = new TwigFilter('htmlSafe', fn(string $text): string => htmlspecialchars($text));
113 4
        $this->environment->addFilter($htmlSafeFilter);
114
115 4
        $adventDoorFilter = new TwigFilter('adventDoor', fn(AnomalyInterface $anomaly): int => (int)((120 - $anomaly->getRemainingTicks()) / 5) + 1);
116 4
        $this->environment->addFilter($adventDoorFilter);
117
118 4
        $shortNameFilter = new TwigFilter('shortName', fn(string $name): string => array_reduce(
119 4
            array_keys(NameAbbreviations::ABBREVIATIONS),
120 4
            fn(string $value, string $from): string => str_replace($from, NameAbbreviations::ABBREVIATIONS[$from], $value),
121 4
            $name
122 4
        ));
123 4
        $this->environment->addFilter($shortNameFilter);
124
125 4
        $getMaxCrewCountByShipFilter = new TwigFilter('getMaxCrewCountByShip', fn(ShipInterface $ship): int => $this->shipCrewCalculator->getMaxCrewCountByShip($ship));
126 4
        $this->environment->addFilter($getMaxCrewCountByShipFilter);
127
128 4
        $numberWithThousandSeperatorFilter = new TwigFilter('numberWithThousandSeperator', fn($value): string => $this->templateHelper->getNumberWithThousandSeperator($value));
129 4
        $this->environment->addFilter($numberWithThousandSeperatorFilter);
130
    }
131
132 4
    private function registerFunctions(): void
133
    {
134 4
        $canAttackTargetFunction = new TwigFunction('canAttackTarget', fn(ShipInterface $ship, ShipInterface|ShipNfsItem $target): bool => $this->fightLib->canAttackTarget($ship, $target));
135 4
        $this->environment->addFunction($canAttackTargetFunction);
136
137 4
        $getEpsProductionPreviewFunction = new TwigFunction('getEpsProductionPreview', fn(PlanetFieldHostInterface $host, BuildingInterface $building): ColonyEpsProductionPreviewWrapper => $this->colonyLibFactory->createEpsProductionPreviewWrapper($host, $building));
138 4
        $this->environment->addFunction($getEpsProductionPreviewFunction);
139
140 4
        $getCommodityProductionPreviewFunction = new TwigFunction('getCommodityProductionPreview', fn(PlanetFieldHostInterface $host, BuildingInterface $building): ColonyProductionPreviewWrapper => $this->colonyLibFactory->createColonyProductionPreviewWrapper($building, $host));
141 4
        $this->environment->addFunction($getCommodityProductionPreviewFunction);
142
143 4
        $getColonyMenuClassFunction = new TwigFunction('getColonyMenuClass', fn(ColonyMenuEnum $currentMenu, int $value): string => ColonyMenuEnum::getMenuClass($currentMenu, $value));
144 4
        $this->environment->addFunction($getColonyMenuClassFunction);
145
146 4
        $getViewFunction = new TwigFunction('getView', fn(string $value): ModuleViewEnum => ModuleViewEnum::from($value));
147 4
        $this->environment->addFunction($getViewFunction);
148
149 4
        $getUniqIdFunction = new TwigFunction('getUniqId', fn(): string => uniqid());
150 4
        $this->environment->addFunction($getUniqIdFunction);
151
152 4
        $gradientColorFunction = new TwigFunction('gradientColor', fn(int $value, int $lowest, int $highest): string => $this->gradientColor->calculateGradientColor($value, $lowest, $highest));
153 4
        $this->environment->addFunction($gradientColorFunction);
154
    }
155
}
156