From 8da283adf74e66ed9263118b04138f392118c91f Mon Sep 17 00:00:00 2001 From: Harmen Janssen Date: Wed, 4 Jul 2018 07:56:04 +0200 Subject: [PATCH] Normalise vendor directory containing hyphen Vendors on Packagist are allowed a hyphen in their name (such as https://packagist.org/packages/grrr-amsterdam/). However, when translating this directory structure into a PHP namespace, it will result in an illegal namespace. This update removes anything from the vendor name that's not alphanumeric or an underscore. --- src/Composer/Installers/OctoberInstaller.php | 1 + .../Installers/Test/OctoberInstallerTest.php | 28 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Composer/Installers/OctoberInstaller.php b/src/Composer/Installers/OctoberInstaller.php index 93b25de5..08d5dc4e 100644 --- a/src/Composer/Installers/OctoberInstaller.php +++ b/src/Composer/Installers/OctoberInstaller.php @@ -33,6 +33,7 @@ public function inflectPackageVars($vars) protected function inflectPluginVars($vars) { $vars['name'] = preg_replace('/^oc-|-plugin$/', '', $vars['name']); + $vars['vendor'] = preg_replace('/[^a-z0-9_]/i', '', $vars['vendor']); return $vars; } diff --git a/tests/Composer/Installers/Test/OctoberInstallerTest.php b/tests/Composer/Installers/Test/OctoberInstallerTest.php index 6e615c77..d75f1fb7 100644 --- a/tests/Composer/Installers/Test/OctoberInstallerTest.php +++ b/tests/Composer/Installers/Test/OctoberInstallerTest.php @@ -24,11 +24,15 @@ public function setUp() /** * @dataProvider packageNameInflectionProvider */ - public function testInflectPackageVars($type, $name, $expected) + public function testInflectPackageVars($type, $vendor, $name, $expectedVendor, $expectedName) { $this->assertEquals( - $this->installer->inflectPackageVars(array('name' => $name, 'type' => $type)), - array('name' => $expected, 'type' => $type) + $this->installer->inflectPackageVars(array( + 'vendor' => $vendor, + 'name' => $name, + 'type' => $type + )), + array('vendor' => $expectedVendor, 'name' => $expectedName, 'type' => $type) ); } @@ -37,29 +41,47 @@ public function packageNameInflectionProvider() return array( array( 'october-plugin', + 'acme', 'subpagelist', + 'acme', 'subpagelist', ), array( 'october-plugin', + 'acme', 'subpagelist-plugin', + 'acme', 'subpagelist', ), array( 'october-plugin', + 'acme', 'semanticoctober', + 'acme', 'semanticoctober', ), + // tests vendor name containing a hyphen + array( + 'october-plugin', + 'foo-bar-co', + 'blog', + 'foobarco', + 'blog' + ), // tests that exactly one '-theme' is cut off array( 'october-theme', + 'acme', 'some-theme-theme', + 'acme', 'some-theme', ), // tests that names without '-theme' suffix stay valid array( 'october-theme', + 'acme', 'someothertheme', + 'acme', 'someothertheme', ), );