Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: PHP Composer

name: ci
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
push:
branches: [main]

jobs:
build:
Expand Down Expand Up @@ -37,7 +35,19 @@ jobs:
- name: Install dependencies
run: ./composer.phar install --prefer-dist --no-progress

- name: Run test suite
- name: Check pattern structure
run: ./composer.phar structure:check

- name: Run test suite with coverage
env:
XDEBUG_MODE: coverage
run: ./composer.phar test
run: ./composer.phar test:coverage

- name: Check coverage threshold
run: ./composer.phar coverage:check

- name: Run coding standards
run: ./composer.phar cs

- name: Run static analysis
run: ./composer.phar stan
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,30 @@
## Just another collection of design patterns implementations in PHP

## Requirements
- PHP 8.3+
- PHP 8.4.1+

## Setup
- Run `./composer.phar install`

## Run Tests
- Run `./composer.phar test`
- Run `./composer.phar test:coverage`

## Run Coding Standards
- Run `./composer.phar cs`
- Run `./composer.phar cs:fix`

## Run Static Analysis
- Run `./composer.phar stan`

## CI Pipeline (local)
- Run `./composer.phar ci`

## Contributing
- Create feature branches from `master`.
- Keep pattern folders mirrored in `src/<Category>/<Pattern>` and `test/<Category>/<Pattern>`.
- Include/maintain `README.md` in each pattern folder.
- Run `./composer.phar ci` before opening a PR.

## Project Conventions
- See `docs/PATTERN_STRUCTURE.md`.
17 changes: 13 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"license": "MIT",
"require": {
"php": ">=8.3"
"php": ">=8.4.1"
},
"autoload": {
"psr-4": {
Expand All @@ -23,14 +23,23 @@
},
"require-dev": {
"phpunit/phpunit": "^13.0",
"phpstan/phpstan": "^2.1",
"squizlabs/php_codesniffer": "^4.0"
},
"scripts": {
"test": "vendor/bin/phpunit test/",
"test": "vendor/bin/phpunit --no-coverage test/",
"test:coverage": "vendor/bin/phpunit --coverage-clover coverage/clover.xml test/",
"cs": "vendor/bin/phpcs src test",
"cs:fix": "vendor/bin/phpcbf src test",
"stan": "vendor/bin/phpstan analyse --configuration=phpstan.neon --no-progress",
"structure:check": "bash scripts/check-pattern-structure.sh",
"coverage:check": "bash scripts/check-coverage.sh 70 coverage/clover.xml",
"ci": [
"@test",
"@cs"
"@structure:check",
"@test:coverage",
"@coverage:check",
"@cs",
"@stan"
]
}
}
57 changes: 55 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions docs/PATTERN_STRUCTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Pattern Structure Conventions

Each pattern should follow the same layout to keep navigation predictable.

## Source layout

- `src/<Category>/<Pattern>/` contains implementation classes.
- `src/<Category>/<Pattern>/README.md` explains intent and usage.

## Test layout

- `test/<Category>/<Pattern>/` contains tests for that pattern.
- Test classes should map to source classes when possible.

## Categories

- Creational
- Structural
- Behavioral
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 1
paths:
- src
- test
62 changes: 62 additions & 0 deletions scripts/check-coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env bash

set -euo pipefail

MIN_COVERAGE="${1:-70}"
CLOVER_FILE="${2:-coverage/clover.xml}"

if [[ ! -f "$CLOVER_FILE" ]]; then
echo "Coverage file not found: $CLOVER_FILE"
exit 1
fi

read -r COVERED STATEMENTS < <(
php -r '
$file = $argv[1];
$xml = @simplexml_load_file($file);
if ($xml === false) {
fwrite(STDERR, "Could not parse coverage XML: $file\n");
exit(1);
}

$metrics = $xml->xpath("//metrics[@statements and @coveredstatements]");
if ($metrics === false || count($metrics) === 0) {
fwrite(STDERR, "No coverage metrics found in $file\n");
exit(1);
}

$covered = -1;
$statements = -1;

foreach ($metrics as $node) {
$nodeStatements = (int) $node["statements"];
$nodeCovered = (int) $node["coveredstatements"];

if ($nodeStatements > $statements) {
$statements = $nodeStatements;
$covered = $nodeCovered;
}
}

if ($statements <= 0) {
fwrite(STDERR, "Invalid coverage statement count in $file\n");
exit(1);
}

echo $covered . " " . $statements . PHP_EOL;
' "$CLOVER_FILE"
)

if [[ -z "$STATEMENTS" || -z "$COVERED" || "$STATEMENTS" -eq 0 ]]; then
echo "Invalid coverage metrics in $CLOVER_FILE"
exit 1
fi

