Merge pull request #393 from Pen-y-Fan/php8

Bump to PHP 8.0+
This commit is contained in:
Emily Bache 2023-01-09 08:41:09 +01:00 committed by GitHub
commit c6040ed788
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 87 additions and 4602 deletions

View File

@ -1,3 +0,0 @@
[*.php]
indent_size = 4

10
php/.gitignore vendored
View File

@ -1,6 +1,6 @@
/.idea /.idea
/report.xml
/.phpstorm.meta.php
/.phpunit.result.cache
/build /build
/vendor /vendor
/.editorconfig
/.phpunit.result.cache
/composer.lock

View File

@ -7,14 +7,15 @@ GildedRose Kata.
The kata uses: The kata uses:
- [PHP 7.3 or 7.4 or 8.0+](https://www.php.net/downloads.php) - [8.0+](https://www.php.net/downloads.php)
- [Composer](https://getcomposer.org) - [Composer](https://getcomposer.org)
Recommended: Recommended:
- [Git](https://git-scm.com/downloads) - [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 ```sh
git clone git@github.com:emilybache/GildedRose-Refactoring-Kata.git git clone git@github.com:emilybache/GildedRose-Refactoring-Kata.git
@ -41,7 +42,6 @@ The project uses composer to install:
- [ApprovalTests.PHP](https://github.com/approvals/ApprovalTests.php) - [ApprovalTests.PHP](https://github.com/approvals/ApprovalTests.php)
- [PHPStan](https://github.com/phpstan/phpstan) - [PHPStan](https://github.com/phpstan/phpstan)
- [Easy Coding Standard (ECS)](https://github.com/symplify/easy-coding-standard) - [Easy Coding Standard (ECS)](https://github.com/symplify/easy-coding-standard)
- [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer/wiki)
## Folders ## Folders
@ -56,20 +56,30 @@ The project uses composer to install:
- `Fixture` - `Fixture`
- `texttest_fixture.php` this could be used by an ApprovalTests, or run from the command line - `texttest_fixture.php` this could be used by an ApprovalTests, or run from the command line
## Fixture
To run the fixture from the php directory:
```shell
php .\fixtures\texttest_fixture.php 10
```
Change **10** to the required days.
## Testing ## Testing
PHPUnit is configured for testing, a composer script has been provided. To run the unit tests, from the root of the PHP PHPUnit is configured for testing, a composer script has been provided. To run the unit tests, from the root of the PHP
project run: project run:
```shell script ```shell script
composer test composer tests
``` ```
A Windows a batch file has been created, like an alias on Linux/Mac (e.g. `alias pu="composer test"`), the same A Windows a batch file has been created, like an alias on Linux/Mac (e.g. `alias pu="composer tests"`), the same
PHPUnit `composer test` can be run: PHPUnit `composer tests` can be run:
```shell script ```shell script
pu pu.bat
``` ```
### Tests with Coverage Report ### Tests with Coverage Report
@ -83,6 +93,8 @@ composer test-coverage
The test-coverage report will be created in /builds, it is best viewed by opening /builds/**index.html** in your The test-coverage report will be created in /builds, it is best viewed by opening /builds/**index.html** in your
browser. browser.
The [XDEbug](https://xdebug.org/download) extension is required for generating the coverage report.
## Code Standard ## Code Standard
Easy Coding Standard (ECS) is configured for style and code standards, **PSR-12** is used. The current code is not upto Easy Coding Standard (ECS) is configured for style and code standards, **PSR-12** is used. The current code is not upto
@ -100,7 +112,7 @@ On Windows a batch file has been created, like an alias on Linux/Mac (e.g. `alia
PHPUnit `composer check-cs` can be run: PHPUnit `composer check-cs` can be run:
```shell script ```shell script
cc cc.bat
``` ```
### Fix Code ### Fix Code
@ -115,7 +127,7 @@ On Windows a batch file has been created, like an alias on Linux/Mac (e.g. `alia
PHPUnit `composer fix-cs` can be run: PHPUnit `composer fix-cs` can be run:
```shell script ```shell script
fc fc.bat
``` ```
## Static Analysis ## Static Analysis
@ -130,7 +142,7 @@ On Windows a batch file has been created, like an alias on Linux/Mac (e.g. `alia
PHPUnit `composer phpstan` can be run: PHPUnit `composer phpstan` can be run:
```shell script ```shell script
ps ps.bat
``` ```
**Happy coding**! **Happy coding**!

View File

@ -1,8 +1,17 @@
{ {
"name": "emilybache/gilded-rose-refactoring-kata", "name": "emilybache/gilded-rose-refactoring-kata",
"description": "A kata to practice refactoring, tests and polymorphism", "description": "A kata to practice refactoring, tests and polymorphism",
"license": "MIT",
"require": { "require": {
"php": "^7.3 || ^8.0" "php": "^8.0"
},
"require-dev": {
"approvals/approval-tests": "dev-Main",
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-phpunit": "^1.3",
"symplify/easy-coding-standard": "^11.1",
"symplify/phpstan-extensions": "^11.1"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@ -14,18 +23,7 @@
"Tests\\": "tests/" "Tests\\": "tests/"
} }
}, },
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^0.12.85",
"phpstan/phpstan-phpunit": "^0.12.18",
"symplify/easy-coding-standard": "^9.3",
"symplify/phpstan-extensions": "^9.3",
"approvals/approval-tests": "dev-Main"
},
"scripts": { "scripts": {
"checkcode": "phpcs src tests --standard=PSR12",
"fixcode": "phpcbf src tests --standard=PSR12",
"test": "phpunit",
"tests": "phpunit", "tests": "phpunit",
"test-coverage": "phpunit --coverage-html build/coverage", "test-coverage": "phpunit --coverage-html build/coverage",
"check-cs": "ecs check", "check-cs": "ecs check",

4517
php/composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -3,34 +3,45 @@
declare(strict_types=1); declare(strict_types=1);
use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer; use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer;
use Symplify\EasyCodingStandard\ValueObject\Option; use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList; use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
return static function (ContainerConfigurator $containerConfigurator): void { // composer require --dev symplify/easy-coding-standard
$services = $containerConfigurator->services(); // vendor/bin/ecs init
$services->set(ArraySyntaxFixer::class)
->call('configure', [[
'syntax' => 'short',
]]);
$parameters = $containerConfigurator->parameters(); return static function (ECSConfig $ecsConfig): void {
$parameters->set(Option::PATHS, [ $ecsConfig->paths([
__DIR__ . '/fixtures',
__DIR__ . '/src', __DIR__ . '/src',
__DIR__ . '/tests', __DIR__ . '/tests',
__DIR__ . '/ecs.php', // check this file too!
]); ]);
$parameters->set $ecsConfig->skip([
(Option::SETS, // rules to skip
[ ]);
SetList::CLEAN_CODE,
SetList::COMMON,
SetList::PSR_12,
]
);
$parameters->set(Option::INDENTATION, "spaces"); // 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::PHPUNIT,
]);
$parameters->set(Option::LINE_ENDING, "\n"); // 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");
}; };

View File

@ -25,7 +25,7 @@ $items = [
$app = new GildedRose($items); $app = new GildedRose($items);
$days = 2; $days = 2;
if (count($argv) > 1) { if ((is_countable($argv) ? count($argv) : 0) > 1) {
$days = (int) $argv[1]; $days = (int) $argv[1];
} }

View File

@ -8,6 +8,7 @@ parameters:
- tests - tests
- fixtures - fixtures
# max is the highest level
level: max level: max
checkGenericClassInNonGenericObjectType: false checkGenericClassInNonGenericObjectType: false

View File

@ -1 +1 @@
composer test composer tests

View File

@ -7,13 +7,11 @@ namespace GildedRose;
final class GildedRose final class GildedRose
{ {
/** /**
* @var Item[] * @param Item[] $items
*/ */
private $items; public function __construct(
private array $items
public function __construct(array $items) ) {
{
$this->items = $items;
} }
public function updateQuality(): void public function updateQuality(): void
@ -29,12 +27,12 @@ final class GildedRose
if ($item->quality < 50) { if ($item->quality < 50) {
$item->quality = $item->quality + 1; $item->quality = $item->quality + 1;
if ($item->name == 'Backstage passes to a TAFKAL80ETC concert') { if ($item->name == 'Backstage passes to a TAFKAL80ETC concert') {
if ($item->sell_in < 11) { if ($item->sellIn < 11) {
if ($item->quality < 50) { if ($item->quality < 50) {
$item->quality = $item->quality + 1; $item->quality = $item->quality + 1;
} }
} }
if ($item->sell_in < 6) { if ($item->sellIn < 6) {
if ($item->quality < 50) { if ($item->quality < 50) {
$item->quality = $item->quality + 1; $item->quality = $item->quality + 1;
} }
@ -44,10 +42,10 @@ final class GildedRose
} }
if ($item->name != 'Sulfuras, Hand of Ragnaros') { if ($item->name != 'Sulfuras, Hand of Ragnaros') {
$item->sell_in = $item->sell_in - 1; $item->sellIn = $item->sellIn - 1;
} }
if ($item->sell_in < 0) { if ($item->sellIn < 0) {
if ($item->name != 'Aged Brie') { if ($item->name != 'Aged Brie') {
if ($item->name != 'Backstage passes to a TAFKAL80ETC concert') { if ($item->name != 'Backstage passes to a TAFKAL80ETC concert') {
if ($item->quality > 0) { if ($item->quality > 0) {

View File

@ -4,32 +4,17 @@ declare(strict_types=1);
namespace GildedRose; namespace GildedRose;
final class Item final class Item implements \Stringable
{ {
/** public function __construct(
* @var string public string $name,
*/ public int $sellIn,
public $name; public int $quality
) {
/**
* @var int
*/
public $sell_in;
/**
* @var int
*/
public $quality;
public function __construct(string $name, int $sell_in, int $quality)
{
$this->name = $name;
$this->sell_in = $sell_in;
$this->quality = $quality;
} }
public function __toString(): string public function __toString(): string
{ {
return "{$this->name}, {$this->sell_in}, {$this->quality}"; return (string) "{$this->name}, {$this->sellIn}, {$this->quality}";
} }
} }