Skip to content

Commit c5bcdd6

Browse files
committed
Update PHP to version ^7.4
1 parent 0260e59 commit c5bcdd6

10 files changed

+149
-172
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33

44
# Composer
55
/vendor
6-
composer.lock
6+
composer.lock
7+
8+
# PHPUnit Cache
9+
/.phpunit.result.cache

Enum.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace vjik\enum;
46

57
use LogicException;
@@ -13,25 +15,14 @@
1315
*/
1416
abstract class Enum
1517
{
16-
17-
/**
18-
* @var array
19-
*/
20-
protected static $_cacheItems = [];
21-
22-
/**
23-
* @var array
24-
*/
25-
protected static $_cacheInstances = [];
18+
protected static array $cacheItems = [];
19+
protected static array $cacheInstances = [];
2620

2721
/**
2822
* @var int|string
2923
*/
3024
protected $id;
3125

32-
/**
33-
* @var string
34-
*/
3526
protected $name;
3627

3728
/**
@@ -56,13 +47,13 @@ public function __construct($id)
5647
* @throws UnexpectedValueException
5748
* @since 2.1.0
5849
*/
59-
public static function get($id)
50+
public static function get($id): self
6051
{
6152
$key = get_called_class() . '~' . $id;
62-
if (empty(static::$_cacheInstances[$key])) {
63-
static::$_cacheInstances[$key] = new static($id);
53+
if (empty(static::$cacheInstances[$key])) {
54+
static::$cacheInstances[$key] = new static($id);
6455
}
65-
return static::$_cacheInstances[$key];
56+
return static::$cacheInstances[$key];
6657
}
6758

6859
/**
@@ -72,7 +63,7 @@ public static function get($id)
7263
* @return bool
7364
* @throws ReflectionException
7465
*/
75-
public static function isValid($id, array $filter = [])
66+
public static function isValid($id, array $filter = []): bool
7667
{
7768
return in_array($id, static::toIds($filter), true);
7869
}
@@ -83,10 +74,10 @@ public static function isValid($id, array $filter = [])
8374
* @return array enum-значение - ключ, массив с данными - значение
8475
* @throws ReflectionException
8576
*/
86-
public static function toArray(array $filter = [])
77+
public static function toArray(array $filter = []): array
8778
{
8879
$class = get_called_class();
89-
if (!array_key_exists($class, static::$_cacheItems)) {
80+
if (!array_key_exists($class, static::$cacheItems)) {
9081
$reflection = new \ReflectionClass($class);
9182
if (is_callable([$class, 'items'])) {
9283
/** @noinspection PhpUndefinedMethodInspection */
@@ -103,9 +94,9 @@ public static function toArray(array $filter = [])
10394
}
10495
$items[$constant]['id'] = $constant;
10596
}
106-
static::$_cacheItems[$class] = $items;
97+
static::$cacheItems[$class] = $items;
10798
}
108-
$items = array_filter(static::$_cacheItems[$class], function ($item) use ($class, $filter) {
99+
$items = array_filter(static::$cacheItems[$class], function ($item) use ($class, $filter) {
109100
foreach ($filter as $key => $filterItem) {
110101
if (is_int($key)) {
111102
$operator = $filterItem[0];
@@ -177,7 +168,7 @@ public static function toArray(array $filter = [])
177168
* @return array
178169
* @throws ReflectionException
179170
*/
180-
public static function toIds(array $filter = [])
171+
public static function toIds(array $filter = []): array
181172
{
182173
$ids = [];
183174
foreach (static::toArray($filter) as $item) {
@@ -192,7 +183,7 @@ public static function toIds(array $filter = [])
192183
* @return array enum-значение - ключ, имя - значение
193184
* @throws ReflectionException
194185
*/
195-
public static function toList(array $filter = [])
186+
public static function toList(array $filter = []): array
196187
{
197188
$list = [];
198189
foreach (static::toArray($filter) as $id => $data) {
@@ -207,7 +198,7 @@ public static function toList(array $filter = [])
207198
* @return array
208199
* @throws ReflectionException
209200
*/
210-
public static function toObjects(array $filter = [])
201+
public static function toObjects(array $filter = []): array
211202
{
212203
$objects = [];
213204
foreach (static::toIds($filter) as $id) {

composer.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vjik/php-enum",
3-
"description": "PHP 5.4+ Enum implementation",
3+
"description": "PHP Enum implementation",
44
"version": "2.2.0",
55
"type": "library",
66
"keywords": [
@@ -23,11 +23,13 @@
2323
"vjik\\enum\\": ""
2424
}
2525
},
26+
"require": {
27+
"php": "^7.4"
28+
},
2629
"require-dev": {
27-
"phpunit/phpunit": "^4.8",
28-
"yiisoft/yii2-coding-standards": "2.*"
30+
"phpunit/phpunit": "^9.4"
2931
},
30-
"require": {
31-
"php": ">=5.4.0"
32+
"config": {
33+
"sort-packages": true
3234
}
33-
}
35+
}

tests/BaseTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace vjik\enum\tests;
46

7+
use PHPUnit\Framework\TestCase;
58
use vjik\enum\tests\enums\Pure;
69
use vjik\enum\tests\enums\WithData;
710
use vjik\enum\tests\enums\WithName;
811

9-
class EnumTest extends \PHPUnit_Framework_TestCase
12+
final class BaseTest extends TestCase
1013
{
11-
protected $pure;
12-
protected $withName;
13-
protected $withData;
14+
protected Pure $pure;
15+
protected WithName $withName;
16+
protected WithData $withData;
1417

15-
protected function setUp()
18+
protected function setUp(): void
1619
{
1720
$this->pure = new Pure(Pure::FOO);
1821
$this->withName = new WithName(WithName::FOO);
1922
$this->withData = new WithData(WithData::ONE);
2023
}
2124

22-
public function testGetId()
25+
public function testGetId(): void
2326
{
2427
$this->assertEquals(Pure::FOO, $this->pure->id);
2528
$this->assertEquals((string)Pure::FOO, $this->pure);
@@ -31,7 +34,7 @@ public function testGetId()
3134
$this->assertEquals((string)WithData::ONE, $this->withData);
3235
}
3336

34-
public function testGetName()
37+
public function testGetName(): void
3538
{
3639
$this->assertEquals(Pure::FOO, $this->pure->name);
3740
$this->assertEquals('Foo Name', $this->withName->name);

tests/PureTest.php

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace vjik\enum\tests;
46

7+
use PHPUnit\Framework\TestCase;
58
use UnexpectedValueException;
69
use vjik\enum\tests\enums\Pure;
710

8-
class PureTest extends \PHPUnit_Framework_TestCase
11+
final class PureTest extends TestCase
912
{
10-
protected $enum;
13+
protected Pure $enum;
1114

12-
protected function setUp()
15+
protected function setUp(): void
1316
{
1417
$this->enum = new Pure(Pure::FOO);
1518
}
1619

17-
18-
/**
19-
* @dataProvider invalidIdProvider
20-
* @expectedException UnexpectedValueException
21-
*/
22-
public function testCreateWithInvalidId($id)
23-
{
24-
new Pure($id);
25-
}
26-
27-
28-
/**
29-
* @return array
30-
*/
31-
public function invalidIdProvider()
20+
public function dataCreateWithInvalidId(): array
3221
{
3322
return [
3423
[0],
@@ -38,20 +27,18 @@ public function invalidIdProvider()
3827
];
3928
}
4029

41-
4230
/**
43-
* @dataProvider isIdProvider
31+
* @dataProvider dataCreateWithInvalidId
32+
*
33+
* @param mixed $id
4434
*/
45-
public function testIsValid($id, $isValid)
35+
public function testCreateWithInvalidId($id): void
4636
{
47-
$this->assertSame(Pure::isValid($id), $isValid);
37+
$this->expectException(UnexpectedValueException::class);
38+
new Pure($id);
4839
}
4940

50-
51-
/**
52-
* @return array
53-
*/
54-
public function isIdProvider()
41+
public function dataIsValid(): array
5542
{
5643
return [
5744
[0, false],
@@ -64,8 +51,18 @@ public function isIdProvider()
6451
];
6552
}
6653

54+
/**
55+
* @dataProvider dataIsValid
56+
*
57+
* @param mixed $id
58+
* @param bool $isValid
59+
*/
60+
public function testIsValid($id, bool $isValid): void
61+
{
62+
$this->assertSame(Pure::isValid($id), $isValid);
63+
}
6764

68-
public function testToArray()
65+
public function testToArray(): void
6966
{
7067
$this->assertSame([
7168
Pure::FOO => [
@@ -88,7 +85,7 @@ public function testToArray()
8885
}
8986

9087

91-
public function testToList()
88+
public function testToList(): void
9289
{
9390
$this->assertSame([
9491
Pure::FOO => Pure::FOO,
@@ -99,7 +96,7 @@ public function testToList()
9996
}
10097

10198

102-
public function testToIds()
99+
public function testToIds(): void
103100
{
104101
$this->assertSame([
105102
Pure::FOO,
@@ -110,7 +107,7 @@ public function testToIds()
110107
}
111108

112109

113-
public function testToObjects()
110+
public function testToObjects(): void
114111
{
115112
$this->assertEquals([
116113
Pure::FOO => new Pure(Pure::FOO),

0 commit comments

Comments
 (0)