Skip to content

Commit

Permalink
Fix the Unsafe usage of new static() PHPStan error
Browse files Browse the repository at this point in the history
  • Loading branch information
meritoo committed Nov 14, 2024
1 parent 172df07 commit 9f71420
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 48 deletions.
19 changes: 18 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.3
1.3.0
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/Exception/Base/UnknownTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 7 additions & 1 deletion src/Exception/Type/UnknownDatePartTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 3 additions & 1 deletion src/Exception/Type/UnknownOopVisibilityTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Traits/Test/Base/BaseTestCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Type/Base/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Utilities/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Exception/Base/UnknownTypeExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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));
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Type/Base/BaseTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -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));
}
}

Expand Down
20 changes: 10 additions & 10 deletions tests/Type/DatePartTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Type/DatePeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
Expand Down
12 changes: 6 additions & 6 deletions tests/Type/OopVisibilityTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
Expand Down

0 comments on commit 9f71420

Please sign in to comment.