Skip to content

Commit

Permalink
Normalise vendor directory containing hyphen
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
harmenjanssen committed Jul 4, 2018
1 parent 5d15e4e commit 8da283a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Composer/Installers/OctoberInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
28 changes: 25 additions & 3 deletions tests/Composer/Installers/Test/OctoberInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand All @@ -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',
),
);
Expand Down

0 comments on commit 8da283a

Please sign in to comment.