Skip to content

Commit 5ea8f5d

Browse files
authored
Minor fixes (#7)
1 parent 0eda2b5 commit 5ea8f5d

21 files changed

+242
-114
lines changed

.github/workflows/rector.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Rector
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
rector:
7+
name: Rector
8+
runs-on: [ubuntu-latest]
9+
10+
steps:
11+
- name: Setup PHP
12+
uses: shivammathur/setup-php@v2
13+
with:
14+
php-version: 8.1
15+
16+
- name: Checkout code
17+
uses: actions/checkout@v5
18+
19+
- name: Get composer cache directory
20+
id: composer-cache
21+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
22+
23+
- name: Cache dependencies
24+
uses: actions/cache@v4
25+
with:
26+
path: ${{ steps.composer-cache.outputs.dir }}
27+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
28+
restore-keys: ${{ runner.os }}-composer-
29+
30+
- name: Install dependencies
31+
run: composer install --prefer-dist --no-progress --ignore-platform-req=ext-*
32+
33+
- name: Restore result cache
34+
uses: actions/cache/restore@v4
35+
with:
36+
path: .rector.result.cache
37+
key: rector-result-cache-${{ github.run_id }}
38+
restore-keys: |
39+
rector-result-cache-
40+
41+
- name: Rector
42+
run: php vendor/bin/rector process --config .rector.php --dry-run
43+
44+
- name: Save result cache
45+
uses: actions/cache/save@v4
46+
if: always()
47+
with:
48+
path: .rector.result.cache
49+
key: rector-result-cache-${{ github.run_id }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/vendor
33
/.phpstan.cache
44
/.phpunit.cache
5+
/.rector.result.cache
56
.php-cs-fixer.cache
67
.phpmd.result-cache.php
78
composer.lock

.php-cs-fixer.dist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'modernize_types_casting' => true,
1010
'nullable_type_declaration_for_default_null_value' => true,
1111
'single_quote' => true,
12-
'php_unit_test_case_static_method_calls' => true,
12+
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
1313
'trailing_comma_in_multiline' => ['after_heredoc' => true, 'elements' => ['arguments', 'array_destructuring', 'arrays']],
1414
])
1515
->setFinder(

.rector.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
6+
use Rector\Config\RectorConfig;
7+
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
8+
9+
try {
10+
return RectorConfig::configure()
11+
->withFileExtensions(['php'])
12+
->withCache(
13+
cacheDirectory: '.rector.result.cache',
14+
cacheClass: FileCacheStorage::class,
15+
)
16+
->withPhpSets(
17+
php81: true,
18+
)
19+
->withPaths([
20+
__DIR__,
21+
])
22+
->withSkipPath(__DIR__ . '/vendor')
23+
->withSkip([
24+
# skip: use static methods
25+
PreferPHPUnitThisCallRector::class
26+
])
27+
->withPreparedSets(
28+
deadCode: true,
29+
codeQuality: true,
30+
codingStyle: true,
31+
typeDeclarations: true,
32+
privatization: true,
33+
naming: true,
34+
instanceOf: true,
35+
earlyReturn: true,
36+
strictBooleans: false,
37+
carbon: true,
38+
rectorPreset: true,
39+
phpunitCodeQuality: true,
40+
doctrineCodeQuality: false,
41+
symfonyCodeQuality: false,
42+
symfonyConfigs: false,
43+
);
44+
} catch (InvalidConfigurationException $exception) {
45+
echo $exception->getMessage();
46+
exit(1);
47+
}

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"phpstan/phpstan-phpunit": "^2.0",
3434
"phpunit/phpunit": "^9.6",
3535
"phpmd/phpmd": "^2.15",
36-
"friendsofphp/php-cs-fixer": "^3.67"
36+
"friendsofphp/php-cs-fixer": "^3.67",
37+
"rector/rector": "^2.1.2"
3738
},
3839
"extra": {
3940
"class": "OpenMage\\ComposerPlugin\\Plugin"
@@ -46,7 +47,9 @@
4647
"phpstan:baseline": "XDEBUG_MODE=off php vendor/bin/phpstan analyze -b .phpstan.dist.baseline.neon",
4748
"phpunit:test": "XDEBUG_MODE=off php vendor/bin/phpunit --configuration .phpunit.dist.xml --no-coverage",
4849
"phpunit:coverage": "XDEBUG_MODE=coverage php vendor/bin/phpunit --configuration .phpunit.dist.xml --testdox",
49-
"phpunit:coverage-local": "XDEBUG_MODE=coverage php vendor/bin/phpunit --configuration .phpunit.dist.xml --coverage-html build/coverage-composer-plugin --testdox"
50+
"phpunit:coverage-local": "XDEBUG_MODE=coverage php vendor/bin/phpunit --configuration .phpunit.dist.xml --coverage-html build/coverage-composer-plugin --testdox",
51+
"rector:test": "vendor/bin/rector process --config .rector.php --dry-run",
52+
"rector:fix": "vendor/bin/rector process --config .rector.php"
5053
},
5154
"scripts-descriptions": {
5255
"php-cs-fixer:test": "Run php-cs-fixer",
@@ -56,7 +59,9 @@
5659
"phpstan:baseline": "Run phpstan and update baseline",
5760
"phpunit:test": "Run PHPUnit",
5861
"phpunit:coverage": "Run PHPUnit with code coverage (requires XDEBUG enabled)",
59-
"phpunit:coverage-local": "Run PHPUnit with local HTML code coverage (requires XDEBUG enabled)"
62+
"phpunit:coverage-local": "Run PHPUnit with local HTML code coverage (requires XDEBUG enabled)",
63+
"rector:test": "Run rector",
64+
"rector:fix": "Run rector and fix issues"
6065
},
6166
"funding": [
6267
{

src/OpenMage/ComposerPlugin/Copy/AbstractCopyPlugin.php

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function __construct(?Event $event)
5959
public function processComposerInstall(): void
6060
{
6161
$package = $this->getComposerPackage();
62-
if (!$package || !$this instanceof CopyFromComposerInterface) {
62+
if (!$package instanceof BasePackage || !$this instanceof CopyFromComposerInterface) {
6363
return;
6464
}
6565

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

7676
if (!$filesystem->exists($copySourcePath) && $this instanceof CopyFromUnpkgInterface) {
77-
if ($event && $event->getIO()->isVerbose()) {
77+
if ($event instanceof Event && $event->getIO()->isVerbose()) {
7878
$event->getIO()->write(sprintf(
7979
'Fallback to Unpkg %s for %s',
8080
$this->getUnpkgName(),
8181
$this->getComposerName(),
8282
));
8383
}
84+
8485
$this->processUnpkgInstall();
8586
return;
8687
}
@@ -96,11 +97,11 @@ public function processComposerInstall(): void
9697

9798
try {
9899
$filesystem->copy($copySource, $copytarget);
99-
if ($event && $event->getIO()->isVeryVerbose()) {
100+
if ($event instanceof Event && $event->getIO()->isVeryVerbose()) {
100101
$event->getIO()->write(sprintf('Copy %s to %s', $copySource, $copytarget));
101102
}
102103
} catch (IOException $exception) {
103-
if ($event) {
104+
if ($event instanceof Event) {
104105
$event->getIO()->write($exception->getMessage());
105106
}
106107
}
@@ -119,7 +120,7 @@ public function processUnpkgInstall(): void
119120
$event = $this->getEvent();
120121
$sourcePath = $this->getUnpkSourcePath();
121122

122-
if ($event && $event->getIO()->isVerbose()) {
123+
if ($event instanceof Event && $event->getIO()->isVerbose()) {
123124
$event->getIO()->write(sprintf(
124125
'Trying to download %s %s from %s',
125126
$this->getUnpkgName(),
@@ -133,29 +134,32 @@ public function processUnpkgInstall(): void
133134
try {
134135
$content = file_get_contents($sourceFilePath);
135136
} catch (ErrorException $errorException) {
136-
if ($event) {
137+
if ($event instanceof Event) {
137138
$event->getIO()->write($errorException->getMessage());
138139
}
140+
139141
return;
140142
}
141143

142144
if (!$content) {
143-
if ($event) {
145+
if ($event instanceof Event) {
144146
$event->getIO()->write(sprintf('Could not read from %s', $sourceFilePath));
145147
}
148+
146149
return;
147150
}
148151

149152
try {
150153
$targetFilePath = $this->getCopyTargetPath() . '/' . $fileName;
151154
$this->getFileSystem()->dumpFile($targetFilePath, $content);
152-
if ($event && $event->getIO()->isVerbose()) {
155+
if ($event instanceof Event && $event->getIO()->isVerbose()) {
153156
$event->getIO()->write(sprintf('Added %s', $targetFilePath));
154157
}
155158
} catch (IOException $exception) {
156-
if ($event) {
159+
if ($event instanceof Event) {
157160
$event->getIO()->write($exception->getMessage());
158161
}
162+
159163
return;
160164
}
161165
}
@@ -169,27 +173,29 @@ public function getComposerPackage(): ?BasePackage
169173

170174
$vendorName = $this->getComposerName();
171175
$module = $this->getInstalledComposerPackage($vendorName);
172-
if ($module) {
176+
if ($module instanceof BasePackage) {
173177
return $module;
174178
}
175179

176180
$event = $this->getEvent();
177-
if (!$event) {
181+
if (!$event instanceof Event) {
178182
return null;
179183
}
180184

181185
$locker = $event->getComposer()->getLocker();
182-
$repo = $locker->getLockedRepository();
186+
$lockArrayRepository = $locker->getLockedRepository();
183187

184-
foreach ($repo->getPackages() as $package) {
185-
if ($package->getName() === $vendorName) {
186-
$this->setInstalledComposerPackage($vendorName, $package);
188+
foreach ($lockArrayRepository->getPackages() as $basePackage) {
189+
if ($basePackage->getName() === $vendorName) {
190+
$this->setInstalledComposerPackage($vendorName, $basePackage);
187191
if ($event->getIO()->isVerbose()) {
188-
$event->getIO()->write(sprintf('%s found with version %s', $vendorName, $package->getVersion()));
192+
$event->getIO()->write(sprintf('%s found with version %s', $vendorName, $basePackage->getVersion()));
189193
}
194+
190195
return $this->getInstalledComposerPackage($vendorName);
191196
}
192197
}
198+
193199
return null;
194200
}
195201

@@ -202,8 +208,9 @@ protected function getUnpkSourcePath(): string
202208
$search = ['{{package}}', '{{version}}'];
203209
$replace = [$this->getUnpkgName(), $this->getUnpkgVersion()];
204210
$path = str_replace($search, $replace, CopyFromUnpkgInterface::UNPKG_URL);
205-
return $path . ($this->getUnpkgSource() ? $this->getUnpkgSource() . '/' : '');
211+
return $path . ($this->getUnpkgSource() !== '' && $this->getUnpkgSource() !== '0' ? $this->getUnpkgSource() . '/' : '');
206212
}
213+
207214
return '';
208215
}
209216

@@ -216,6 +223,7 @@ protected function getCwd(): string
216223
if ($cwd === false) {
217224
throw new Exception('This should not happen.');
218225
}
226+
219227
return $cwd;
220228
}
221229

@@ -225,7 +233,7 @@ protected function getCwd(): string
225233
protected function getVendorDirectoryFromComposer(): string
226234
{
227235
$event = $this->getEvent();
228-
if (!$event) {
236+
if (!$event instanceof Event) {
229237
return '';
230238
}
231239

@@ -240,22 +248,20 @@ protected function getVendorDirectoryFromComposer(): string
240248
protected function getMageRootDirectoryFromComposer(): string
241249
{
242250
$event = $this->getEvent();
243-
if (!$event) {
251+
if (!$event instanceof Event) {
244252
return '';
245253
}
246254

247255
$composerExtra = $event->getComposer()->getPackage()->getExtra();
248-
$magentoRootDir = '';
249-
250256
if (array_key_exists(self::EXTRA_MAGENTO_ROOT_DIR, $composerExtra) &&
251-
$composerExtra[self::EXTRA_MAGENTO_ROOT_DIR] !== '.'
252-
) {
253-
$magentoRootDir = $composerExtra[self::EXTRA_MAGENTO_ROOT_DIR] . '/';
257+
$composerExtra[self::EXTRA_MAGENTO_ROOT_DIR] !== '.') {
258+
return $composerExtra[self::EXTRA_MAGENTO_ROOT_DIR] . '/';
254259
}
255-
return $magentoRootDir;
260+
261+
return '';
256262
}
257263

258-
private function getCopyTargetPath(): string
264+
protected function getCopyTargetPath(): string
259265
{
260266
return sprintf(
261267
'%s/%s%s',
@@ -270,9 +276,9 @@ protected function getInstalledComposerPackage(string $vendorName): ?BasePackage
270276
return $this->composerPackages[$vendorName] ?? null;
271277
}
272278

273-
private function setInstalledComposerPackage(string $vendorName, BasePackage $package): void
279+
protected function setInstalledComposerPackage(string $vendorName, BasePackage $basePackage): void
274280
{
275-
$this->composerPackages[$vendorName] = $package;
281+
$this->composerPackages[$vendorName] = $basePackage;
276282
}
277283

278284
public function getFileSystem(): Filesystem

src/OpenMage/ComposerPlugin/Copy/CopyInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
interface CopyInterface
1818
{
1919
public const EXTRA_MAGENTO_ROOT_DIR = 'magento-root-dir';
20+
2021
public const EXTRA_UNPKG_PACKAGES = 'openmage-unpkg-packages';
22+
2123
public const VENDOR_DIR = 'vendor-dir';
2224

2325
public function getCopyTarget(): string;

src/OpenMage/ComposerPlugin/Copy/Plugins/TinyMce.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
class TinyMce extends Copy\AbstractCopyPlugin implements Copy\CopyFromComposerInterface
2222
{
2323
public const TINYMCE_LICENSE_FILE = 'LICENSE_TINYMCE.txt';
24+
2425
public const TINYMCE_LICENSE_NOTE = 'LICENSE_TINYMCE_OPENMAGE.txt';
2526

2627
public const TINYMCE_LICENSE_FILE_TEXT = <<<TEXT
@@ -94,9 +95,9 @@ private function addTinyMceLicenseFile(): void
9495
if (!is_null($this->event) && $this->event->getIO()->isVerbose()) {
9596
$this->event->getIO()->write(sprintf('Added %s', self::TINYMCE_LICENSE_FILE));
9697
}
97-
} catch (IOException $exception) {
98+
} catch (IOException $ioException) {
9899
if (!is_null($this->event)) {
99-
$this->event->getIO()->write($exception->getMessage());
100+
$this->event->getIO()->write($ioException->getMessage());
100101
}
101102
}
102103
}
@@ -115,9 +116,9 @@ private function addTinyMceLicenseNote(): void
115116
if (!is_null($this->event) && $this->event->getIO()->isVerbose()) {
116117
$this->event->getIO()->write(sprintf('Added %s', self::TINYMCE_LICENSE_NOTE));
117118
}
118-
} catch (IOException $exception) {
119+
} catch (IOException $ioException) {
119120
if (!is_null($this->event)) {
120-
$this->event->getIO()->write($exception->getMessage());
121+
$this->event->getIO()->write($ioException->getMessage());
121122
}
122123
}
123124
}
@@ -134,9 +135,9 @@ private function removedTinyMceLicenseFiles(): void
134135
if (!is_null($this->event) && $this->event->getIO()->isVeryVerbose()) {
135136
$this->event->getIO()->write(sprintf('Removed %s and %s', self::TINYMCE_LICENSE_FILE, self::TINYMCE_LICENSE_NOTE));
136137
}
137-
} catch (IOException $exception) {
138+
} catch (IOException $ioException) {
138139
if (!is_null($this->event)) {
139-
$this->event->getIO()->write($exception->getMessage());
140+
$this->event->getIO()->write($ioException->getMessage());
140141
}
141142
}
142143
}

src/OpenMage/ComposerPlugin/Copy/Plugins/TinyMceLanguages.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function processComposerInstall(): void
5454
if (!InstalledVersions::isInstalled(self::TINYMCE)) {
5555
return;
5656
}
57+
5758
parent::processComposerInstall();
5859
}
5960
}

0 commit comments

Comments
 (0)