Passed
Pull Request — master (#11)
by
unknown
02:39
created

TemplateSettingsPatcher::getFileTemplates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 22
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 24
ccs 0
cts 2
cp 0
crap 2
rs 9.568
1
<?php declare(strict_types=1);
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Mediact\CodingStandard\PhpStorm\Patcher;
8
9
use Mediact\CodingStandard\PhpStorm\EnvironmentInterface;
10
use Mediact\CodingStandard\PhpStorm\FilesystemInterface;
11
use Mediact\CodingStandard\PhpStorm\XmlAccessorInterface;
12
13
class TemplateSettingsPatcher implements ConfigPatcherInterface
14
{
15
    use CopyFilesTrait;
16
17
    const INCLUDES_PATH = [
18
        'M2-PHP-File-Header.php',
19
        'M2-Settings.php',
20
        'M2-XML-File-Header.xml'
21
    ];
22
23
    /**
24
     * @var XmlAccessorInterface
25
     */
26
    private $xmlAccessor;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param XmlAccessorInterface $xmlAccessor
32
     */
33
    public function __construct(XmlAccessorInterface $xmlAccessor)
34
    {
35
        $this->xmlAccessor = $xmlAccessor;
36
    }
37
38
    /**
39
     * Patch the config.
40
     *
41
     * @param EnvironmentInterface $environment
42
     *
43
     * @return void
44
     */
45
    public function patch(
46
        EnvironmentInterface $environment
47
    ): void {
48
        $this->patchFileTemplateSettings(
49
            $environment
50
        );
51
        $this->patchIncludes(
52
            $environment
53
        );
54
    }
55
56
    /**
57
     * Patch file template settings if exists otherwise create one.
58
     *
59
     * @param EnvironmentInterface $environment
60
     *
61
     * @return void
62
     */
63
    public function patchFileTemplateSettings(
64
        EnvironmentInterface $environment
65
    ): void {
66
        if (!$environment->getIdeConfigFilesystem()->has('file.template.settings.xml')) {
67
            $this->copyFile(
68
                $environment->getDefaultsFilesystem(),
69
                $environment->getIdeConfigFilesystem(),
70
                'file.template.settings.xml'
71
            );
72
        } else {
73
            $xml = simplexml_load_string(
74
                $environment->getIdeConfigFilesystem()->read('file.template.settings.xml')
75
            );
76
77
            foreach ($this->getFileTemplates() as $xmlTag => $fileTemplateNames) {
78
                foreach ($fileTemplateNames as $fileTemplateName) {
79
                    $node = $this->xmlAccessor->getDescendant(
80
                        $xml,
0 ignored issues
show
Bug introduced by
It seems like $xml can also be of type false; however, parameter $element of Mediact\CodingStandard\P...erface::getDescendant() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
                        /** @scrutinizer ignore-type */ $xml,
Loading history...
81
                        [
82
                            [
83
                                'component',
84
                                ['name' => 'ExportableFileTemplateSettings']
85
                            ],
86
                            [$xmlTag],
87
                            ['template', ['name' => $fileTemplateName]]
88
                        ]
89
                    );
90
                    $this->xmlAccessor->setAttributes(
91
                        $node,
92
                        [
93
                            'reformat' => 'false',
94
                            'live-template-enabled' => 'true'
95
                        ]
96
                    );
97
                    $environment->getIdeConfigFilesystem()->put('file.template.settings.xml', $xml->asXML());
98
                }
99
            }
100
        }
101
    }
102
103
    /**
104
     * Add copyright files to project if they do not exist
105
     *
106
     * @param EnvironmentInterface $environment
107
     *
108
     * @return void
109
     */
110
    public function patchIncludes(EnvironmentInterface $environment): void
111
    {
112
        foreach (self::INCLUDES_PATH as $fileName) {
113
            if (!$environment->getIdeConfigFilesystem()->has("includes/$fileName")) {
114
                $this->copyFile(
115
                    $environment->getDefaultsFilesystem(),
116
                    $environment->getIdeConfigFilesystem(),
117
                    $fileName
118
                );
119
            }
120
        }
121
    }
122
123
    /**
124
     * Enable file templates
125
     *
126
     * @return array
127
     */
128
    public function getFileTemplates(): array
129
    {
130
        return [
131
            'default_templates' => [
132
                'M2-Acl XML.xml',
133
                'M2-Class.php',
134
                'M2-Class-Backend-Controller',
135
                'M2-Class-Block.php',
136
                'M2-Class-Helper.php',
137
                'M2-Class-Observer.php',
138
                'M2-Class-ViewModel.php',
139
                'M2-Config-XML.xml',
140
                'M2-Db-schema-XML.xml',
141
                'M2-DI.xml',
142
                'M2-Events.xml',
143
                'M2-Extension-Attributes-XML.xml',
144
                'M2-Layout-XML.xml',
145
                'M2-Menu.xml',
146
                'M2-Module-XML.xml',
147
                'M2-Registration.php',
148
                'M2-Routes.xml',
149
                'M2-Sales-XML.xml',
150
                'M2-System-include-XML.xml',
151
                'M2-System-XML.xml'
152
            ]
153
        ];
154
    }
155
}
156