| 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
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 |