2323
2424use function Safe \json_decode ;
2525
26+ /**
27+ * Reads composer.json and exposes metadata for license generation.
28+ *
29+ * This class parses a composer.json file via SplFileObject and provides
30+ * methods to extract license information, package name, authors, vendor,
31+ * and the current year for copyright notices.
32+ */
2633final readonly class Reader implements ReaderInterface
2734{
2835 private array $ data ;
2936
3037 /**
38+ * Creates a new Reader instance.
39+ *
3140 * @param SplFileObject $source The source file to read from, typically composer.json
41+ *
42+ * @throws JsonException if the JSON content is invalid
3243 */
3344 public function __construct (SplFileObject $ source )
3445 {
3546 $ this ->data = $ this ->readData ($ source );
3647 }
3748
3849 /**
39- * @param SplFileObject $source The source file to read from, typically composer.json
50+ * Reads and parses the JSON content from the source file.
51+ *
52+ * @param SplFileObject $source The source file to read from
4053 *
41- * @return array
54+ * @return array The parsed JSON data as an associative array
4255 *
4356 * @throws JsonException if the JSON is invalid
4457 */
@@ -50,7 +63,13 @@ private function readData(SplFileObject $source): array
5063 }
5164
5265 /**
53- * @return string|null
66+ * Retrieves the license identifier from composer.json.
67+ *
68+ * If the license is a single string, returns it directly.
69+ * If it's an array with one element, extracts that element.
70+ * Returns null if no license is set or if multiple licenses are specified.
71+ *
72+ * @return string|null the license string, or null if not set or unsupported
5473 */
5574 public function getLicense (): ?string
5675 {
@@ -64,15 +83,22 @@ public function getLicense(): ?string
6483 }
6584
6685 /**
67- * @return string
86+ * Retrieves the package name from composer.json.
87+ *
88+ * @return string the full package name (vendor/package), or empty string if not set
6889 */
6990 public function getPackageName (): string
7091 {
7192 return $ this ->data ['name ' ] ?? '' ;
7293 }
7394
7495 /**
75- * @return array
96+ * Retrieves the list of authors from composer.json.
97+ *
98+ * Each author is normalized to include name, email, homepage, and role fields.
99+ * Returns an empty array if no authors are defined.
100+ *
101+ * @return array<int, array{name: string, email: string, homepage: string, role: string}>
76102 */
77103 public function getAuthors (): array
78104 {
@@ -94,7 +120,12 @@ public function getAuthors(): array
94120 }
95121
96122 /**
97- * @return string|null
123+ * Extracts the vendor name from the package name.
124+ *
125+ * The package name is expected in vendor/package format.
126+ * Returns null if no package name is set or if the package has no vendor prefix.
127+ *
128+ * @return string|null the vendor name, or null if package has no vendor prefix
98129 */
99130 public function getVendor (): ?string
100131 {
@@ -114,17 +145,24 @@ public function getVendor(): ?string
114145 }
115146
116147 /**
117- * @return int
148+ * Returns the current year for copyright notices.
149+ *
150+ * @return int the current year as an integer
118151 */
119152 public function getYear (): int
120153 {
121154 return (int ) date ('Y ' );
122155 }
123156
124157 /**
125- * @param array $license
158+ * Extracts a single license from an array of licenses.
159+ *
160+ * Returns the first license if exactly one element exists.
161+ * Returns null if the array is empty or contains multiple licenses.
162+ *
163+ * @param array<string> $license The license array to extract from
126164 *
127- * @return string|null
165+ * @return string|null a single license string, or null if extraction is not possible
128166 */
129167 private function extractLicense (array $ license ): ?string
130168 {
0 commit comments