Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions migrations/Migrations.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions migrations/UninstallPsalm/UninstallPsalm.mk
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions migrations/UninstallPsalm/migrate
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

use Laminas\Internal\Migrations\GitAttributes;
use Laminas\Internal\Migrations\ProjectInformation;

require __DIR__ . '/../autoloader.php';

/** @var mixed $rootDirectory */
$rootDirectory = $argv[1] ?? null;
if (
! is_string($rootDirectory)
|| $rootDirectory === ''
|| ! is_dir($rootDirectory)
) {
throw new RuntimeException('Pass a single argument representing the root directory of the target library');
}

$project = ProjectInformation::fromDirectory($rootDirectory);
$attributes = new GitAttributes($project->gitAttributes);
$attributes->unignore('psalm.xml');
$attributes->unignore('psalm.xml.dist');
$attributes->unignore('psalm-baseline.xml');
34 changes: 34 additions & 0 deletions migrations/src/GitAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
101 changes: 101 additions & 0 deletions migrations/test/GitAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,105 @@ public function testResultOfIgnoringThings(string $source, string $expect, strin
$attributes->ignore($ignore);
self::assertStringEqualsFile($file, $expect);
}

/**
* @return iterable<string, array{
* 0: non-empty-string,
* 1: non-empty-string,
* 2: non-empty-string,
* }>
*/
public static function unignoreProvider(): iterable
{
yield 'Leading slashes normalised, removed successfully' => [
<<<TXT
# A Comment
/thing1 export-ignore
thing2 export-ignore

/thing/thing3 export-ignore

TXT,
<<<TXT
# A Comment
thing2 export-ignore

/thing/thing3 export-ignore

TXT,
'thing1',
];

yield 'Matching slashes, removed successfully' => [
<<<TXT
# A Comment
/thing1 export-ignore
thing2 export-ignore

/thing/thing3 export-ignore

TXT,
<<<TXT
# A Comment
thing2 export-ignore

/thing/thing3 export-ignore

TXT,
'/thing1',
];

yield 'Slash mismatch, removed successfully' => [
<<<TXT
# A Comment
/thing1 export-ignore
thing2 export-ignore

/thing/thing3 export-ignore

TXT,
<<<TXT
# A Comment
/thing1 export-ignore

/thing/thing3 export-ignore

TXT,
'/thing2',
];

yield 'Not currently ignored' => [
<<<TXT
# A Comment
/thing1 export-ignore
thing2 export-ignore

/thing/thing3 export-ignore

TXT,
<<<TXT
# A Comment
/thing1 export-ignore
thing2 export-ignore

/thing/thing3 export-ignore

TXT,
'thing4',
];
}

/**
* @param non-empty-string $source
* @param non-empty-string $expect
* @param non-empty-string $ignore
*/
#[DataProvider('unignoreProvider')]
public function testResultOfUnIgnoringThings(string $source, string $expect, string $ignore): void
{
$file = TestHelper::writeToTempFile($source);
$attributes = new GitAttributes($file);
$attributes->unignore($ignore);
self::assertStringEqualsFile($file, $expect);
}
}
Loading