Passed
Push — master ( e8b4e9...1426d4 )
by Klaas
02:00 queued 11s
created

TemplateSettingsPatcher::patchIncludes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 3
b 0
f 1
nc 3
nop 1
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 12
rs 10
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
    /**
18
     * @var XmlAccessorInterface
19
     */
20
    private $xmlAccessor;
21
22
    /**
23
     * @var array
24
     */
25
    private $includePaths = [];
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param XmlAccessorInterface $xmlAccessor
31
     */
32
    public function __construct(XmlAccessorInterface $xmlAccessor)
33
    {
34
        $this->xmlAccessor  = $xmlAccessor;
35
        $this->includePaths = [
36
            'M2-PHP-File-Header.php',
37
            'M2-Settings.php',
38
            'M2-XML-File-Header.xml'
39
        ];
40
    }
41
42
    /**
43
     * Patch the config.
44
     *
45
     * @param EnvironmentInterface $environment
46
     *
47
     * @return void
48
     */
49
    public function patch(
50
        EnvironmentInterface $environment
51
    ): void {
52
        $this->patchFileTemplateSettings(
53
            $environment
54
        );
55
        $this->patchIncludes(
56
            $environment
57
        );
58
    }
59
60
    /**
61
     * Patch file template settings if exists otherwise create one.
62
     *
63
     * @param EnvironmentInterface $environment
64
     *
65
     * @return void
66
     */
67
    public function patchFileTemplateSettings(
68
        EnvironmentInterface $environment
69
    ): void {
70
        if (!$environment->getIdeConfigFilesystem()->has('file.template.settings.xml')) {
71
            $this->copyFile(
72
                $environment->getDefaultsFilesystem(),
73
                $environment->getIdeConfigFilesystem(),
74
                'file.template.settings.xml'
75
            );
76
        } else {
77
            $xml = simplexml_load_string(
78
                $environment->getIdeConfigFilesystem()->read('file.template.settings.xml')
79
            );
80
81
            foreach ($this->getFileTemplates() as $xmlTag => $fileTemplateNames) {
82
                foreach ($fileTemplateNames as $fileTemplateName) {
83
                    $node = $this->xmlAccessor->getDescendant(
84
                        $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

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