Passed
Push — master ( 27afec...e8b4e9 )
by Klaas
01:42 queued 14s
created

TemplateSettingsPatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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
     * Constructor.
24
     *
25
     * @param XmlAccessorInterface $xmlAccessor
26
     */
27
    public function __construct(XmlAccessorInterface $xmlAccessor)
28
    {
29
        $this->xmlAccessor = $xmlAccessor;
30
    }
31
32
    /**
33
     * Patch the config.
34
     *
35
     * @param EnvironmentInterface $environment
36
     *
37
     * @return void
38
     */
39
    public function patch(
40
        EnvironmentInterface $environment
41
    ): void {
42
        $this->patchFileTemplateSettings(
43
            $environment->getIdeConfigFilesystem(),
44
            $environment
45
        );
46
    }
47
48
    /**
49
     * Patch file template settings if exists otherwise create one.
50
     *
51
     * @param FilesystemInterface  $ideConfigFs
52
     * @param EnvironmentInterface $environment
53
     *
54
     * @return void
55
     */
56
    public function patchFileTemplateSettings(
57
        FilesystemInterface $ideConfigFs,
58
        EnvironmentInterface $environment
59
    ): void {
60
        if (!$ideConfigFs->has('file.template.settings.xml')) {
61
            $this->copyFile(
62
                $environment->getDefaultsFilesystem(),
63
                $environment->getIdeConfigFilesystem(),
64
                'file.template.settings.xml'
65
            );
66
        } else {
67
            $xml = simplexml_load_string(
68
                $ideConfigFs->read('file.template.settings.xml')
69
            );
70
71
            foreach ($this->getFileTemplates() as $xmlTag => $fileTemplateNames) {
72
                foreach ($fileTemplateNames as $fileTemplateName) {
73
                    $node = $this->xmlAccessor->getDescendant(
74
                        $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

74
                        /** @scrutinizer ignore-type */ $xml,
Loading history...
75
                        [
76
                            [
77
                                'component',
78
                                ['name' => 'ExportableFileTemplateSettings']
79
                            ],
80
                            [$xmlTag],
81
                            ['template', ['name' => $fileTemplateName]]
82
                        ]
83
                    );
84
                    $this->xmlAccessor->setAttributes(
85
                        $node,
86
                        [
87
                            'reformat' => 'false',
88
                            'live-template-enabled' => 'true'
89
                        ]
90
                    );
91
                    $ideConfigFs->put('file.template.settings.xml', $xml->asXML());
92
                }
93
            }
94
        }
95
    }
96
97
    /**
98
     * Enable file templates
99
     *
100
     * @return array
101
     */
102
    public function getFileTemplates(): array
103
    {
104
        return [
105
            'default_templates' => [
106
                'M2-Acl XML.xml',
107
                'M2-Class.php',
108
                'M2-Class-Block.php',
109
                'M2-Class-Helper.php',
110
                'M2-Class-Observer.php',
111
                'M2-Class-ViewModel.php',
112
                'M2-Config-XML.xml',
113
                'M2-Db-schema-XML.xml',
114
                'M2-DI.xml',
115
                'M2-Extension-Attributes-XML.xml',
116
                'M2-Layout-XML.xml',
117
                'M2-Module-XML.xml',
118
                'M2-Registration.php',
119
                'M2-Sales-XML.xml',
120
                'M2-System-include-XML.xml',
121
                'M2-System-XML.xml'
122
            ],
123
            'includes_templates' => [
124
                'M2-PHP-File-Header.php',
125
                'M2-Settings.php',
126
                'M2-XML-File-Header.xml',
127
            ]
128
        ];
129
    }
130
}
131