Skip to content

Commit

Permalink
Merge pull request #18 from inpsyde/feature/library-calculate-name
Browse files Browse the repository at this point in the history
Make sure "name" is generated for LibraryProperties
  • Loading branch information
Chrico authored Mar 9, 2022
2 parents c2fbaf2 + 2455d05 commit 3bbff61
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
22 changes: 16 additions & 6 deletions src/Properties/LibraryProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ public static function new(string $composerJsonFile, ?string $baseUrl = null): L
$properties[self::PROP_VERSION] = $version;
}

$baseName = static::buildBaseName((string) $composerJsonData['name']);
[$baseName, $name] = static::buildNames($composerJsonData);
$basePath = dirname($composerJsonFile);
if (empty($properties[self::PROP_NAME])) {
$properties[self::PROP_NAME] = $name;
}

return new self(
$baseName,
Expand All @@ -91,15 +94,22 @@ public static function new(string $composerJsonFile, ?string $baseUrl = null): L
}

/**
* @param string $packageName
* @param array $composerJsonData
*
* @return string
* @return array{string, string}
*/
private static function buildBaseName(string $packageName): string
private static function buildNames(array $composerJsonData): array
{
$packageNamePieces = explode('/', $packageName, 2);
$composerName = (string) ($composerJsonData['name'] ?? '');
$packageNamePieces = explode('/', $composerName, 2);
$basename = implode('-', $packageNamePieces);
// "inpsyde/foo-bar-baz" => "Inpsyde Foo Bar Baz"
$name = mb_convert_case(
str_replace(['-', '_', '.'], ' ', implode(' ', $packageNamePieces)),
MB_CASE_TITLE
);

return implode('-', $packageNamePieces);
return [$basename, $name];
}

/**
Expand Down
14 changes: 9 additions & 5 deletions tests/unit/Properties/LibraryPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public function testForLibraryInvalidFile(): void
public function testForLibrary(): void
{
$inputName = 'vendor/test';
$expectedName = "vendor-test";
$expectedBaseName = "vendor-test";
$expectedName = "Vendor Test";
$composerJsonData = [
"name" => $inputName,
];
Expand All @@ -40,7 +41,8 @@ public function testForLibrary(): void

$testee = LibraryProperties::new($root->url() . '/json/composer.json');

static::assertSame($expectedName, $testee->baseName());
static::assertSame($expectedBaseName, $testee->baseName());
static::assertSame($expectedName, $testee->name());
}

/**
Expand Down Expand Up @@ -71,9 +73,10 @@ public function testVersionInRoot(): void
*/
public function testForLibraryWithoutVendor(): void
{
$expectedName = "properties-test";
$expectedBaseName = "properties-test";
$expectedName = "Properties Test";
$composerJsonData = [
"name" => $expectedName,
"name" => $expectedBaseName,
];

$structure = [
Expand All @@ -85,7 +88,8 @@ public function testForLibraryWithoutVendor(): void

$testee = LibraryProperties::new($root->url() . '/json/composer.json');

static::assertSame($expectedName, $testee->baseName());
static::assertSame($expectedBaseName, $testee->baseName());
static::assertSame($expectedName, $testee->name());
}

/**
Expand Down

0 comments on commit 3bbff61

Please sign in to comment.