Skip to content

Commit

Permalink
Run php-cs-fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
s-damian committed Dec 7, 2022
1 parent eed0850 commit 74b97fd
Show file tree
Hide file tree
Showing 32 changed files with 674 additions and 611 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.idea
.php-cs-fixer.cache
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "s-damian/solid-php",
"description": "SOLID - Examples with PHP 8",
"type": "library",
"keywords": ["php", "solid"],
"license": "MIT",
"authors": [
{
"name": "Stephen Damian",
"email": "contact@damian-freelance.fr",
"homepage": "https://github.com/s-damian"
}
],
"require": {
"php": " ^8.1"
},
"scripts": {
"fix-all": "./tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --config=tools/php-cs-fixer/.php-cs-fixer.dist.php",
"fix-all-dry": "./tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --config=tools/php-cs-fixer/.php-cs-fixer.dist.php -vv --dry-run --diff"
}
}
72 changes: 36 additions & 36 deletions src/1_single-responsibility-principle/Article/Article.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
<?php

namespace Article;

class Article
{
public function getTitle(): string
{
return 'Article Title';
}

public function getDescription(): string
{
return 'Article Description';
}

public function getContent(): string
{
return '<p>Article Content</p>';
}

public function getSlug(): string
{
return 'article-slug';
}

public function getAll(): array
{
return [
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'content' => $this->getContent(),
'slug' => $this->getSlug(),
];
}
}
<?php

namespace Article;

class Article
{
public function getTitle(): string
{
return 'Article Title';
}

public function getDescription(): string
{
return 'Article Description';
}

public function getContent(): string
{
return '<p>Article Content</p>';
}

public function getSlug(): string
{
return 'article-slug';
}

public function getAll(): array
{
return [
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'content' => $this->getContent(),
'slug' => $this->getSlug(),
];
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Article\Formatters;

use Article\Article;

interface FormatterInterface
{
public function format(Article $article): string;
}
<?php

namespace Article\Formatters;

use Article\Article;

interface FormatterInterface
{
public function format(Article $article): string;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace Article\Formatters;

use Article\Article;

class JsonFormatter implements FormatterInterface
{
public function format(Article $article): string
{
return json_encode($article->getAll());
}
}
<?php

namespace Article\Formatters;

use Article\Article;

class JsonFormatter implements FormatterInterface
{
public function format(Article $article): string
{
return json_encode($article->getAll());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace Article\Formatters;

use Article\Article;

class XmlFormatter implements FormatterInterface
{
public function format(Article $article): string
{
return xmlrpc_encode($article->getAll());
}
}
<?php

namespace Article\Formatters;

use Article\Article;

class XmlFormatter implements FormatterInterface
{
public function format(Article $article): string
{
return xmlrpc_encode($article->getAll());
}
}
82 changes: 42 additions & 40 deletions src/1_single-responsibility-principle/index.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
<?php

/**
* Single Responsibility Principle.
*/

require_once 'Article/Article.php';
require_once 'Article/Formatters/FormatterInterface.php';
require_once 'Article/Formatters/JsonFormatter.php';
require_once 'Article/Formatters/XmlFormatter.php';

use Article\Article;
use Article\Formatters\JsonFormatter;
use Article\Formatters\XmlFormatter;

/**
* In this example, the "\Article\Formatters\JsonFormatter" and "\Article\Formatters\XmlFormatter" Formatters implement
* the "\Article\Formatters\FormatterInterface" interface.
* This is useful to ensure that the "format" method is present in all "Formatters".
*
* The "format" methods of Formatters expect an instance of "\Article\Article" as a parameter,
* because these Formatters must return with the "format" method in response the data of an "\Article\Article" in their formats.
*
* In this example, we see that each class has only one responsibility.
*/


$article = new Article();


$jsonFormatter = new JsonFormatter();

// return string - Returns the content of the article in JSON format.
echo '<pre>'; var_dump($jsonFormatter->format($article));


$xmlFormatter = new XmlFormatter();

// return string - Returns the content of the article in XML format.
echo '<pre>'; var_dump($xmlFormatter->format($article));
<?php

/**
* Single Responsibility Principle.
*/

require_once 'Article/Article.php';
require_once 'Article/Formatters/FormatterInterface.php';
require_once 'Article/Formatters/JsonFormatter.php';
require_once 'Article/Formatters/XmlFormatter.php';

use Article\Article;
use Article\Formatters\JsonFormatter;
use Article\Formatters\XmlFormatter;

/**
* In this example, the "\Article\Formatters\JsonFormatter" and "\Article\Formatters\XmlFormatter" Formatters implement
* the "\Article\Formatters\FormatterInterface" interface.
* This is useful to ensure that the "format" method is present in all "Formatters".
*
* The "format" methods of Formatters expect an instance of "\Article\Article" as a parameter,
* because these Formatters must return with the "format" method in response the data of an "\Article\Article" in their formats.
*
* In this example, we see that each class has only one responsibility.
*/


$article = new Article();


$jsonFormatter = new JsonFormatter();

// return string - Returns the content of the article in JSON format.
echo '<pre>';
var_dump($jsonFormatter->format($article));


$xmlFormatter = new XmlFormatter();

// return string - Returns the content of the article in XML format.
echo '<pre>';
var_dump($xmlFormatter->format($article));
26 changes: 13 additions & 13 deletions src/2_open-closed-principle/Sport/Sport.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace Sport;

use Sport\Sports\SportInterface;

class Sport
{
public function rules(SportInterface $sport): string
{
return 'The '.$sport->name().' rule is: '.$sport->rules();
}
}
<?php

namespace Sport;

use Sport\Sports\SportInterface;

class Sport
{
public function rules(SportInterface $sport): string
{
return 'The '.$sport->name().' rule is: '.$sport->rules();
}
}
32 changes: 16 additions & 16 deletions src/2_open-closed-principle/Sport/Sports/Basketball.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

namespace Sport\Sports;

final class Basketball implements SportInterface
{
public function name(): string
{
return 'Basketball';
}

public function rules(): string
{
return 'The rules...';
}
}
<?php

namespace Sport\Sports;

final class Basketball implements SportInterface
{
public function name(): string
{
return 'Basketball';
}

public function rules(): string
{
return 'The rules...';
}
}
32 changes: 16 additions & 16 deletions src/2_open-closed-principle/Sport/Sports/Football.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

namespace Sport\Sports;

final class Football implements SportInterface
{
public function name(): string
{
return 'Football';
}

public function rules(): string
{
return 'The rules...';
}
}
<?php

namespace Sport\Sports;

final class Football implements SportInterface
{
public function name(): string
{
return 'Football';
}

public function rules(): string
{
return 'The rules...';
}
}
20 changes: 10 additions & 10 deletions src/2_open-closed-principle/Sport/Sports/SportInterface.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Sport\Sports;

interface SportInterface
{
public function name(): string;

public function rules(): string;
}
<?php

namespace Sport\Sports;

interface SportInterface
{
public function name(): string;

public function rules(): string;
}
Loading

0 comments on commit 74b97fd

Please sign in to comment.