Created
April 16, 2025 13:45
-
-
Save janedbal/7e7b2b76a101dd8b6c9161dd4d2e1369 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types = 1); | |
namespace App\PHPStan\Extension; | |
use JetBrains\PhpStorm\Deprecated; | |
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass; | |
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClassConstant; | |
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum; | |
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnumBackedCase; | |
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnumUnitCase; | |
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionFunction; | |
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod; | |
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionProperty; | |
use PHPStan\Reflection\Deprecation\ClassConstantDeprecationExtension; | |
use PHPStan\Reflection\Deprecation\ClassDeprecationExtension; | |
use PHPStan\Reflection\Deprecation\Deprecation; | |
use PHPStan\Reflection\Deprecation\EnumCaseDeprecationExtension; | |
use PHPStan\Reflection\Deprecation\FunctionDeprecationExtension; | |
use PHPStan\Reflection\Deprecation\MethodDeprecationExtension; | |
use PHPStan\Reflection\Deprecation\PropertyDeprecationExtension; | |
final class JetBrainsDeprecatedExtension implements | |
ClassDeprecationExtension, | |
ClassConstantDeprecationExtension, | |
MethodDeprecationExtension, | |
PropertyDeprecationExtension, | |
FunctionDeprecationExtension, | |
EnumCaseDeprecationExtension | |
{ | |
/** | |
* @param ReflectionClass|ReflectionEnum $reflection | |
*/ | |
public function getClassDeprecation($reflection): ?Deprecation | |
{ | |
return $this->buildDeprecation($reflection); | |
} | |
public function getFunctionDeprecation(ReflectionFunction $reflection): ?Deprecation | |
{ | |
return $this->buildDeprecation($reflection); | |
} | |
public function getMethodDeprecation(ReflectionMethod $reflection): ?Deprecation | |
{ | |
return $this->buildDeprecation($reflection); | |
} | |
public function getPropertyDeprecation(ReflectionProperty $reflection): ?Deprecation | |
{ | |
return $this->buildDeprecation($reflection); | |
} | |
public function getClassConstantDeprecation(ReflectionClassConstant $reflection): ?Deprecation | |
{ | |
return $this->buildDeprecation($reflection); | |
} | |
public function getEnumCaseDeprecation($reflection): ?Deprecation | |
{ | |
return $this->buildDeprecation($reflection); | |
} | |
/** | |
* @param ReflectionEnumUnitCase|ReflectionEnumBackedCase|ReflectionFunction|ReflectionMethod|ReflectionProperty|ReflectionClassConstant|ReflectionClass|ReflectionEnum $reflection | |
*/ | |
private function buildDeprecation(object $reflection): ?Deprecation | |
{ | |
foreach ($reflection->getAttributes(Deprecated::class) as $attribute) { | |
$description = $attribute->getArguments()[0] ?? $attribute->getArguments()['reason'] ?? null; | |
return $description === null | |
? Deprecation::create() | |
: Deprecation::createWithDescription($description); | |
} | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
- | |
class: App\PHPStan\Extension\JetBrainsDeprecatedExtension | |
tags: | |
- phpstan.propertyDeprecationExtension | |
- phpstan.methodDeprecationExtension | |
- phpstan.classConstantDeprecationExtension | |
- phpstan.classDeprecationExtension | |
- phpstan.functionDeprecationExtension | |
- phpstan.enumCaseDeprecationExtension |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment