Skip to content

Commit

Permalink
chore: composer dependency analyser tool (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru authored Mar 16, 2024
2 parents 27da03c + 98bd58b commit 5192462
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
- name: Check package versions
run: |
composer run-script monorepo-builder
- name: Check dependencies
run: |
composer run-script composer-dependency-analyser
- name: Check packages
run: |
composer run-script composer-require-checker
Expand Down Expand Up @@ -66,6 +69,9 @@ jobs:
uses: ramsey/composer-install@v2
with:
working-directory: ${{ needs.settings.outputs.packages-directory }}/${{ matrix.package }}
- name: Check dependencies
run: |
composer run-script composer-dependency-analyser -- --composer-json="${{ needs.settings.outputs.packages-directory }}/${{ matrix.package }}/composer.json"
- name: Check packages
run: |
composer run-script composer-require-checker -- "${{ needs.settings.outputs.packages-directory }}/${{ matrix.package }}/composer.json"
Expand Down
76 changes: 76 additions & 0 deletions composer-dependency-analyser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php declare(strict_types = 1);

use LastDragon_ru\LaraASP\Dev\App\Example;
use ShipMonk\ComposerDependencyAnalyser\CliOptions;
use ShipMonk\ComposerDependencyAnalyser\ComposerJson;
use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;
use ShipMonk\ComposerDependencyAnalyser\Initializer;
use ShipMonk\ComposerDependencyAnalyser\Path;

// Assertions
assert(isset($this) && $this instanceof Initializer);
assert(isset($options) && $options instanceof CliOptions);
assert(isset($composerJson) && $composerJson instanceof ComposerJson);

// General
$config = (new Configuration())
->enableAnalysisOfUnusedDevDependencies()
->disableReportingUnmatchedIgnores()
->ignoreUnknownClasses([
Example::class,
])
->ignoreErrorsOnPackage('symfony/polyfill-php83', [ErrorType::UNUSED_DEPENDENCY])
->ignoreErrorsOnPackage('bamarni/composer-bin-plugin', [ErrorType::UNUSED_DEPENDENCY])
->ignoreErrorsOnPackage('orchestra/testbench', [ErrorType::UNUSED_DEPENDENCY])
->ignoreErrorsOnPackage('orchestra/testbench-core', [ErrorType::SHADOW_DEPENDENCY])
->ignoreErrorsOnPackage('phpstan/phpstan', [ErrorType::SHADOW_DEPENDENCY])
->ignoreErrorsOnPackage('laravel/scout', [ErrorType::DEV_DEPENDENCY_IN_PROD]);

// Configure paths
//
// In our case, tests located inside the same directory with class and
// `exclude-from-classmap` is used to exclude them from the class map.
// So we need to mark these excluded files as "dev".
$path = Path::resolve($this->cwd, ($options->composerJson ?? 'composer.json'));
$json = (string) file_get_contents($path);
$json = json_decode($json, true, JSON_THROW_ON_ERROR);
$excluded = $json['autoload']['exclude-from-classmap'] ?? [];

if ($excluded) {
$config = $config->disableComposerAutoloadPathScan();
$regexp = array_map(
static function (string $exclude) use ($path): string {
// Similar to how composer process it, but not the exact match.
$exclude = dirname($path)."/{$exclude}";
$exclude = preg_replace('{/+}', '/', preg_quote(trim(strtr($exclude, '\\', '/'), '/')));
$exclude = strtr($exclude, ['\\*\\*' => '.+?', '\\*' => '[^/]+?']);

return $exclude;
},
$excluded,
);
$regexp = '{('.implode(')|(', $regexp).')}';

foreach ($composerJson->autoloadPaths as $absolutePath => $isDevPath) {
if ($isDevPath) {
$config = $config->addPathToScan($absolutePath, $isDevPath);
} elseif (is_file($absolutePath)) {
$config = $config->addPathToScan($absolutePath, (bool) preg_match($regexp, $absolutePath));
} else {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($absolutePath));

foreach ($iterator as $entry) {
if (!$entry->isFile() || !in_array($entry->getExtension(), $config->getFileExtensions(), true)) {
continue;
}

$entryPath = $entry->getPathname();
$config = $config->addPathToScan($entryPath, (bool) preg_match($regexp, $entryPath));
}
}
}
}

