Design Patterns – Configuring the project

Let’s get started

Firstly I will create our skeleton to go through patterns. The project will be available at https://github.com/diegosm/designpatterns


Ok, after cloning this repository on my machine, lets create some folders and files, the structure should be like the image below:

.designpatterns
.. src
... Behavioral
... Creational
... Structural
.. tests
... Behavioral
... Creational
... Structural
.. .gitignore
.. composer.json
.. phpcs.xml
.. phpunit.xml

Then we will create our files:

composer.json

{
  "name": "diegosm/designpatterns",
  "description": "Design Patterns Using PHP",
  "type": "project",
  "license": "FREE",
  "authors": [
    {
      "name": "Diego Mota",
      "email": "diego.smota@gmail.com"
    }
  ],
  "require": {
    "php": "^8.0"
  },
  "require-dev": {
    "phpunit/phpunit": "^9.3",
    "squizlabs/php_codesniffer": "^3.4"
  },
  "autoload": {
    "psr-4": {
      "DesignPatterns\\": "src/"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "Tests\\": "tests/"
    }
  },
  "scripts": {
    "phpcs": "./vendor/bin/phpcs",
    "test": "./vendor/bin/phpunit"
  }
}

phpcs.xml

<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
    <description>The coding standard for our project.</description>
    <rule ref="PSR2"/>

    <file>src</file>
    <file>tests</file>

    <!-- Show progression -->
    <arg value="p"/>
</ruleset>

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./src</directory>
        </include>
    </coverage>
</phpunit>

.gitignore

/**/.idea/*
/.idea
/vendor
.phpunit.result.cache

Now is time to run composer. We will use these commands often. (Except install :D)

$ composer install
$ composer phpcs
$ composer test

Folders won’t be committed yet since they are empty.

Don’t forget to tracking this post, here I will update with links to other design patterns posts.

Next post will be about our first design pattern implementation, We will learn Strategy Pattern, maybe is the easiest pattern and one of most common used.

Don’t forget to follow our Design Patterns – What, when, why? series 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *




Enter Captcha Here :