Skip to content

🔧 SetProperty

Sets top-level properties of composer.json, such as project name, description, type, keywords, license, etc.

When to use

Use this when you need to initialize or modify project metadata in code. This method performs type-safe assignment against a whitelist of properties, avoiding hand-written JSON. For keys inside the config section, use SetConfig instead.

Signature

go
func (c *Composer) SetProperty(property string, value interface{}) error

Parameters

ParameterTypeDescription
propertystringProperty name, see the table below
valueinterface{}Property value; the type must match the property (silently ignored on mismatch)

Supported properties and their value types:

PropertyValue type
namestring (vendor/package)
descriptionstring
typestring (library/project/metapackage, etc.)
keywords[]string
homepagestring (URL)
licenseinterface{} (string or array of strings)
minimum-stabilitystring (stable/RC/beta/alpha/dev)
prefer-stablebool

Return value

  • error: returned on read/write failure, or when the property name is not on the whitelist (returns an unsupported property error).

Example

go
package main

import (
	"log"

	"github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
	comp, err := composer.New(composer.DefaultOptions())
	if err != nil {
		log.Fatal(err)
	}

	if err := comp.SetProperty("name", "myvendor/mypackage"); err != nil {
		log.Fatal(err)
	}
	if err := comp.SetProperty("description", "An awesome PHP library"); err != nil {
		log.Fatal(err)
	}
	if err := comp.SetProperty("keywords", []string{"php", "library", "awesome"}); err != nil {
		log.Fatal(err)
	}
	if err := comp.SetProperty("license", "MIT"); err != nil {
		log.Fatal(err)
	}
	if err := comp.SetProperty("prefer-stable", true); err != nil {
		log.Fatal(err)
	}
}

Advanced

  • ⚠️ Property names not listed return unsupported property; when the value type assertion fails (e.g. passing an int to name), the method silently skips it without reporting an error, so the caller must ensure the type is correct.
  • 🔗 For keys inside the config section, use SetConfig; for repositories, stability, and similar settings, use dedicated methods such as SetMinimumStability, SetPreferredInstall.
  • 📖 For the underlying structure, see ComposerJSON (read-composer-json).

Released under the MIT License