// Return
return $config;
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@
],
"validate:dependencies": [
"@monorepo-builder",
"@composer-unused",
"@composer-require-checker"
"@composer-dependency-analyser",
"@composer-require-checker",
"@composer-unused"
],
"phpstan": [
"./vendor-bin/phpstan/vendor/bin/phpstan analyse"
Expand All @@ -187,6 +188,9 @@
"composer-require-checker": [
"./vendor-bin/composer-require-checker/vendor/bin/composer-require-checker check"
],
"composer-dependency-analyser": [
"vendor-bin/composer-dependency-analyser/vendor/bin/composer-dependency-analyser --verbose"
],
"monorepo-builder": [
"./vendor-bin/monorepo-builder/vendor/bin/monorepo-builder validate"
],
Expand Down
3 changes: 1 addition & 2 deletions dev/App/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Laravel\Prompts\Output\ConsoleOutput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down Expand Up @@ -38,7 +37,7 @@ public static function run(InputInterface $input, OutputInterface $output = null

$app = static::create();
$kernel = $app->make(ConsoleKernelContract::class);
$status = $kernel->handle($input, $output ?? new ConsoleOutput());
$status = $kernel->handle($input, $output);

$kernel->terminate($input, $status);

Expand Down
1 change: 1 addition & 0 deletions packages/core/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"require-dev": {
"phpunit/phpunit": "^10.1.0",
"lastdragon-ru/lara-asp-testing": "self.version",
"mockery/mockery": "^1.6.2",
"orchestra/testbench": "^8.0.0"
},
"autoload": {
Expand Down
1 change: 1 addition & 0 deletions packages/documentator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"require-dev": {
"lastdragon-ru/lara-asp-testing": "self.version",
"mockery/mockery": "^1.6.2",
"orchestra/testbench": "^8.0.0",
"phpunit/phpunit": "^10.1.0"
},
Expand Down
1 change: 1 addition & 0 deletions packages/eloquent/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"require-dev": {
"ext-pdo_sqlite": "*",
"lastdragon-ru/lara-asp-testing": "self.version",
"mockery/mockery": "^1.6.2",
"orchestra/testbench": "^8.0.0",
"phpunit/phpunit": "^10.1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/migrator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"ext-json": "*",
"laravel/framework": "^10.34.0",
"lastdragon-ru/lara-asp-core": "self.version",
"symfony/filesystem": "^6.3.0",
"symfony/console": "^6.3.0",
"symfony/finder": "^6.3.0",
"symfony/polyfill-php83": "^1.28"
},
Expand Down
1 change: 1 addition & 0 deletions packages/serializer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"require-dev": {
"lastdragon-ru/lara-asp-testing": "self.version",
"mockery/mockery": "^1.6.2",
"orchestra/testbench": "^8.0.0",
"phpunit/phpunit": "^10.1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/spa/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"require-dev": {
"phpunit/phpunit": "^10.1.0",
"lastdragon-ru/lara-asp-testing": "self.version",
"symfony/filesystem": "^6.3.0",
"mockery/mockery": "^1.6.2",
"orchestra/testbench": "^8.0.0"
},
"autoload": {
Expand Down
4 changes: 3 additions & 1 deletion packages/testing/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
"require-dev": {
"guzzlehttp/psr7": "^1.9.1|^2.4.5",
"laravel/scout": "^9.8.0|^10.0.0",
"orchestra/testbench": "^8.0.0"
"orchestra/testbench": "^8.0.0",
"symfony/console": "^6.3.0",
"symfony/http-kernel": "^6.3.0"
},
"autoload": {
"files": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Builder;
use LastDragon_ru\LaraASP\GraphQL\Testing\Package\Requirements\RequiresLaravelScout;
use LastDragon_ru\LaraASP\Testing\Assertions\ScoutAssertions;
use LastDragon_ru\LaraASP\Testing\Requirements\Requirements\RequiresComposerPackage;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\Attributes\CoversNothing;

/**
* @internal
*/
#[CoversNothing]
#[RequiresLaravelScout]
#[RequiresComposerPackage('laravel/scout')]
final class AssertScoutQueryEqualsTest extends TestCase {
/**
* Trait where assertion defined.
Expand Down
5 changes: 5 additions & 0 deletions vendor-bin/composer-dependency-analyser/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require-dev": {
"shipmonk/composer-dependency-analyser": "dev-master"
}
}
84 changes: 84 additions & 0 deletions vendor-bin/composer-dependency-analyser/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5192462

Please sign in to comment.