Creating a New PHP Project from Scratch with Composer

Create Project Directory

Initialize Project with Composer

In your new project directory run:

composer init

This will guide you through the creation of the composer.json file which contains the details, configuration options, and dependency definitions for your project.

Setup PSR-4 Autoloading

Add the following bit to your composer.json file to let composer know where your project's source files reside:

    "autoload": {
        "psr-4": {  
            "SayWebSolutions\\ProjectClass\\": "src/"  
        }  
    }

Read more about PSR-4:

Setup Codesniffer to Perform Linting on Project Source Code

Documentation is available at the PHP_CodeSniffer GitHub project.

composer require --dev squizlabs/php_codesniffer ^3

Add the following custom composer script definitions to your composer.json file in order to run codesniffer with the composer lint command, and fix resulting errors with the composer lint-fix command:

    "scripts": {
        "lint": "phpcs ./src --standard=PSR2",
        "lint-fix": "phpcbf ./src --standard=PSR2"
    }

Run phpcs -i to see the list of installed coding standards that you can use based on your format / style preferences:

$ vendor/bin/phpcs -i
The installed coding standards are Squiz, PSR2, Zend, PSR12, PSR1, PEAR and MySource

This stackoverflow question has some good summary info on the available standards, and it sounds like PSR2 is a sane default choice while getting aquainted with the linting process.

Tags

 PHP  Composer