From fa5b8b47d6dcd42614332f4c6cab756aefc77d4e Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Wed, 2 Mar 2022 12:34:00 +0100 Subject: [PATCH 1/6] Make sure "name" is generated for LibraryProperties --- src/Properties/LibraryProperties.php | 30 +++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Properties/LibraryProperties.php b/src/Properties/LibraryProperties.php index 0c41ea0..6003353 100644 --- a/src/Properties/LibraryProperties.php +++ b/src/Properties/LibraryProperties.php @@ -79,8 +79,10 @@ public static function new(string $composerJsonFile, ?string $baseUrl = null): L $properties[self::PROP_VERSION] = $version; } - $baseName = static::buildBaseName((string) $composerJsonData['name']); - $basePath = dirname($composerJsonFile); + [$basePath, $baseName, $name] = static::buildNames($composerJsonData, $composerJsonFile); + if (empty($properties[self::PROP_NAME])) { + $properties[self::PROP_NAME] = $name; + } return new self( $baseName, @@ -91,15 +93,29 @@ public static function new(string $composerJsonFile, ?string $baseUrl = null): L } /** - * @param string $packageName + * @param array $composerJsonData + * @param string $composerJsonFile * - * @return string + * @return array{string, string, string} */ - private static function buildBaseName(string $packageName): string + private static function buildNames(array $composerJsonData, string $composerJsonFile): array { - $packageNamePieces = explode('/', $packageName, 2); + $composerName = (string) ($composerJsonData['name'] ?? ''); + $basePath = dirname($composerJsonFile); + + $packageNamePieces = explode('/', $composerName, 2); + if (empty($packageNamePieces[1])) { + return [$basePath, $composerName, $composerName]; + } + + $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 [$basePath, $basename, $name]; } /** From 357d87ff117bdb2aecd5957e76c876117e96c19f Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Wed, 2 Mar 2022 12:36:49 +0100 Subject: [PATCH 2/6] Add tests for calculated LibraryProperties name --- tests/unit/Properties/LibraryPropertiesTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/unit/Properties/LibraryPropertiesTest.php b/tests/unit/Properties/LibraryPropertiesTest.php index c491c45..384e2fa 100644 --- a/tests/unit/Properties/LibraryPropertiesTest.php +++ b/tests/unit/Properties/LibraryPropertiesTest.php @@ -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, ]; @@ -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()); } /** From e3c5f9e449c699882947c2e6641a7c14495014d9 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Wed, 2 Mar 2022 12:47:44 +0100 Subject: [PATCH 3/6] Improve generated name for libs without vendor --- src/Properties/LibraryProperties.php | 4 ---- tests/unit/Properties/LibraryPropertiesTest.php | 8 +++++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Properties/LibraryProperties.php b/src/Properties/LibraryProperties.php index 6003353..11e52df 100644 --- a/src/Properties/LibraryProperties.php +++ b/src/Properties/LibraryProperties.php @@ -104,10 +104,6 @@ private static function buildNames(array $composerJsonData, string $composerJson $basePath = dirname($composerJsonFile); $packageNamePieces = explode('/', $composerName, 2); - if (empty($packageNamePieces[1])) { - return [$basePath, $composerName, $composerName]; - } - $basename = implode('-', $packageNamePieces); // "inpsyde/foo-bar-baz" => "Inpsyde Foo Bar Baz" $name = mb_convert_case( diff --git a/tests/unit/Properties/LibraryPropertiesTest.php b/tests/unit/Properties/LibraryPropertiesTest.php index 384e2fa..3b6c5cf 100644 --- a/tests/unit/Properties/LibraryPropertiesTest.php +++ b/tests/unit/Properties/LibraryPropertiesTest.php @@ -73,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 = [ @@ -87,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()); } /** From 9b73d04b4f6af77342d161a7f2f5b5707eb6b959 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Wed, 9 Mar 2022 13:33:44 +0100 Subject: [PATCH 4/6] Don't calculate basePath in LibraryProperties::buildNames --- src/Properties/LibraryProperties.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Properties/LibraryProperties.php b/src/Properties/LibraryProperties.php index 11e52df..b68795d 100644 --- a/src/Properties/LibraryProperties.php +++ b/src/Properties/LibraryProperties.php @@ -79,7 +79,8 @@ public static function new(string $composerJsonFile, ?string $baseUrl = null): L $properties[self::PROP_VERSION] = $version; } - [$basePath, $baseName, $name] = static::buildNames($composerJsonData, $composerJsonFile); + $basePath = dirname($composerJsonFile); + [$baseName, $name] = static::buildNames($composerJsonData, $composerJsonFile); if (empty($properties[self::PROP_NAME])) { $properties[self::PROP_NAME] = $name; } @@ -94,15 +95,11 @@ public static function new(string $composerJsonFile, ?string $baseUrl = null): L /** * @param array $composerJsonData - * @param string $composerJsonFile * - * @return array{string, string, string} + * @return array{string, string} */ - private static function buildNames(array $composerJsonData, string $composerJsonFile): array + private static function buildNames(array $composerJsonData): array { - $composerName = (string) ($composerJsonData['name'] ?? ''); - $basePath = dirname($composerJsonFile); - $packageNamePieces = explode('/', $composerName, 2); $basename = implode('-', $packageNamePieces); // "inpsyde/foo-bar-baz" => "Inpsyde Foo Bar Baz" @@ -111,7 +108,7 @@ private static function buildNames(array $composerJsonData, string $composerJson MB_CASE_TITLE ); - return [$basePath, $basename, $name]; + return [$basename, $name]; } /** From 7c1d1a1c3ab9e357b9401d95853ebe72c23b4655 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Wed, 9 Mar 2022 13:35:15 +0100 Subject: [PATCH 5/6] Revert "Don't calculate basePath in LibraryProperties::buildNames" This reverts commit 9b73d04b4f6af77342d161a7f2f5b5707eb6b959. --- src/Properties/LibraryProperties.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Properties/LibraryProperties.php b/src/Properties/LibraryProperties.php index b68795d..11e52df 100644 --- a/src/Properties/LibraryProperties.php +++ b/src/Properties/LibraryProperties.php @@ -79,8 +79,7 @@ public static function new(string $composerJsonFile, ?string $baseUrl = null): L $properties[self::PROP_VERSION] = $version; } - $basePath = dirname($composerJsonFile); - [$baseName, $name] = static::buildNames($composerJsonData, $composerJsonFile); + [$basePath, $baseName, $name] = static::buildNames($composerJsonData, $composerJsonFile); if (empty($properties[self::PROP_NAME])) { $properties[self::PROP_NAME] = $name; } @@ -95,11 +94,15 @@ public static function new(string $composerJsonFile, ?string $baseUrl = null): L /** * @param array $composerJsonData + * @param string $composerJsonFile * - * @return array{string, string} + * @return array{string, string, string} */ - private static function buildNames(array $composerJsonData): array + private static function buildNames(array $composerJsonData, string $composerJsonFile): array { + $composerName = (string) ($composerJsonData['name'] ?? ''); + $basePath = dirname($composerJsonFile); + $packageNamePieces = explode('/', $composerName, 2); $basename = implode('-', $packageNamePieces); // "inpsyde/foo-bar-baz" => "Inpsyde Foo Bar Baz" @@ -108,7 +111,7 @@ private static function buildNames(array $composerJsonData): array MB_CASE_TITLE ); - return [$basename, $name]; + return [$basePath, $basename, $name]; } /** From 2455d05096b29b4b710d02f7a9f0dcac959b4c2a Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Wed, 9 Mar 2022 13:36:32 +0100 Subject: [PATCH 6/6] Don't calculate basePath in LibraryProperties::buildNames --- src/Properties/LibraryProperties.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Properties/LibraryProperties.php b/src/Properties/LibraryProperties.php index 11e52df..77539dd 100644 --- a/src/Properties/LibraryProperties.php +++ b/src/Properties/LibraryProperties.php @@ -79,7 +79,8 @@ public static function new(string $composerJsonFile, ?string $baseUrl = null): L $properties[self::PROP_VERSION] = $version; } - [$basePath, $baseName, $name] = static::buildNames($composerJsonData, $composerJsonFile); + [$baseName, $name] = static::buildNames($composerJsonData); + $basePath = dirname($composerJsonFile); if (empty($properties[self::PROP_NAME])) { $properties[self::PROP_NAME] = $name; } @@ -94,15 +95,12 @@ public static function new(string $composerJsonFile, ?string $baseUrl = null): L /** * @param array $composerJsonData - * @param string $composerJsonFile * - * @return array{string, string, string} + * @return array{string, string} */ - private static function buildNames(array $composerJsonData, string $composerJsonFile): array + private static function buildNames(array $composerJsonData): array { $composerName = (string) ($composerJsonData['name'] ?? ''); - $basePath = dirname($composerJsonFile); - $packageNamePieces = explode('/', $composerName, 2); $basename = implode('-', $packageNamePieces); // "inpsyde/foo-bar-baz" => "Inpsyde Foo Bar Baz" @@ -111,7 +109,7 @@ private static function buildNames(array $composerJsonData, string $composerJson MB_CASE_TITLE ); - return [$basePath, $basename, $name]; + return [$basename, $name]; } /**