-
Notifications
You must be signed in to change notification settings - Fork 1
/
composer-dependency-analyser.php
122 lines (106 loc) · 3.58 KB
/
composer-dependency-analyser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php declare(strict_types = 1);
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\RefreshDatabaseState;
use Illuminate\Foundation\Testing\TestCase;
use Orchestra\Testbench\TestCase as TestbenchTestCase;
use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;
use ShipMonk\ComposerDependencyAnalyser\Path;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\Glob;
// General
$config = (new Configuration())
->enableAnalysisOfUnusedDevDependencies()
->ignoreErrorsOnPackage('symfony/polyfill-php83', [ErrorType::UNUSED_DEPENDENCY])
->ignoreErrorsOnPackage('bamarni/composer-bin-plugin', [ErrorType::UNUSED_DEPENDENCY])
->ignoreErrorsOnPackage('laravel/scout', [ErrorType::DEV_DEPENDENCY_IN_PROD])
->ignoreUnknownClasses([
FormRequest::class,
RefreshDatabase::class,
RefreshDatabaseState::class,
TestCase::class,
TestbenchTestCase::class,
]);
// Load composer.json
$path = Path::realpath(getopt('', ['composer-json:'])['composer-json'] ?? 'composer.json');
$root = Path::realpath(dirname(__FILE__).'/composer.json') === $path;
if ($root) {
$config
->ignoreErrorsOnPackage('phpstan/phpstan-mockery', [ErrorType::UNUSED_DEPENDENCY])
->ignoreErrorsOnPackage('phpstan/phpstan-phpunit', [ErrorType::UNUSED_DEPENDENCY])
->ignoreErrorsOnPackage('phpstan/phpstan-strict-rules', [ErrorType::UNUSED_DEPENDENCY])
->ignoreErrorsOnPackage('spaze/phpstan-disallowed-calls', [ErrorType::UNUSED_DEPENDENCY]);
} else {
$config->disableReportingUnmatchedIgnores();
}
// Configure paths
//
// In our case, tests located inside the same directory with class and
// `.gitattributes` is used to exclude them from the release. So we need
// to mark these excluded files as "dev".
$files = Finder::create()
->ignoreDotFiles(false)
->ignoreVCSIgnored(true)
->exclude('node_modules')
->exclude('vendor-bin')
->exclude('vendor')
->exclude('dev')
->in(dirname($path))
->name('.gitattributes')
->files();
$parse = static function (string $line): string {
// Simplified parser
// https://git-scm.com/docs/gitattributes
$line = trim($line);
if (str_starts_with($line, '#')) {
$line = '';
}
if (str_ends_with($line, ' export-ignore')) {
$line = trim(explode(' ', $line, 2)[0] ?? '');
} else {
$line = '';
}
// File?
$line = match (pathinfo($line, PATHINFO_EXTENSION)) {
'' => "{$line}/*.php",
'php' => $line,
default => '',
};
if (!str_contains($line, '*')) {
$line = '';
}
// Convert
if ($line) {
$line = ltrim($line, '/');
$line = Glob::toRegex($line);
}
// Return
return $line;
};
foreach ($files as $file) {
// Parse
$attributes = file($file->getPathname());
$attributes = array_filter(array_map($parse, $attributes));
if (!$attributes) {
continue;
}
// Add as dev
$dependencies = Finder::create()
->ignoreVCSIgnored(true)
->notName('composer-dependency-analyser.php')
->notName('monorepo-builder.php')
->exclude('node_modules')
->exclude('vendor-bin')
->exclude('vendor')
->exclude('dev')
->in($file->getPath())
->path($attributes)
->name('*.php')
->files();
foreach ($dependencies as $dependency) {
$config->addPathToScan($dependency->getPathname(), true);
}
}
// Return
return $config;