Skip to content

Commit

Permalink
PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
iLexN committed Nov 26, 2021
1 parent cef8213 commit 06a8ea0
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 75 deletions.
6 changes: 6 additions & 0 deletions example/create-change-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
new DefaultFormatter('https://github.com/iLexN/keep-a-change-log/compare')
);

$c->addRelease(
(new Release('1.4.0', '2021-11-26'))
->added('Add support for PHP 8.1')
->removed('Remove support for PHP 8.0')
);

$c->addRelease(
(new Release('1.3.1', '2021-08-10'))
->fixed('fix composer.json bin command')
Expand Down
14 changes: 8 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
parameters:
level: max
paths:
- src
- tests


level: max
paths:
- src
- tests
ignoreErrors:
# phpstan not support 8.1 enum
- '#Access to undefined constant Ilex\\ChangeLog\\Enum\\ChangeType::(.*)#'
- '#Access to an undefined property Ilex\\ChangeLog\\Enum\\ChangeType::(.*)#'

20 changes: 20 additions & 0 deletions src/Enum/ChangeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace Ilex\ChangeLog\Enum;

enum ChangeType: int
{

case ADDED = 1;

case CHANGED = 2;

case DEPRECATED = 3;

case REMOVED = 4;

case FIXED = 5;

case SECURITY = 6;
}
18 changes: 10 additions & 8 deletions src/Release.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ilex\ChangeLog;

use Ilex\ChangeLog\Enum\ChangeType;
use Ilex\ChangeLog\Type\ChangeTypeFactory;
use Ilex\ChangeLog\Type\ChangeTypeFactoryInterface;

Expand Down Expand Up @@ -40,10 +41,11 @@ private function setFactory(?ChangeTypeFactoryInterface $changeTypeFactory): Cha
}


private function addChangeList(int $key, string $description): self
private function addChangeList(ChangeType $type, string $description): self
{
$key = $type->value;
if (!\array_key_exists($key, $this->changeList)) {
$this->changeList[$key] = $this->changeTypeFactory->create($key);
$this->changeList[$key] = $this->changeTypeFactory->create($type);
}

$this->changeList[$key]->add($description);
Expand All @@ -60,34 +62,34 @@ public function getChangeList(): array

public function added(string $description): self
{
return $this->addChangeList(ChangeTypeFactory::ADDED, $description);
return $this->addChangeList(ChangeType::ADDED, $description);
}

public function changed(string $description): self
{
return $this->addChangeList(ChangeTypeFactory::CHANGED, $description);
return $this->addChangeList(ChangeType::CHANGED, $description);
}

public function deprecated(string $description): self
{
return $this->addChangeList(
ChangeTypeFactory::DEPRECATED,
ChangeType::DEPRECATED,
$description
);
}

public function removed(string $description): self
{
return $this->addChangeList(ChangeTypeFactory::REMOVED, $description);
return $this->addChangeList(ChangeType::REMOVED, $description);
}

public function fixed(string $description): self
{
return $this->addChangeList(ChangeTypeFactory::FIXED, $description);
return $this->addChangeList(ChangeType::FIXED, $description);
}

public function security(string $description): self
{
return $this->addChangeList(ChangeTypeFactory::SECURITY, $description);
return $this->addChangeList(ChangeType::SECURITY, $description);
}
}
52 changes: 8 additions & 44 deletions src/Type/ChangeTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,23 @@

namespace Ilex\ChangeLog\Type;

use Ilex\ChangeLog\Enum\ChangeType;

