2121use Composer \Factory ;
2222use Composer \Json \JsonFile ;
2323
24+ /**
25+ * Represents a specialized reader for a Composer JSON file.
26+ *
27+ * This class SHALL provide convenient accessors for commonly used
28+ * `composer.json` metadata after reading and caching the file contents.
29+ * Consumers SHOULD use this class when they need normalized access to
30+ * package-level metadata. The internal data cache MUST reflect the
31+ * contents returned by the underlying JSON file reader at construction
32+ * time.
33+ */
2434final class ComposerJson extends JsonFile
2535{
36+ /**
37+ * Stores the decoded Composer JSON document contents.
38+ *
39+ * This property MUST contain the data read from the target Composer
40+ * file during construction. Consumers SHOULD treat the structure as
41+ * internal implementation detail and SHALL rely on accessor methods
42+ * instead of direct access.
43+ *
44+ * @var array<string, mixed>
45+ */
2646 private array $ data ;
2747
2848 /**
29- * @param string|null $path
49+ * Initializes the Composer JSON reader.
50+ *
51+ * When no path is provided, the default Composer file location
52+ * returned by Composer's factory SHALL be used. The constructor MUST
53+ * immediately read and cache the JSON document contents so that
54+ * subsequent accessor methods can operate on the in-memory data.
55+ *
56+ * @param string|null $path The absolute or relative path to a
57+ * Composer JSON file. When omitted, the
58+ * default Composer file path SHALL be used.
3059 */
3160 public function __construct (?string $ path = null )
3261 {
@@ -35,23 +64,45 @@ public function __construct(?string $path = null)
3564 }
3665
3766 /**
38- * @return string
67+ * Returns the package name declared in the Composer file.
68+ *
69+ * This method SHALL return the value of the `name` key when present.
70+ * If the package name is not defined, the method MUST return an
71+ * empty string.
72+ *
73+ * @return string the package name, or an empty string when undefined
3974 */
4075 public function getPackageName (): string
4176 {
4277 return $ this ->data ['name ' ] ?? '' ;
4378 }
4479
4580 /**
46- * @return string
81+ * Returns the package description declared in the Composer file.
82+ *
83+ * This method SHALL return the value of the `description` key when
84+ * present. If the description is not defined, the method MUST return
85+ * an empty string.
86+ *
87+ * @return string the package description, or an empty string when
88+ * undefined
4789 */
4890 public function getPackageDescription (): string
4991 {
5092 return $ this ->data ['description ' ] ?? '' ;
5193 }
5294
5395 /**
54- * @return string|null
96+ * Returns the package license when it can be resolved to a single value.
97+ *
98+ * This method SHALL return the `license` value directly when it is a
99+ * string. When the license is an array containing exactly one item,
100+ * that single item SHALL be returned. When the license field is not
101+ * present, is empty, or cannot be resolved to exactly one string
102+ * value, the method MUST return null.
103+ *
104+ * @return string|null the resolved license identifier, or null when
105+ * no single license value can be determined
55106 */
56107 public function getPackageLicense (): ?string
57108 {
@@ -69,25 +120,47 @@ public function getPackageLicense(): ?string
69120 }
70121
71122 /**
72- * @return array
123+ * Returns the package authors declared in the Composer file.
124+ *
125+ * This method SHALL return the value of the `authors` key when
126+ * present. If the key is absent, the method MUST return an empty
127+ * array.
128+ *
129+ * @return array the authors list as declared in the Composer file,
130+ * or an empty array when undefined
73131 */
74132 public function getAuthors (): array
75133 {
76134 return $ this ->data ['authors ' ] ?? [];
77135 }
78136
79137 /**
80- * @return array
138+ * Returns the extra configuration section declared in the Composer file.
139+ *
140+ * This method SHALL return the value of the `extra` key when present.
141+ * If the key is absent, the method MUST return an empty array.
142+ *
143+ * @return array the extra configuration data, or an empty array when
144+ * undefined
81145 */
82146 public function getExtra (): array
83147 {
84148 return $ this ->data ['extra ' ] ?? [];
85149 }
86150
87151 /**
88- * @param string $type
152+ * Returns the autoload configuration for the requested autoload type.
153+ *
154+ * This method SHALL inspect the `autoload` section and return the
155+ * nested configuration for the requested type, such as `psr-4`.
156+ * When the `autoload` section or the requested type is not defined,
157+ * the method MUST return an empty array.
158+ *
159+ * @param string $type The autoload mapping type to retrieve. This
160+ * defaults to `psr-4`.
89161 *
90- * @return array
162+ * @return array the autoload configuration for the requested type,
163+ * or an empty array when unavailable
91164 */
92165 public function getAutoload (string $ type = 'psr-4 ' ): array
93166 {
0 commit comments