diff --git a/README.md b/README.md index e478056..4e51b42 100644 --- a/README.md +++ b/README.md @@ -128,3 +128,7 @@ make install-rector ``` For more information about this migration [see the README](migrations/InstallStandaloneRector/README.md). + +### Psalm Removal + +Psalm can be removed from the project with `make uninstall-psalm` if Psalm is installed. diff --git a/migrations/Migrations.mk b/migrations/Migrations.mk index cf8c0c6..db37fce 100644 --- a/migrations/Migrations.mk +++ b/migrations/Migrations.mk @@ -2,3 +2,4 @@ include $(_MAKEFILE_DIR)migrations/PHP-86/PHP86.mk include $(_MAKEFILE_DIR)migrations/InstallStandaloneRector/InstallStandaloneRector.mk include $(_MAKEFILE_DIR)migrations/InstallMago/InstallMago.mk +include $(_MAKEFILE_DIR)migrations/UninstallPsalm/UninstallPsalm.mk diff --git a/migrations/UninstallPsalm/UninstallPsalm.mk b/migrations/UninstallPsalm/UninstallPsalm.mk new file mode 100644 index 0000000..3e3ddad --- /dev/null +++ b/migrations/UninstallPsalm/UninstallPsalm.mk @@ -0,0 +1,30 @@ + +HAS_PSALM := FALSE +PSALM_CONFIG := + +ifneq ("$(wildcard psalm.xml.dist)","") + HAS_PSALM := TRUE + PSALM_CONFIG := psalm.xml.dist +endif + +ifneq ("$(wildcard psalm.xml)","") + HAS_PSALM := TRUE + PSALM_CONFIG := psalm.xml +endif + +ifeq ("$(HAS_PSALM)","TRUE") + +UNINSTALL_PSALM_DIRECTORY := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) + +HELP += $(call MK_HELP,'uninstall-psalm','Remove Psalm along with its configuration file and baseline') + +uninstall-psalm: + $(DOCKER_RUN) ${DOCKER_IMAGE_NAME} composer rm psalm/plugin-phpunit + $(DOCKER_RUN) ${DOCKER_IMAGE_NAME} composer rm vimeo/psalm + rm -f psalm.xml + rm -f psalm.xml.dist + rm -f psalm-baseline.xml + $(DOCKER_RUN) ${DOCKER_IMAGE_NAME} php $(UNINSTALL_PSALM_DIRECTORY)migrate $(PROJECT_DIR) +.PHONY: uninstall-psalm + +endif diff --git a/migrations/UninstallPsalm/migrate b/migrations/UninstallPsalm/migrate new file mode 100644 index 0000000..8600446 --- /dev/null +++ b/migrations/UninstallPsalm/migrate @@ -0,0 +1,25 @@ +#!/usr/bin/env php +gitAttributes); +$attributes->unignore('psalm.xml'); +$attributes->unignore('psalm.xml.dist'); +$attributes->unignore('psalm-baseline.xml'); diff --git a/migrations/src/GitAttributes.php b/migrations/src/GitAttributes.php index 208ef58..d5ce801 100644 --- a/migrations/src/GitAttributes.php +++ b/migrations/src/GitAttributes.php @@ -5,9 +5,11 @@ namespace Laminas\Internal\Migrations; use function assert; +use function count; use function explode; use function file_get_contents; use function file_put_contents; +use function implode; use function ltrim; use function sprintf; use function str_starts_with; @@ -43,6 +45,38 @@ public function ignore(string $path): void $this->write($content); } + public function unignore(string $path): void + { + $unmodifiedLines = $this->getLines(); + $lines = $unmodifiedLines; + + foreach ($lines as $index => $line) { + if (! $this->isExportIgnoreLine($line)) { + continue; + } + + $result = str_starts_with( + ltrim(trim($line), DIRECTORY_SEPARATOR), + ltrim(trim($path), DIRECTORY_SEPARATOR), + ); + + if (! $result) { + continue; + } + + unset($lines[$index]); + } + + if (count($unmodifiedLines) === count($lines)) { + return; + } + + $data = implode(PHP_EOL, $lines); + assert($data !== '', 'We should not have an empty file here'); + + $this->write($data); + } + private function isExportIgnored(string $path): bool { foreach ($this->getLines() as $line) { diff --git a/migrations/test/GitAttributesTest.php b/migrations/test/GitAttributesTest.php index 5ecbbdc..a746f31 100644 --- a/migrations/test/GitAttributesTest.php +++ b/migrations/test/GitAttributesTest.php @@ -135,4 +135,105 @@ public function testResultOfIgnoringThings(string $source, string $expect, strin $attributes->ignore($ignore); self::assertStringEqualsFile($file, $expect); } + + /** + * @return iterable + */ + public static function unignoreProvider(): iterable + { + yield 'Leading slashes normalised, removed successfully' => [ + << [ + << [ + << [ + <<unignore($ignore); + self::assertStringEqualsFile($file, $expect); + } }