/**
* Class ChangeTypeFactory
*
* @package Ilex\ChangeLog\Type
*/
class ChangeTypeFactory implements ChangeTypeFactoryInterface
{

/**
* ADDED
* @var int
*/
public final const ADDED = 1;

/**
* CHANGED
* @var int
*/
public final const CHANGED = 2;

/**
* DEPRECATED
* @var int
*/
public final const DEPRECATED = 3;

/**
* REMOVED
* @var int
*/
public final const REMOVED = 4;

/**
* FIXED
* @var int
*/
public final const FIXED = 5;

/**
* SECURITY
* @var int
*/
public final const SECURITY = 6;

public function create(int $type): TypeInterface
public function create(ChangeType $type): TypeInterface
{
return match ($type) {
//self::ADDED => new Added(),
self::CHANGED => new Changed(),
self:: DEPRECATED => new Deprecated(),
self::REMOVED => new Removed(),
self::FIXED => new Fixed(),
self::SECURITY => new Security(),
ChangeType::CHANGED => new Changed(),
ChangeType:: DEPRECATED => new Deprecated(),
ChangeType::REMOVED => new Removed(),
ChangeType::FIXED => new Fixed(),
ChangeType::SECURITY => new Security(),
default => new Added(),
};
}
Expand Down
4 changes: 3 additions & 1 deletion src/Type/ChangeTypeFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Ilex\ChangeLog\Type;

use Ilex\ChangeLog\Enum\ChangeType;

interface ChangeTypeFactoryInterface
{
public function create(int $type): TypeInterface;
public function create(ChangeType $type): TypeInterface;
}
7 changes: 4 additions & 3 deletions tests/ReleaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ilex\ChangeLog\Tests;

use Ilex\ChangeLog\Enum\ChangeType;
use Ilex\ChangeLog\Release;
use Ilex\ChangeLog\Type\Added;
use Ilex\ChangeLog\Type\Changed;
Expand Down Expand Up @@ -67,12 +68,12 @@ public function testConstructWithFactory(): void
public function testAddChangeList(): void
{
try {
call_method($this->release, 'addChangeList', [1, 'add1']);
call_method($this->release, 'addChangeList', [ChangeType::ADDED, 'add1']);
} catch (\ReflectionException $e) {
self::fail($e->getMessage());
}
try {
call_method($this->release, 'addChangeList', [1, 'add2']);
call_method($this->release, 'addChangeList', [ChangeType::ADDED, 'add2']);
} catch (\ReflectionException $e) {
self::fail($e->getMessage());
}
Expand All @@ -94,7 +95,7 @@ public function testAddChangeListReturnSelf(): void
try {
self::assertEquals(
$this->release,
call_method($this->release, 'addChangeList', [1, 'add2']),
call_method($this->release, 'addChangeList', [ChangeType::ADDED, 'add2']),
'Test return self'
);
} catch (\ReflectionException $e) {
Expand Down
27 changes: 14 additions & 13 deletions tests/Type/ChangeTypeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ilex\ChangeLog\Tests\Type;

use Ilex\ChangeLog\Enum\ChangeType;
use Ilex\ChangeLog\Type\Added;
use Ilex\ChangeLog\Type\Changed;
use Ilex\ChangeLog\Type\ChangeTypeFactory;
Expand All @@ -17,42 +18,42 @@ public function testFactory(): void
{
$changeTypeFactory = new ChangeTypeFactory();

self::assertEquals(new Added(), $changeTypeFactory->create(1));
//self::assertEquals(new Added(), $changeTypeFactory->create(ChangeType::ADDED));
self::assertEquals(
new Added(),
$changeTypeFactory->create(ChangeTypeFactory::ADDED)
$changeTypeFactory->create(ChangeType::ADDED)
);

self::assertEquals(new Changed(), $changeTypeFactory->create(2));
//self::assertEquals(new Changed(), $changeTypeFactory->create(ChangeType::CHANGED));
self::assertEquals(
new Changed(),
$changeTypeFactory->create(ChangeTypeFactory::CHANGED)
$changeTypeFactory->create(ChangeType::CHANGED)
);

self::assertEquals(new Deprecated(), $changeTypeFactory->create(3));
//self::assertEquals(new Deprecated(), $changeTypeFactory->create(ChangeType::DEPRECATED));
self::assertEquals(
new Deprecated(),
$changeTypeFactory->create(ChangeTypeFactory::DEPRECATED)
$changeTypeFactory->create(ChangeType::DEPRECATED)
);

self::assertEquals(new Removed(), $changeTypeFactory->create(4));
//self::assertEquals(new Removed(), $changeTypeFactory->create(ChangeType::REMOVED));
self::assertEquals(
new Removed(),
$changeTypeFactory->create(ChangeTypeFactory::REMOVED)
$changeTypeFactory->create(ChangeType::REMOVED)
);

self::assertEquals(new Fixed(), $changeTypeFactory->create(5));
//self::assertEquals(new Fixed(), $changeTypeFactory->create(ChangeType::FIXED));
self::assertEquals(
new Fixed(),
$changeTypeFactory->create(ChangeTypeFactory::FIXED)
$changeTypeFactory->create(ChangeType::FIXED)
);

self::assertEquals(new SECURITY(), $changeTypeFactory->create(6));
//self::assertEquals(new SECURITY(), $changeTypeFactory->create(ChangeType::SECURITY));
self::assertEquals(
new SECURITY(),
$changeTypeFactory->create(ChangeTypeFactory::SECURITY)
$changeTypeFactory->create(ChangeType::SECURITY)
);

self::assertEquals(new Added(), $changeTypeFactory->create(11111));
// self::assertEquals(new Added(), $changeTypeFactory->create(11111));
}
}

0 comments on commit 06a8ea0

Please sign in to comment.