Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PhpStorm stubs #2034

Merged
merged 2 commits into from Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -13,7 +13,7 @@
"hoa/compiler": "3.17.08.08",
"hoa/exception": "^1.0",
"hoa/regex": "1.17.01.13",
"jetbrains/phpstorm-stubs": "dev-master#a221d6e3064973fbad90af817f4d380d84baa74c",
"jetbrains/phpstorm-stubs": "dev-master#d07464d9afbfd62c9dd7ae830c91ec0711e13a46",
"nette/bootstrap": "^3.0",
"nette/di": "^3.0.11",
"nette/finder": "^2.5",
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions conf/config.neon
Expand Up @@ -1347,6 +1347,11 @@ services:
tags:
- phpstan.dynamicStaticMethodThrowTypeExtension

-
class: PHPStan\Type\Php\DateTimeZoneConstructorThrowTypeExtension
tags:
- phpstan.dynamicStaticMethodThrowTypeExtension

-
class: PHPStan\Type\Php\DsMapDynamicReturnTypeExtension
tags:
Expand Down
50 changes: 50 additions & 0 deletions src/Type/Php/DateTimeZoneConstructorThrowTypeExtension.php
@@ -0,0 +1,50 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use DateTimeZone;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicStaticMethodThrowTypeExtension;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use function count;

class DateTimeZoneConstructorThrowTypeExtension implements DynamicStaticMethodThrowTypeExtension
{

public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === '__construct' && $methodReflection->getDeclaringClass()->getName() === DateTimeZone::class;
}

public function getThrowTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
{
if (count($methodCall->getArgs()) === 0) {
return null;
}

$valueType = $scope->getType($methodCall->getArgs()[0]->value);
$constantStrings = TypeUtils::getConstantStrings($valueType);

foreach ($constantStrings as $constantString) {
try {
new DateTimeZone($constantString->getValue());
} catch (\Exception $e) { // phpcs:ignore
return $methodReflection->getThrowType();
}

$valueType = TypeCombinator::remove($valueType, $constantString);
}

if (!$valueType instanceof NeverType) {
return $methodReflection->getThrowType();
}

return null;
}

}
Expand Up @@ -40,6 +40,10 @@ public function testRule(): void
'Method MissingExceptionMethodThrows\Foo::doLorem2() throws checked exception InvalidArgumentException but it\'s missing from the PHPDoc @throws tag.',
34,
],
[
'Method MissingExceptionMethodThrows\Foo::dateTimeZoneDoesThrows() throws checked exception Exception but it\'s missing from the PHPDoc @throws tag.',
95,
],
]);
}

Expand Down
Expand Up @@ -85,4 +85,14 @@ private function throwsInterface(): void

}

public function dateTimeZoneDoesNotThrow(): void
{
new \DateTimeZone('UTC');
}

public function dateTimeZoneDoesThrows(string $tz): void
{
new \DateTimeZone($tz);
}

}