diff --git a/CHANGELOG.md b/CHANGELOG.md index bd7ceea..611d627 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,28 @@ Common and useful classes, methods, exceptions etc. -# 1.2.3 +# 1.3.0 1. Support integers by the `BaseType::isCorrectType()` method 2. Add PHPStan to GitHub Actions 3. Fix PHPStan errors - level `1` +4. Make the `BaseType::isCorrectType()` static method non-static + + Before: + + ```php + if (DatePartType::isCorrectType('day')) { + // ... + } + ``` + + After: + + ```php + if ((new DatePartType())->isCorrectType('day')) { + // ... + } + ``` # 1.2.2 diff --git a/TODO.md b/TODO.md index 472462e..f67d794 100644 --- a/TODO.md +++ b/TODO.md @@ -6,10 +6,10 @@ - [ ] `Undefined variable: $isEqual` - replace `eval()` with callable in [src/Utilities/Regex.php:151](./src/Utilities/Regex.php) -- [ ] `Unsafe usage of new static()` - chose one +- [x] `Unsafe usage of new static()` - chose one of [possible solutions](https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static) - in [src/Type/Base/BaseType.php:37](./src/Type/Base/BaseType.php) -- [ ] `Unsafe usage of new static()` - chose one +- [x] `Unsafe usage of new static()` - chose one of [possible solutions](https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static) - in [src/Exception/Base/UnknownTypeException.php:40](./src/Exception/Base/UnknownTypeException.php) - [ ] Clean and remove the [phpstan-baseline.neon](phpstan-baseline.neon) file finally diff --git a/VERSION b/VERSION index 0495c4a..f0bb29e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.3 +1.3.0 diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 03694d8..c8acb47 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,15 +1,5 @@ parameters: ignoreErrors: - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: src/Exception/Base/UnknownTypeException.php - - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: src/Type/Base/BaseType.php - - message: "#^Undefined variable\\: \\$isEqual$#" count: 1 diff --git a/src/Exception/Base/UnknownTypeException.php b/src/Exception/Base/UnknownTypeException.php index 790ea97..ca9700d 100644 --- a/src/Exception/Base/UnknownTypeException.php +++ b/src/Exception/Base/UnknownTypeException.php @@ -28,15 +28,14 @@ abstract class UnknownTypeException extends Exception * @param string $typeName Name of the something * @return UnknownTypeException */ - protected static function create($unknownType, BaseType $typeInstance, string $typeName): UnknownTypeException + protected static function createMessage($unknownType, BaseType $typeInstance, string $typeName): string { $template = 'The \'%s\' type of %s is unknown. Probably doesn\'t exist or there is a typo. You should use one' .' of these types: %s.'; $allTypes = $typeInstance->getAll(); $types = Arrays::values2string($allTypes, '', ', ') ?? '[types not found]'; - $message = sprintf($template, $unknownType, $typeName, $types); - return new static($message); + return sprintf($template, $unknownType, $typeName, $types); } } diff --git a/src/Exception/Type/UnknownDatePartTypeException.php b/src/Exception/Type/UnknownDatePartTypeException.php index 29e1d5a..363f317 100644 --- a/src/Exception/Type/UnknownDatePartTypeException.php +++ b/src/Exception/Type/UnknownDatePartTypeException.php @@ -28,6 +28,12 @@ class UnknownDatePartTypeException extends UnknownTypeException */ public static function createException(string $unknownDatePart, string $value): UnknownDatePartTypeException { - return parent::create($unknownDatePart, new DatePartType(), sprintf('date part (with value %s)', $value)); + $message = parent::createMessage( + $unknownDatePart, + new DatePartType(), + sprintf('date part (with value %s)', $value), + ); + + return new self($message); } } diff --git a/src/Exception/Type/UnknownOopVisibilityTypeException.php b/src/Exception/Type/UnknownOopVisibilityTypeException.php index 6463362..e998ef0 100644 --- a/src/Exception/Type/UnknownOopVisibilityTypeException.php +++ b/src/Exception/Type/UnknownOopVisibilityTypeException.php @@ -27,6 +27,8 @@ class UnknownOopVisibilityTypeException extends UnknownTypeException */ public static function createException(string $unknownType): UnknownOopVisibilityTypeException { - return parent::create($unknownType, new OopVisibilityType(), 'OOP-related visibility'); + $message = parent::createMessage($unknownType, new OopVisibilityType(), 'OOP-related visibility'); + + return new self($message); } } diff --git a/src/Traits/Test/Base/BaseTestCaseTrait.php b/src/Traits/Test/Base/BaseTestCaseTrait.php index 197609c..54b6b03 100644 --- a/src/Traits/Test/Base/BaseTestCaseTrait.php +++ b/src/Traits/Test/Base/BaseTestCaseTrait.php @@ -205,7 +205,7 @@ protected static function assertMethodArgumentsCount( protected static function assertMethodVisibility(ReflectionMethod $method, string $visibilityType): void { // Type of visibility is not correct? - if (!OopVisibilityType::isCorrectType($visibilityType)) { + if (!(new OopVisibilityType())->isCorrectType($visibilityType)) { throw UnknownOopVisibilityTypeException::createException($visibilityType); } diff --git a/src/Type/Base/BaseType.php b/src/Type/Base/BaseType.php index ca37d67..f610a35 100644 --- a/src/Type/Base/BaseType.php +++ b/src/Type/Base/BaseType.php @@ -30,8 +30,8 @@ public function getAll(): ?array return $this->all; } - public static function isCorrectType(null|string|int $type): bool + public function isCorrectType(null|string|int $type): bool { - return in_array($type, (new static())->getAll(), true); + return in_array($type, $this->getAll(), true); } } diff --git a/src/Utilities/Date.php b/src/Utilities/Date.php index 093c4a6..d77f59d 100644 --- a/src/Utilities/Date.php +++ b/src/Utilities/Date.php @@ -454,7 +454,7 @@ public static function getDatesForPeriod(string $period): ?DatePeriod * Type of period is incorrect? * Nothing to do */ - if (!DatePeriod::isCorrectType($period)) { + if (!(new DatePeriod())->isCorrectType($period)) { return null; } diff --git a/tests/Exception/Base/UnknownTypeExceptionTest.php b/tests/Exception/Base/UnknownTypeExceptionTest.php index 766f1dc..9f05518 100644 --- a/tests/Exception/Base/UnknownTypeExceptionTest.php +++ b/tests/Exception/Base/UnknownTypeExceptionTest.php @@ -73,7 +73,9 @@ class UnknownTestTypeException extends UnknownTypeException */ public static function createException(string $unknownType): UnknownTestTypeException { - return parent::create($unknownType, new TestType(), 'type of something used for testing'); + $message = parent::createMessage($unknownType, new TestType(), 'type of something used for testing'); + + return new self($message); } } @@ -94,7 +96,7 @@ class TestService */ public function getTranslatedType(string $type): string { - if (TestType::isCorrectType($type)) { + if ((new TestType())->isCorrectType($type)) { return ucfirst(str_replace('_', ' ', $type)); } diff --git a/tests/Type/Base/BaseTypeTest.php b/tests/Type/Base/BaseTypeTest.php index e380f3e..8becd8e 100644 --- a/tests/Type/Base/BaseTypeTest.php +++ b/tests/Type/Base/BaseTypeTest.php @@ -202,7 +202,7 @@ public function testGetAll(BaseType $type, array $expectedTypes): void */ public function testIsCorrectTypeUsingTestEmptyType(null|string|int $toVerifyType, bool $isCorrect): void { - self::assertEquals($isCorrect, TestEmptyType::isCorrectType($toVerifyType)); + self::assertEquals($isCorrect, (new TestEmptyType())->isCorrectType($toVerifyType)); } /** @@ -213,7 +213,7 @@ public function testIsCorrectTypeUsingTestEmptyType(null|string|int $toVerifyTyp */ public function testIsCorrectTypeUsingTestType(null|string|int $toVerifyType, bool $isCorrect): void { - self::assertEquals($isCorrect, TestType::isCorrectType($toVerifyType)); + self::assertEquals($isCorrect, (new TestType())->isCorrectType($toVerifyType)); } } diff --git a/tests/Type/DatePartTypeTest.php b/tests/Type/DatePartTypeTest.php index 5a24fe1..2c8daa4 100644 --- a/tests/Type/DatePartTypeTest.php +++ b/tests/Type/DatePartTypeTest.php @@ -30,52 +30,52 @@ class DatePartTypeTest extends BaseTypeTestCase public function provideTypeToVerify(): Generator { yield [ - DatePartType::isCorrectType(''), + (new DatePartType())->isCorrectType(''), false, ]; yield [ - DatePartType::isCorrectType(null), + (new DatePartType())->isCorrectType(null), false, ]; yield [ - DatePartType::isCorrectType('0'), + (new DatePartType())->isCorrectType('0'), false, ]; yield [ - DatePartType::isCorrectType('1'), + (new DatePartType())->isCorrectType('1'), false, ]; yield [ - DatePartType::isCorrectType('day'), + (new DatePartType())->isCorrectType('day'), true, ]; yield [ - DatePartType::isCorrectType('hour'), + (new DatePartType())->isCorrectType('hour'), true, ]; yield [ - DatePartType::isCorrectType('minute'), + (new DatePartType())->isCorrectType('minute'), true, ]; yield [ - DatePartType::isCorrectType('month'), + (new DatePartType())->isCorrectType('month'), true, ]; yield [ - DatePartType::isCorrectType('second'), + (new DatePartType())->isCorrectType('second'), true, ]; yield [ - DatePartType::isCorrectType('year'), + (new DatePartType())->isCorrectType('year'), true, ]; } diff --git a/tests/Type/DatePeriodTest.php b/tests/Type/DatePeriodTest.php index a45bb3d..bb2614c 100644 --- a/tests/Type/DatePeriodTest.php +++ b/tests/Type/DatePeriodTest.php @@ -219,27 +219,27 @@ public function provideDatePeriodAndUnknownDate(): ?Generator public function provideTypeToVerify(): Generator { yield [ - DatePeriod::isCorrectType(''), + (new DatePeriod())->isCorrectType(''), false, ]; yield [ - DatePeriod::isCorrectType('-1'), + (new DatePeriod())->isCorrectType('-1'), false, ]; yield [ - DatePeriod::isCorrectType('4'), + (new DatePeriod())->isCorrectType('4'), true, ]; yield [ - DatePeriod::isCorrectType('3'), + (new DatePeriod())->isCorrectType('3'), true, ]; yield [ - DatePeriod::isCorrectType('8'), + (new DatePeriod())->isCorrectType('8'), true, ]; } diff --git a/tests/Type/OopVisibilityTypeTest.php b/tests/Type/OopVisibilityTypeTest.php index cc929c3..4cd817a 100644 --- a/tests/Type/OopVisibilityTypeTest.php +++ b/tests/Type/OopVisibilityTypeTest.php @@ -30,32 +30,32 @@ class OopVisibilityTypeTest extends BaseTypeTestCase public function provideTypeToVerify(): Generator { yield [ - OopVisibilityType::isCorrectType(''), + (new OopVisibilityType())->isCorrectType(''), false, ]; yield [ - OopVisibilityType::isCorrectType(null), + (new OopVisibilityType())->isCorrectType(null), false, ]; yield [ - OopVisibilityType::isCorrectType('-1'), + (new OopVisibilityType())->isCorrectType('-1'), false, ]; yield [ - OopVisibilityType::isCorrectType('1'), + (new OopVisibilityType())->isCorrectType('1'), true, ]; yield [ - OopVisibilityType::isCorrectType('2'), + (new OopVisibilityType())->isCorrectType('2'), true, ]; yield [ - OopVisibilityType::isCorrectType('3'), + (new OopVisibilityType())->isCorrectType('3'), true, ]; }