InspectionsPatcher   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 27
c 4
b 0
f 0
dl 0
loc 85
ccs 27
cts 27
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setProjectProfiles() 0 15 2
A patch() 0 10 1
A __construct() 0 3 1
A setProjectPhpCsProfile() 0 13 1
1
<?php
2
3
/**
4
 * Copyright MediaCT. All rights reserved.
5
 * https://www.mediact.nl
6
 */
7
8
declare(strict_types=1);
9
10
namespace Mediact\CodingStandard\PhpStorm\Patcher;
11
12
use Mediact\CodingStandard\PhpStorm\EnvironmentInterface;
13
use Mediact\CodingStandard\PhpStorm\XmlAccessorInterface;
14
use SimpleXMLElement;
15
16
class InspectionsPatcher implements ConfigPatcherInterface
17
{
18
    use CopyFilesTrait;
19
20
    public const PROJECT_PHPCS      = 'phpcs.xml';
21
    public const INSPECTION_PROFILE = 'inspectionProfiles/MediaCT.xml';
22
23
    /**
24
     * @var XmlAccessorInterface
25
     */
26
    private $xmlAccessor;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param XmlAccessorInterface $xmlAccessor
32
     */
33 1
    public function __construct(XmlAccessorInterface $xmlAccessor)
34
    {
35 1
        $this->xmlAccessor = $xmlAccessor;
36 1
    }
37
38
    /**
39
     * Patch the config.
40
     *
41
     * @param EnvironmentInterface $environment
42
     *
43
     * @return void
44
     */
45 1
    public function patch(
46
        EnvironmentInterface $environment
47
    ): void {
48 1
        $this->copyDirectory(
49 1
            $environment->getDefaultsFilesystem(),
50 1
            $environment->getIdeConfigFilesystem(),
51 1
            'inspectionProfiles'
52
        );
53
54 1
        $this->setProjectProfiles($environment);
55 1
    }
56
57
    /**
58
     * Set profiles on project level.
59
     *
60
     * @param EnvironmentInterface $environment
61
     *
62
     * @return void
63
     */
64 2
    private function setProjectProfiles(EnvironmentInterface $environment): void
65
    {
66 2
        $projectFs = $environment->getProjectFilesystem();
67 2
        if (!$projectFs->has(self::PROJECT_PHPCS)) {
68 1
            return;
69
        }
70
71 1
        $ideConfigFs = $environment->getIdeConfigFilesystem();
72
73 1
        $xml = simplexml_load_string(
74 1
            $ideConfigFs->read(self::INSPECTION_PROFILE)
75
        );
76
77 1
        $this->setProjectPhpCsProfile($xml);
78 1
        $ideConfigFs->put(self::INSPECTION_PROFILE, $xml->asXML());
0 ignored issues
show
Bug introduced by
It seems like $xml->asXML() can also be of type true; however, parameter $contents of Mediact\CodingStandard\P...esystemInterface::put() does only seem to accept string, 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

78
        $ideConfigFs->put(self::INSPECTION_PROFILE, /** @scrutinizer ignore-type */ $xml->asXML());
Loading history...
79 1
    }
80
81
    /**
82
     * Set the PhpCs profile in the XML.
83
     *
84
     * @param SimpleXMLElement $xml
85
     *
86
     * @return void
87
     */
88 1
    private function setProjectPhpCsProfile(SimpleXMLElement $xml): void
89
    {
90 1
        $node = $this->xmlAccessor->getDescendant(
91 1
            $xml,
92
            [
93 1
                ['profile'],
94
                ['inspection_tool', ['class' => 'PhpCSValidationInspection']],
95
                ['option', ['name' => 'CUSTOM_RULESET_PATH']]
96
            ]
97
        );
98 1
        $this->xmlAccessor->setAttributes(
99 1
            $node,
100 1
            ['value' => '$PROJECT_DIR$/' . self::PROJECT_PHPCS]
101
        );
102 1
    }
103
}
104