Skip to main content

Library

In library mode, your project is a Composer package that ships classes and no executable. Use it for a Behat context, a PHPUnit extension, a framework plugin or a PSR implementation. This page is for developers building a library from Scaffold. Follow it to select the mode and replace the placeholder class with your own.

To select library mode, turn off both entry points, the CLI command app and the CLI script, when you run install.sh:

./install.sh --namespace=AcmeApp --name=acme-app --author="Jane Doe" \
--php --no-php-command --no-php-script

Replace the namespace, name and author with your own. In interactive mode, answer n to both the Use CLI command app and Use simple script prompts.

What you get​

install.sh generates the following:

  • composer.json with the PSR-4 autoloader and no bin section
  • src/Example.php and tests/phpunit/Unit/ExampleUnitTest.php, a placeholder class and its test
  • PHP_CodeSniffer and PHPStan, configured to check src and tests/phpunit
  • Rector, configured to check every PHP file outside vendor and node_modules
  • PHPUnit, configured to report coverage of src
  • The Test PHP GitHub Actions workflow

install.sh leaves out both entry points, box.json, the PHAR build and the Release PHP workflow. A library has no PHAR or script for that workflow to attach.

When Renovate is on, which is the default, install.sh adds a rule to renovate.json so that Renovate skips the require section of composer.json. Those constraints are your package's public contract, so change them by hand.

Add your classes​

PHP_CodeSniffer and PHPStan both report an error when neither src nor tests/phpunit holds a PHP file, so add your first class before you delete the placeholders.

To replace the placeholder pair with your classes:

  1. Add your classes under src/, in the AcmeApp\App\ namespace. AcmeApp is your --namespace value, and App stays as is.

  2. Cover each class with a test in tests/phpunit/Unit/. Give each test class a #[CoversClass] attribute, because composer test fails when a test has no coverage metadata.

  3. Delete src/Example.php and tests/phpunit/Unit/ExampleUnitTest.php.

  4. Run the tests:

    composer test

    Confirm that every test passes.