Skip to content

Commit 6f870d8

Browse files
authored
[ci] add autoremoval of unused deps (#8051)
1 parent f159245 commit 6f870d8

4 files changed

Lines changed: 92 additions & 4 deletions

File tree

‎.github/workflows/code_analysis.yaml‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ jobs:
7171
name: 'Check no "*.php" files in rules Fixture directory'
7272
run: php scripts/no-php-file-in-fixtures.php
7373

74-
-
75-
name: 'Detect composer dependency issues'
76-
run: vendor/bin/composer-dependency-analyser
77-
7874
-
7975
name: "PHP Linter"
8076
run: |
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Remove Unused Dependencies
2+
3+
on:
4+
pull_request: null
5+
6+
jobs:
7+
remove_unused_deps:
8+
strategy:
9+
fail-fast: false
10+
11+
runs-on: ubuntu-latest
12+
timeout-minutes: 8
13+
14+
if: github.event.pull_request.head.repo.full_name == 'rectorphp/rector-src'
15+
steps:
16+
-
17+
uses: actions/checkout@v4
18+
with:
19+
# Must be used to trigger workflow after push
20+
token: ${{ secrets.ACCESS_TOKEN }}
21+
22+
-
23+
uses: shivammathur/setup-php@v2
24+
with:
25+
php-version: 8.3
26+
coverage: none
27+
28+
- run: composer install --no-progress --ansi
29+
30+
## detect unused dependencies and remove them from composer.json
31+
- run: php scripts/remove-unused-deps.php --ansi
32+
33+
-
34+
# commit only to core contributors who have repository access
35+
uses: stefanzweifel/git-auto-commit-action@v5
36+
with:
37+
commit_message: '[ci-review] Remove unused dependencies'
38+
commit_author: 'GitHub Action <actions@github.com>'
39+
commit_user_email: 'action@github.com'

‎phpstan.neon‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,8 @@ parameters:
445445
-
446446
message: '#Access to deprecated property \$file of class Rector\\Rector\\AbstractRector#'
447447
path: src/Rector/AbstractRector.php
448+
449+
# allowed
450+
-
451+
message: '#"@\\simplexml_load_string\(\$junitXml\)" is forbidden to use#'
452+
path: scripts/remove-unused-deps.php

‎scripts/remove-unused-deps.php‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Symfony\Component\Console\Input\ArrayInput;
6+
use Symfony\Component\Console\Output\ConsoleOutput;
7+
use Symfony\Component\Console\Style\SymfonyStyle;
8+
9+
require __DIR__ . '/../vendor/autoload.php';
10+
11+
$symfonyStyle = new SymfonyStyle(new ArrayInput([]), new ConsoleOutput());
12+
13+
// 1. run the same workflow as CI "Detect composer dependency issues", in machine-readable format
14+
exec(__DIR__ . '/../vendor/bin/composer-dependency-analyser --format junit 2>/dev/null', $outputLines);
15+
$junitXml = implode("\n", $outputLines);
16+
17+
$simpleXml = @simplexml_load_string($junitXml);
18+
if (! $simpleXml instanceof SimpleXMLElement) {
19+
$symfonyStyle->error('Failed to parse composer-dependency-analyser output');
20+
exit(1);
21+
}
22+
23+
// 2. collect unused dependencies from the "unused dependencies" testsuite
24+
$unusedDependencies = [];
25+
foreach ($simpleXml->testsuite as $testsuite) {
26+
if ((string) $testsuite['name'] !== 'unused dependencies') {
27+
continue;
28+
}
29+
30+
foreach ($testsuite->testcase as $testcase) {
31+
$unusedDependencies[] = (string) $testcase['name'];
32+
}
33+
}
34+
35+
if ($unusedDependencies === []) {
36+
$symfonyStyle->success('No unused dependencies found');
37+
exit(0);
38+
}
39+
40+
$symfonyStyle->listing($unusedDependencies);
41+
42+
// 3. remove unused dependencies from composer.json (composer auto-detects require/require-dev)
43+
// the autocommit to the branch is handled by the workflow's git-auto-commit-action
44+
foreach ($unusedDependencies as $unusedDependency) {
45+
exec(sprintf('composer remove %s --no-update --no-interaction 2>&1', escapeshellarg($unusedDependency)));
46+
}
47+
48+
$symfonyStyle->success(sprintf('Removed %d unused dependency(ies) from composer.json', count($unusedDependencies)));

0 commit comments

Comments
 (0)