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
49 changes: 49 additions & 0 deletions .github/workflows/rector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Rector

on: [push, pull_request]

jobs:
rector:
name: Rector
runs-on: [ubuntu-latest]

steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1

- name: Checkout code
uses: actions/checkout@v5

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer install --prefer-dist --no-progress --ignore-platform-req=ext-*

- name: Restore result cache
uses: actions/cache/restore@v4
with:
path: .rector.result.cache
key: rector-result-cache-${{ github.run_id }}
restore-keys: |
rector-result-cache-

- name: Rector
run: php vendor/bin/rector process --config .rector.php --dry-run

- name: Save result cache
uses: actions/cache/save@v4
if: always()
with:
path: .rector.result.cache
key: rector-result-cache-${{ github.run_id }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/vendor
/.phpstan.cache
/.phpunit.cache
/.rector.result.cache
.php-cs-fixer.cache
.phpmd.result-cache.php
composer.lock
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'modernize_types_casting' => true,
'nullable_type_declaration_for_default_null_value' => true,
'single_quote' => true,
'php_unit_test_case_static_method_calls' => true,
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
'trailing_comma_in_multiline' => ['after_heredoc' => true, 'elements' => ['arguments', 'array_destructuring', 'arrays']],
])
->setFinder(
Expand Down
47 changes: 47 additions & 0 deletions .rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

use Rector\Caching\ValueObject\Storage\FileCacheStorage;
use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;

try {
return RectorConfig::configure()
->withFileExtensions(['php'])
->withCache(
cacheDirectory: '.rector.result.cache',
cacheClass: FileCacheStorage::class,
)
->withPhpSets(
php81: true,
)
->withPaths([
__DIR__,
])
->withSkipPath(__DIR__ . '/vendor')
->withSkip([
# skip: use static methods
PreferPHPUnitThisCallRector::class
])
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
typeDeclarations: true,
privatization: true,
naming: true,
instanceOf: true,
earlyReturn: true,
strictBooleans: false,
carbon: true,
rectorPreset: true,
phpunitCodeQuality: true,
doctrineCodeQuality: false,
symfonyCodeQuality: false,
symfonyConfigs: false,
);
} catch (InvalidConfigurationException $exception) {
echo $exception->getMessage();
exit(1);
}
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"phpstan/phpstan-phpunit": "^2.0",
"phpunit/phpunit": "^9.6",
"phpmd/phpmd": "^2.15",
"friendsofphp/php-cs-fixer": "^3.67"
"friendsofphp/php-cs-fixer": "^3.67",
"rector/rector": "^2.1.2"
},
"extra": {
"class": "OpenMage\\ComposerPlugin\\Plugin"
Expand All @@ -46,7 +47,9 @@
"phpstan:baseline": "XDEBUG_MODE=off php vendor/bin/phpstan analyze -b .phpstan.dist.baseline.neon",
"phpunit:test": "XDEBUG_MODE=off php vendor/bin/phpunit --configuration .phpunit.dist.xml --no-coverage",
"phpunit:coverage": "XDEBUG_MODE=coverage php vendor/bin/phpunit --configuration .phpunit.dist.xml --testdox",
"phpunit:coverage-local": "XDEBUG_MODE=coverage php vendor/bin/phpunit --configuration .phpunit.dist.xml --coverage-html build/coverage-composer-plugin --testdox"
"phpunit:coverage-local": "XDEBUG_MODE=coverage php vendor/bin/phpunit --configuration .phpunit.dist.xml --coverage-html build/coverage-composer-plugin --testdox",
"rector:test": "vendor/bin/rector process --config .rector.php --dry-run",
"rector:fix": "vendor/bin/rector process --config .rector.php"
},
"scripts-descriptions": {
"php-cs-fixer:test": "Run php-cs-fixer",
Expand All @@ -56,7 +59,9 @@
"phpstan:baseline": "Run phpstan and update baseline",
"phpunit:test": "Run PHPUnit",
"phpunit:coverage": "Run PHPUnit with code coverage (requires XDEBUG enabled)",
"phpunit:coverage-local": "Run PHPUnit with local HTML code coverage (requires XDEBUG enabled)"
"phpunit:coverage-local": "Run PHPUnit with local HTML code coverage (requires XDEBUG enabled)",
"rector:test": "Run rector",
"rector:fix": "Run rector and fix issues"
},
"funding": [
{
Expand Down
62 changes: 34 additions & 28 deletions src/OpenMage/ComposerPlugin/Copy/AbstractCopyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct(?Event $event)
public function processComposerInstall(): void
{
$package = $this->getComposerPackage();
if (!$package || !$this instanceof CopyFromComposerInterface) {
if (!$package instanceof BasePackage || !$this instanceof CopyFromComposerInterface) {
return;
}

Expand All @@ -74,13 +74,14 @@ public function processComposerInstall(): void
$filesystem = $this->getFileSystem();

if (!$filesystem->exists($copySourcePath) && $this instanceof CopyFromUnpkgInterface) {
if ($event && $event->getIO()->isVerbose()) {
if ($event instanceof Event && $event->getIO()->isVerbose()) {
$event->getIO()->write(sprintf(
'Fallback to Unpkg %s for %s',
$this->getUnpkgName(),
$this->getComposerName(),
));
}

$this->processUnpkgInstall();
return;
}
Expand All @@ -96,11 +97,11 @@ public function processComposerInstall(): void

try {
$filesystem->copy($copySource, $copytarget);
if ($event && $event->getIO()->isVeryVerbose()) {
if ($event instanceof Event && $event->getIO()->isVeryVerbose()) {
$event->getIO()->write(sprintf('Copy %s to %s', $copySource, $copytarget));
}
} catch (IOException $exception) {
if ($event) {
if ($event instanceof Event) {
$event->getIO()->write($exception->getMessage());
}
}
Expand All @@ -119,7 +120,7 @@ public function processUnpkgInstall(): void
$event = $this->getEvent();
$sourcePath = $this->getUnpkSourcePath();

if ($event && $event->getIO()->isVerbose()) {
if ($event instanceof Event && $event->getIO()->isVerbose()) {
$event->getIO()->write(sprintf(
'Trying to download %s %s from %s',
$this->getUnpkgName(),
Expand All @@ -133,29 +134,32 @@ public function processUnpkgInstall(): void
try {
$content = file_get_contents($sourceFilePath);
} catch (ErrorException $errorException) {
if ($event) {
if ($event instanceof Event) {
$event->getIO()->write($errorException->getMessage());
}

return;
}

if (!$content) {
if ($event) {
if ($event instanceof Event) {
$event->getIO()->write(sprintf('Could not read from %s', $sourceFilePath));
}

return;
}

try {
$targetFilePath = $this->getCopyTargetPath() . '/' . $fileName;
$this->getFileSystem()->dumpFile($targetFilePath, $content);
if ($event && $event->getIO()->isVerbose()) {
if ($event instanceof Event && $event->getIO()->isVerbose()) {
$event->getIO()->write(sprintf('Added %s', $targetFilePath));
}
} catch (IOException $exception) {
if ($event) {
if ($event instanceof Event) {
$event->getIO()->write($exception->getMessage());
}

return;
}
}
Expand All @@ -169,27 +173,29 @@ public function getComposerPackage(): ?BasePackage

$vendorName = $this->getComposerName();
$module = $this->getInstalledComposerPackage($vendorName);
if ($module) {
if ($module instanceof BasePackage) {
return $module;
}

$event = $this->getEvent();
if (!$event) {
if (!$event instanceof Event) {
return null;
}

$locker = $event->getComposer()->getLocker();
$repo = $locker->getLockedRepository();
$lockArrayRepository = $locker->getLockedRepository();

foreach ($repo->getPackages() as $package) {
if ($package->getName() === $vendorName) {
$this->setInstalledComposerPackage($vendorName, $package);
foreach ($lockArrayRepository->getPackages() as $basePackage) {
if ($basePackage->getName() === $vendorName) {
$this->setInstalledComposerPackage($vendorName, $basePackage);
if ($event->getIO()->isVerbose()) {
$event->getIO()->write(sprintf('%s found with version %s', $vendorName, $package->getVersion()));
$event->getIO()->write(sprintf('%s found with version %s', $vendorName, $basePackage->getVersion()));
}

return $this->getInstalledComposerPackage($vendorName);
}
}

return null;
}

Expand All @@ -202,8 +208,9 @@ protected function getUnpkSourcePath(): string
$search = ['{{package}}', '{{version}}'];
$replace = [$this->getUnpkgName(), $this->getUnpkgVersion()];
$path = str_replace($search, $replace, CopyFromUnpkgInterface::UNPKG_URL);
return $path . ($this->getUnpkgSource() ? $this->getUnpkgSource() . '/' : '');
return $path . ($this->getUnpkgSource() !== '' && $this->getUnpkgSource() !== '0' ? $this->getUnpkgSource() . '/' : '');
}

return '';
}

Expand All @@ -216,6 +223,7 @@ protected function getCwd(): string
if ($cwd === false) {
throw new Exception('This should not happen.');
}

return $cwd;
}

Expand All @@ -225,7 +233,7 @@ protected function getCwd(): string
protected function getVendorDirectoryFromComposer(): string
{
$event = $this->getEvent();
if (!$event) {
if (!$event instanceof Event) {
return '';
}

Expand All @@ -240,22 +248,20 @@ protected function getVendorDirectoryFromComposer(): string
protected function getMageRootDirectoryFromComposer(): string
{
$event = $this->getEvent();
if (!$event) {
if (!$event instanceof Event) {
return '';
}

$composerExtra = $event->getComposer()->getPackage()->getExtra();
$magentoRootDir = '';

if (array_key_exists(self::EXTRA_MAGENTO_ROOT_DIR, $composerExtra) &&
$composerExtra[self::EXTRA_MAGENTO_ROOT_DIR] !== '.'
) {
$magentoRootDir = $composerExtra[self::EXTRA_MAGENTO_ROOT_DIR] . '/';
$composerExtra[self::EXTRA_MAGENTO_ROOT_DIR] !== '.') {
return $composerExtra[self::EXTRA_MAGENTO_ROOT_DIR] . '/';
}
return $magentoRootDir;

return '';
}

private function getCopyTargetPath(): string
protected function getCopyTargetPath(): string
{
return sprintf(
'%s/%s%s',
Expand All @@ -270,9 +276,9 @@ protected function getInstalledComposerPackage(string $vendorName): ?BasePackage
return $this->composerPackages[$vendorName] ?? null;
}

private function setInstalledComposerPackage(string $vendorName, BasePackage $package): void
protected function setInstalledComposerPackage(string $vendorName, BasePackage $basePackage): void
{
$this->composerPackages[$vendorName] = $package;
$this->composerPackages[$vendorName] = $basePackage;
}

public function getFileSystem(): Filesystem
Expand Down
2 changes: 2 additions & 0 deletions src/OpenMage/ComposerPlugin/Copy/CopyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
interface CopyInterface
{
public const EXTRA_MAGENTO_ROOT_DIR = 'magento-root-dir';

public const EXTRA_UNPKG_PACKAGES = 'openmage-unpkg-packages';

public const VENDOR_DIR = 'vendor-dir';

public function getCopyTarget(): string;
Expand Down
Loading