1
|
|
|
<?php |
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\XmlAccessorInterface; |
11
|
|
|
use SimpleXMLElement; |
12
|
|
|
|
13
|
|
|
class InspectionsPatcher implements ConfigPatcherInterface |
14
|
|
|
{ |
15
|
|
|
use CopyFilesTrait; |
16
|
|
|
|
17
|
|
|
const PROJECT_PHPCS = 'phpcs.xml'; |
18
|
|
|
const INSPECTION_PROFILE = 'inspectionProfiles/MediaCT.xml'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var XmlAccessorInterface |
22
|
|
|
*/ |
23
|
|
|
private $xmlAccessor; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
* |
28
|
|
|
* @param XmlAccessorInterface $xmlAccessor |
29
|
|
|
*/ |
30
|
|
|
public function __construct(XmlAccessorInterface $xmlAccessor) |
31
|
|
|
{ |
32
|
|
|
$this->xmlAccessor = $xmlAccessor; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Patch the config. |
37
|
|
|
* |
38
|
|
|
* @param EnvironmentInterface $environment |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function patch( |
43
|
|
|
EnvironmentInterface $environment |
44
|
|
|
) { |
45
|
|
|
$this->copyDirectory( |
46
|
|
|
$environment->getDefaultsFilesystem(), |
47
|
|
|
$environment->getIdeConfigFilesystem(), |
48
|
|
|
'inspectionProfiles' |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$this->setProjectProfiles($environment); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set profiles on project level. |
56
|
|
|
* |
57
|
|
|
* @param EnvironmentInterface $environment |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
private function setProjectProfiles(EnvironmentInterface $environment) |
62
|
|
|
{ |
63
|
|
|
$projectFs = $environment->getProjectFilesystem(); |
64
|
|
|
if (!$projectFs->has(self::PROJECT_PHPCS)) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$ideConfigFs = $environment->getIdeConfigFilesystem(); |
69
|
|
|
|
70
|
|
|
$xml = simplexml_load_string( |
71
|
|
|
$ideConfigFs->read(self::INSPECTION_PROFILE) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$this->setProjectPhpCsProfile($xml); |
75
|
|
|
$ideConfigFs->put(self::INSPECTION_PROFILE, $xml->asXML()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set the PhpCs profile in the XML. |
80
|
|
|
* |
81
|
|
|
* @param SimpleXMLElement $xml |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
private function setProjectPhpCsProfile(SimpleXMLElement $xml) |
86
|
|
|
{ |
87
|
|
|
$node = $this->xmlAccessor->getDescendant( |
88
|
|
|
$xml, |
89
|
|
|
[ |
90
|
|
|
['profile'], |
91
|
|
|
['inspection_tool', ['class' => 'PhpCSValidationInspection']], |
92
|
|
|
['option', ['name' => 'CUSTOM_RULESET_PATH']] |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
$this->xmlAccessor->setAttributes( |
96
|
|
|
$node, |
97
|
|
|
['value' => '$PROJECT_DIR$/' . self::PROJECT_PHPCS] |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|