Skip to content

Commit

Permalink
Merge pull request emilybache#106 from Pen-y-Fan/php8
Browse files Browse the repository at this point in the history
Bump to PHP 8.0+
  • Loading branch information
emilybache authored Jan 6, 2023
2 parents 88c805c + 6d3cf93 commit 05f95cd
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 180 deletions.
19 changes: 10 additions & 9 deletions php/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ See the [top level readme](../README.md) for general information about this exer

The kata uses:

- [PHP 7.2-7.4 or 8.0+](https://www.php.net/downloads.php)
- [PHP 8.0+](https://www.php.net/downloads.php)
- [Composer](https://getcomposer.org)

Recommended:
- [Git](https://git-scm.com/downloads)

Clone the repository
See [GitHub cloning a repository](https://help.github.com/en/articles/cloning-a-repository) for details on how to
create a local copy of this project on your computer.

```sh
git clone [email protected]:emilybache/Tennis-Refactoring-Kata.git
Expand All @@ -35,7 +36,7 @@ composer install
Run all the tests

```shell script
composer test
composer tests
```

## Dependencies
Expand All @@ -60,19 +61,19 @@ PHPUnit is pre-configured to run tests. PHPUnit can be run using a composer scri
root of the PHP kata run:

```shell script
composer test
composer tests
```

On Windows a batch file has been created, similar to an alias on Linux/Mac (e.g. `alias pu="composer test"`), the same
PHPUnit `composer test` can be run:

```shell script
pu
pu.bat
```

### Tests with Coverage Report

To run all test and generate a html coverage report run:
To run all test and generate a html coverage report run (requires [xdebug](https://xdebug.org/download)):

```shell script
composer test-coverage
Expand All @@ -97,7 +98,7 @@ On Windows a batch file has been created, similar to an alias on Linux/Mac (e.g.
same `composer check-cs` can be run:

```shell script
cc
cc.bat
```

### Fix Code
Expand All @@ -112,7 +113,7 @@ On Windows a batch file has been created, similar to an alias on Linux/Mac (e.g.
`composer fix-cs` can be run:

```shell script
fc
fc.bat
```

## Static Analysis
Expand All @@ -127,7 +128,7 @@ On Windows a batch file has been created, similar to an alias on Linux/Mac (e.g.
same `composer phpstan` can be run:

```shell script
ps
ps.bat
```

**Happy coding**!
17 changes: 7 additions & 10 deletions php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"description": "Tennis Refactoring Kata",
"license": "MIT",
"require": {
"php": "^7.3|^8.0"
"php": "^8.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^0.12.25",
"phpstan/phpstan-phpunit": "^0.12.10",
"symplify/easy-coding-standard": "^9.0",
"symplify/phpstan-extensions": "^9.0"
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-phpunit": "^1.3",
"symplify/easy-coding-standard": "^11.1",
"symplify/phpstan-extensions": "^11.1"
},
"autoload": {
"psr-4": {
Expand All @@ -23,13 +23,10 @@
}
},
"scripts": {
"checkcode": "phpcs src tests --standard=PSR12",
"fixcode": "phpcbf src tests --standard=PSR12",
"test": "phpunit",
"tests": "phpunit",
"test-coverage": "phpunit --coverage-html build/coverage",
"check-cs": "ecs check src tests --ansi",
"fix-cs": "ecs check src tests --fix --ansi",
"check-cs": "ecs check --ansi",
"fix-cs": "ecs check --fix --ansi",
"phpstan": "phpstan analyse --ansi"
}
}
40 changes: 26 additions & 14 deletions php/ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,45 @@
declare(strict_types=1);

use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\EasyCodingStandard\ValueObject\Option;
use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ArraySyntaxFixer::class)
->call('configure', [[
'syntax' => 'short',
]]);
// composer require --dev symplify/easy-coding-standard
// vendor/bin/ecs init

$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PATHS, [
return static function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
__DIR__ . '/ecs.php', // check this file too!
]);

$parameters->set(Option::SETS, [
// run and fix, one by one
$ecsConfig->skip([
// skip specific rules
]);

// run and fix, one by one
$ecsConfig->sets([
SetList::SPACES,
SetList::ARRAY,
SetList::DOCBLOCK,
SetList::NAMESPACES,
SetList::CONTROL_STRUCTURES,
SetList::CLEAN_CODE,
SetList::STRICT,
SetList::PSR_12,
SetList::PHP_70,
SetList::PHP_71,
SetList::PHPUNIT,
]);

// add declare(strict_types=1); to all php files:
$ecsConfig->rule(DeclareStrictTypesFixer::class);

// change $array = array(); to $array = [];
$ecsConfig->ruleWithConfiguration(ArraySyntaxFixer::class, [
'syntax' => 'short',
]);

// [default: PHP_EOL]; other options: "\n"
$ecsConfig->lineEnding("\n");
};
4 changes: 2 additions & 2 deletions php/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ parameters:
- src
- tests

# The level 8 is the highest level
level: 8
# max is the highest level
level: max
2 changes: 1 addition & 1 deletion php/pu.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
composer test
composer tests
13 changes: 4 additions & 9 deletions php/src/TennisGame.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
<?php

declare(strict_types=1);

namespace TennisGame;

interface TennisGame
{
/**
* @param $playerName
* @return void
*/
public function wonPoint($playerName);
public function wonPoint(string $playerName): void;

/**
* @return string
*/
public function getScore();
public function getScore(): string;
}
71 changes: 32 additions & 39 deletions php/src/TennisGame1.php
Original file line number Diff line number Diff line change
@@ -1,78 +1,71 @@
<?php

declare(strict_types=1);

namespace TennisGame;

class TennisGame1 implements TennisGame
{
private $m_score1 = 0;
private $m_score2 = 0;
private $player1Name = '';
private $player2Name = '';
private int $m_score1 = 0;

public function __construct($player1Name, $player2Name)
{
$this->player1Name = $player1Name;
$this->player2Name = $player2Name;
private int $m_score2 = 0;

public function __construct(
private string $player1Name,
private string $player2Name
) {
}

public function wonPoint($playerName)
public function wonPoint(string $playerName): void
{
if ('player1' == $playerName) {
if ($playerName === 'player1') {
$this->m_score1++;
} else {
$this->m_score2++;
}
}

public function getScore()
public function getScore(): string
{
$score = "";
if ($this->m_score1 == $this->m_score2) {
switch ($this->m_score1) {
case 0:
$score = "Love-All";
break;
case 1:
$score = "Fifteen-All";
break;
case 2:
$score = "Thirty-All";
break;
default:
$score = "Deuce";
break;
}
$score = '';
if ($this->m_score1 === $this->m_score2) {
$score = match ($this->m_score1) {
0 => 'Love-All',
1 => 'Fifteen-All',
2 => 'Thirty-All',
default => 'Deuce',
};
} elseif ($this->m_score1 >= 4 || $this->m_score2 >= 4) {
$minusResult = $this->m_score1 - $this->m_score2;
if ($minusResult == 1) {
$score = "Advantage player1";
} elseif ($minusResult == -1) {
$score = "Advantage player2";
if ($minusResult === 1) {
$score = 'Advantage player1';
} elseif ($minusResult === -1) {
$score = 'Advantage player2';
} elseif ($minusResult >= 2) {
$score = "Win for player1";
$score = 'Win for player1';
} else {
$score = "Win for player2";
$score = 'Win for player2';
}
} else {
for ($i = 1; $i < 3; $i++) {
if ($i == 1) {
if ($i === 1) {
$tempScore = $this->m_score1;
} else {
$score .= "-";
$score .= '-';
$tempScore = $this->m_score2;
}
switch ($tempScore) {
case 0:
$score .= "Love";
$score .= 'Love';
break;
case 1:
$score .= "Fifteen";
$score .= 'Fifteen';
break;
case 2:
$score .= "Thirty";
$score .= 'Thirty';
break;
case 3:
$score .= "Forty";
$score .= 'Forty';
break;
}
}
Expand Down
Loading

0 comments on commit 05f95cd

Please sign in to comment.