Skip to content

Commit

Permalink
Allow specifying a list of extensions to ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
mad-briller committed Apr 18, 2023
1 parent 3f3d1ba commit f5e02d4
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 7 deletions.
46 changes: 41 additions & 5 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ env:
COMPOSER_ROOT_VERSION: "1.2.x-dev"

jobs:
test:
integration-test:
name: "Integration test"
runs-on: "ubuntu-latest"

Expand Down Expand Up @@ -40,16 +40,52 @@ jobs:
tools: "composer:${{ matrix.composer-version }}"

- name: "Install dependencies"
working-directory: "e2e"
working-directory: "e2e/integration"
run: "composer install --no-interaction --no-progress --no-suggest"

- name: "Test"
working-directory: "e2e"
working-directory: "e2e/integration"
run: "vendor/bin/phpstan analyse -l 8 FooTest.php"

- name: "Rename directory"
run: "mv e2e e3e"
working-directory: "e2e"
run: "mv integration integration-copy"

- name: "Test relative paths - after renaming"
working-directory: "e3e"
working-directory: "e2e/integration-copy"
run: "vendor/bin/phpstan analyse -l 8 FooTest.php"

ignore-test:
name: "Ignore test"
runs-on: "ubuntu-latest"

strategy:
matrix:
php-version:
- "7.4"
- "8.0"
- "8.1"
- "8.2"
composer-version:
- "v2"
- "preview"
- "snapshot"

steps:
- name: "Checkout"
uses: actions/checkout@v3

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
tools: "composer:${{ matrix.composer-version }}"

- name: "Install dependencies"
working-directory: "e2e/ignore"
run: "composer install --no-interaction --no-progress --no-suggest"

- name: "Test"
working-directory: "e2e/ignore"
run: "vendor/bin/phpstan analyse -l 8 IgnoreTest.php"
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ Add `phpstan` key in the extension `composer.json`'s `extra` section:
}
```

## Ignoring a particular extension

You may want to disable auto-installation of a particular extension to handle installation manually. Ignore an extension by adding an `extra.phpstan/extension-installer.ignore` array in `composer.json` that specifies a list of packages to ignore:

```json
{
"extra": {
"phpstan/extension-installer": {
"ignore": [
"phpstan/phpstan-phpunit"
]
}
}
}
```

## Limitations

The extension installer depends on Composer script events, therefore you cannot use `--no-scripts` flag.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
}
},
"extra": {
"class": "PHPStan\\ExtensionInstaller\\Plugin"
"class": "PHPStan\\ExtensionInstaller\\Plugin",
"phpstan/extension-installer": {
"ignore": ["phpstan/phpstan-phpunit"]
}
},
"autoload": {
"psr-4": {
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions e2e/ignore/IgnoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App;

class IgnoreTest
{
}
35 changes: 35 additions & 0 deletions e2e/ignore/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"require": {
"phpstan/phpstan": "^1.8.0",
"phpstan/extension-installer": "1.2.x-dev",
"testing/test-extension": "^1.0"
},
"repositories": [
{
"type": "path",
"url": "../..",
"options": {
"symlink": false
}
},
{
"type": "path",
"url": "../test-extension",
"options": {
"symlink": false
}
}
],
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
},
"extra": {
"phpstan/extension-installer": {
"ignore": [
"testing/test-extension"
]
}
}
}
2 changes: 2 additions & 0 deletions e2e/integration/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/composer.lock
/vendor
File renamed without changes.
2 changes: 1 addition & 1 deletion e2e/composer.json → e2e/integration/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"repositories": [
{
"type": "path",
"url": "..",
"url": "../..",
"options": {
"symlink": false
}
Expand Down
11 changes: 11 additions & 0 deletions e2e/test-extension/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1.0",
"name": "testing/test-extension",
"extra": {
"phpstan": {
"includes": [
"extension.neon"
]
}
}
}
2 changes: 2 additions & 0 deletions e2e/test-extension/extension.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
invalidConfig: []
20 changes: 20 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use function ksort;
use function md5;
use function md5_file;
use function sort;
use function sprintf;
use function strpos;
use function var_export;
Expand Down Expand Up @@ -97,9 +98,18 @@ public function process(Event $event): void
}
$notInstalledPackages = [];
$installedPackages = [];
$ignoredPackages = [];

$data = [];
$fs = new Filesystem();
$ignore = [];

$packageExtra = $composer->getPackage()->getExtra();

if (isset($packageExtra['phpstan/extension-installer']['ignore'])) {
$ignore = $packageExtra['phpstan/extension-installer']['ignore'];
}

foreach ($composer->getRepositoryManager()->getLocalRepository()->getPackages() as $package) {
if (
$package->getType() !== 'phpstan-extension'
Expand All @@ -119,6 +129,11 @@ public function process(Event $event): void
continue;
}

if (in_array($package->getName(), $ignore, true)) {
$ignoredPackages[] = $package->getName();
continue;
}

$installPath = $installationManager->getInstallPath($package);

$absoluteInstallPath = $fs->isAbsolutePath($installPath)
Expand All @@ -138,6 +153,7 @@ public function process(Event $event): void
ksort($data);
ksort($installedPackages);
ksort($notInstalledPackages);
sort($ignoredPackages);

$generatedConfigFileContents = sprintf(self::$generatedFileTemplate, var_export($data, true), var_export($notInstalledPackages, true));
file_put_contents($generatedConfigFilePath, $generatedConfigFileContents);
Expand All @@ -154,6 +170,10 @@ public function process(Event $event): void
foreach (array_keys($notInstalledPackages) as $name) {
$io->write(sprintf('> <comment>%s:</comment> not supported', $name));
}

foreach ($ignoredPackages as $name) {
$io->write(sprintf('> <comment>%s:</comment> ignored', $name));
}
}

}

0 comments on commit f5e02d4

Please sign in to comment.