PERCENT="$(awk -v c="$COVERED" -v s="$STATEMENTS" 'BEGIN { printf "%.2f", (c / s) * 100 }')"

echo "Coverage: ${PERCENT}% (min ${MIN_COVERAGE}%)"

if awk -v p="$PERCENT" -v m="$MIN_COVERAGE" 'BEGIN { exit !(p < m) }'; then
echo "Coverage below threshold."
exit 1
fi
26 changes: 26 additions & 0 deletions scripts/check-pattern-structure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

cd "$ROOT_DIR"

status=0

while IFS= read -r pattern_dir; do
category="$(basename "$(dirname "$pattern_dir")")"
pattern="$(basename "$pattern_dir")"

if [[ ! -f "${pattern_dir}/README.md" ]]; then
echo "Missing README: ${pattern_dir}/README.md"
status=1
fi

if [[ ! -d "test/${category}/${pattern}" ]]; then
echo "Missing test directory: test/${category}/${pattern}"
status=1
fi
done < <(find src -mindepth 2 -maxdepth 2 -type d | sort)

exit "$status"
4 changes: 2 additions & 2 deletions src/Structural/Facade/CoffeeMakerFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public function makeCups(int $numberOfCupsToMake): array
$numberOfCupsToMake = $this->checkCupsToMake($numberOfCupsToMake);

$coffeeCups = [];
for ($i=1; $i<$numberOfCupsToMake+1; $i++) {
for ($i = 1; $i < $numberOfCupsToMake + 1; $i++) {
$products = implode(',', $this->getProducts());
$coffeeCups["coffee #".$i] = $products;
$coffeeCups["coffee #" . $i] = $products;
}
return $coffeeCups;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Structural/Proxy/ApiProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

class ApiProxy extends Api
{
public function __construct(private ?Api $wrapper) {}
public function __construct(private ?Api $wrapper)
{
}

public function doApiCall(string $url, array $data, string $method): array|null
{
Expand Down
5 changes: 4 additions & 1 deletion test/Creational/AbstractFactory/BmwCarFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public function testShouldCreateABmwFamilyCar()

private function createCarDescription(string $type): string
{
return "BMW $type car!" . PHP_EOL . "Name:" . (self::GENERIC_CAR_NAME) . "" . PHP_EOL . "Color:" . (self::GENERIC_CAR_COLOR) . "" . PHP_EOL . "Engine:" . (self::GENERIC_CAR_ENGINE_SPECS) . "";
return "BMW $type car!"
. PHP_EOL . "Name:" . self::GENERIC_CAR_NAME
. PHP_EOL . "Color:" . self::GENERIC_CAR_COLOR
. PHP_EOL . "Engine:" . self::GENERIC_CAR_ENGINE_SPECS;
}
}
1 change: 0 additions & 1 deletion test/Creational/Builder/ClassicWatchBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class ClassicWatchBuilderTest extends TestCase
{

public function testShouldCreateAClassicWatch()
{
$classicWatch = (new ClassicWatchBuilder())
Expand Down
1 change: 0 additions & 1 deletion test/Creational/Builder/ClassicWatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class ClassicWatchTest extends TestCase
{

public function testShouldAddAComponentToAWatch()
{
$watch = new ClassicWatch();
Expand Down
1 change: 0 additions & 1 deletion test/Creational/Builder/DirectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class DirectorTest extends TestCase
{

public function testShouldCreateASportWatch()
{
$sportWatch = (new Director())->build(new SportWatchBuilder());
Expand Down
1 change: 0 additions & 1 deletion test/Creational/Builder/SportWatchBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class SportWatchBuilderTest extends TestCase
{

public function testShouldCreateASportWatch()
{
$watch = (new SportWatchBuilder())
Expand Down
1 change: 0 additions & 1 deletion test/Creational/Builder/SportWatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class SportWatchTest extends TestCase
{

public function testShouldAddAComponentToASportWatch()
{
$watch = new SportWatch();
Expand Down
1 change: 0 additions & 1 deletion test/Structural/Composite/PhoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class PhoneTest extends TestCase
{

public function testShouldGetPhonePrice()
{
$phone = new Phone(
Expand Down
1 change: 0 additions & 1 deletion test/Structural/Decorator/ApiResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class ApiResponseTest extends TestCase
{

public function testShouldReturnARawApiResponse()
{
$expected = [
Expand Down
1 change: 0 additions & 1 deletion test/Structural/Decorator/JsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class JsonResponseTest extends TestCase
{

public function testShouldBeAbleToConvertResponseToJsonString()
{
$expected = '{"message":"api response to json"}';
Expand Down
Loading