5 Command Line Tips for Composer

The command line is an essential tool for developers of all types. As a PHP Developer, I use it all the time. Making the most of composer is essential to managing your PHP projects. This article will show a few things to improve your use of Composer.

Require a Minimum PHP Version

One thing that helps you in life is to manage people’s expectations. This is particularly important when working with Open Source software. One way to do that is to specify the minimum PHP version you support.

composer require php '>=7.1.0';

This will detect your customers PHP version, and throw an error if it’s too low.

Use Options for Default Values

The composer init command provides a number of options. Each of these options becomes the default value when the script prompts you to enter a value. This reduces the amount of typing you need to do while giving you the flexibility to make necessary changes.

composer init --license=MIT \
--description="Briefly describe your project" \
--author="Andrew Woods <atwoods1@gmail.com>" \
--type=project

The \ characters above continues the command onto the next line. because long lines that wrap can make it hard to read the command.

Require Your Favorite Packages

As the number of projects you work on increases, you’ll notice that you use some of the same packages over and over again. The composer init command will prompt you for packages to install. Instead, I run separate commands for each one. This has 2 benefits:

  1. Reduces typing
  2. Makes the packages more visible

Review Your New composer.json File

A lot of things happen during the composer init and composer require processes, and tons of content get written to your terminal. When it’s all over, you should review the file to check that it has everything you want

$ cat composer.json

This is best done after you’ve run all the other composer commands for your project.

Create Your Own Composer Init Function

You’ve learned some great tips above.  Putting them all in one place though, is like superchariging your setup. Open your .bashrc file, and create a function called composer_init. which initializes a new composer.json for your project.

function composer_init {
    composer init --license=MIT \
    --description="Briefly describe your project" \
    --author="Firstname Lastname <user@example.com>" \
    --type=project

    # Require your favorite packages
    composer require monolog/monolog
    composer require symfony/console
    # Set the minimum PHP version
    composer require php '>=7.1.0'

    # Review your composer.json
    cat composer.json
}

 

Conclusion

There’s a lot Composer can do for you between its built in functionality, and the packages that the PHP community has written. This is just the beginning. There are opportunites to improve upon the tips mentioned above. By adding in some of your own magic, you can simplify your project setup.

 

Share Your Thoughts

I'd love to hear your thoughts on the article.