-
Notifications
You must be signed in to change notification settings - Fork 1
Add php82 compatibility #88
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
Open
DotnDev
wants to merge
6
commits into
sidux:master
Choose a base branch
from
nenrici:add_php82_compatibility
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2cbd757
feat(chore): Upgrade to PHP 8.2.
nenrici 9219a6d
feat(php8): Added promoted parameters logic
DotnDev a7562f9
feat(php8): Added Enum logic
DotnDev ddea7ea
chore(parameter): Simplified promoted parameter logic
DotnDev 4cee7e5
chore(roave): Removed Roave lib usages
DotnDev 34c67e1
chore(rector): Removed Rector
DotnDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sidux\PhpGenerator\Helper; | ||
|
|
||
| use phpDocumentor\Reflection\DocBlock\Tags\Return_; | ||
| use phpDocumentor\Reflection\DocBlockFactory; | ||
| use phpDocumentor\Reflection\TypeResolver; | ||
| use phpDocumentor\Reflection\Types\Context; | ||
| use PHPStan\BetterReflection\BetterReflection; | ||
| use ReflectionClass; | ||
| use ReflectionFunction; | ||
| use ReflectionParameter; | ||
| use ReflectionProperty; | ||
| use ReflectionMethod; | ||
|
|
||
| final class ReflectionHelper | ||
| { | ||
| public static function getBodyCode(ReflectionFunction|ReflectionMethod|\PHPStan\BetterReflection\Reflection\ReflectionFunction $reflection, bool $isInterface = false): string | ||
| { | ||
| if ($isInterface || ($reflection instanceof ReflectionMethod && $reflection->isAbstract())) { | ||
| return ''; | ||
| } | ||
|
|
||
| $filename = $reflection->getFileName(); | ||
|
|
||
| if ($filename === false) { | ||
| throw new \LogicException("Filename is not set"); | ||
| } | ||
|
|
||
| $file = file($filename); | ||
|
|
||
| if ($file === false) { | ||
| throw new \LogicException("Unable to open file {$filename}."); | ||
| } | ||
|
|
||
| $startLine = self::getBodyCodeStartLine($reflection->getName(), $file); | ||
| $endLine = self::getBodyCodeEndLine($reflection->getName(), $file); | ||
|
|
||
| $length = $endLine - $startLine + 1; | ||
|
|
||
| if ($length <= 2) { | ||
| $length = 0; | ||
| } | ||
|
|
||
| $code = \array_slice($file, $startLine, $length); | ||
|
|
||
| return trim(preg_replace('/\s+{\n|\s+}\n/', '', implode('', $code))); | ||
| } | ||
|
|
||
| public static function getBodyCodeStartLine(string $functionOrMethodName, array $file): int | ||
| { | ||
| $startLine = 0; | ||
| $found = false; | ||
|
|
||
| foreach ($file as $lineNumber => $line) { | ||
| if (str_contains($line, $functionOrMethodName)) { | ||
| $startLine = $lineNumber; | ||
| $found = true; | ||
| } | ||
|
|
||
| if ($found && str_contains($line, '{')) { | ||
| $startLine = $lineNumber; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return $startLine; | ||
| } | ||
|
|
||
| public static function getBodyCodeEndLine(string $functionOrMethodName, array $file): int | ||
| { | ||
| $endLine = 0; | ||
| $found = false; | ||
|
|
||
| foreach ($file as $lineNumber => $line) { | ||
| if (str_contains($line, $functionOrMethodName)) { | ||
| $endLine = $lineNumber; | ||
| $found = true; | ||
| } | ||
|
|
||
| if ($found && str_contains($line, '}')) { | ||
| $endLine = $lineNumber; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return $endLine; | ||
| } | ||
|
|
||
| public static function getDocBlockReturnTypes(ReflectionFunction|ReflectionMethod|\PHPStan\BetterReflection\Reflection\ReflectionFunction $reflection): array | ||
| { | ||
| $docBlockFactory = DocBlockFactory::createInstance(); | ||
| $typeResolver = new TypeResolver(); | ||
| $docComment = $reflection->getDocComment(); | ||
| $context = $reflection->getNamespaceName() ? new Context($reflection->getNamespaceName()) : null; | ||
|
|
||
| if ($docComment === false) { | ||
| return []; | ||
| } | ||
|
|
||
| $returnTags = $docBlockFactory | ||
| ->create($docComment, $context) | ||
| ->getTagsByName('return'); | ||
|
|
||
| $types = []; | ||
|
|
||
| foreach ($returnTags as $returnTag) { | ||
| /** @var Return_ $returnTag */ | ||
| $returnTypes = explode('|', (string)$returnTag->getType()); | ||
|
|
||
| foreach ($returnTypes as $returnType) { | ||
| $type = $typeResolver->resolve($returnType, $context); | ||
| $types[] = $type; | ||
| } | ||
| } | ||
|
|
||
| return $types; | ||
| } | ||
|
|
||
| public static function getDocBlockTypes(ReflectionParameter|ReflectionProperty|\PHPStan\BetterReflection\Reflection\ReflectionParameter $reflection): array | ||
| { | ||
| $docBlockFactory = DocBlockFactory::createInstance(); | ||
| $typeResolver = new TypeResolver(); | ||
|
|
||
| if ($reflection instanceof ReflectionProperty) { | ||
| $docComment = $reflection->getDocComment(); | ||
| $context = new Context($reflection->getDeclaringClass()->getNamespaceName()); | ||
|
|
||
| } else { | ||
| $docComment = $reflection->getDeclaringFunction()->getDocComment(); | ||
| $namespace = $reflection->getDeclaringFunction()->getNamespaceName(); | ||
| $context = $namespace ? new Context($reflection->getDeclaringFunction()->getNamespaceName()) : null; | ||
| } | ||
|
|
||
| if ($docComment === false) { | ||
| return []; | ||
| } | ||
|
|
||
| $paramTags = $docBlockFactory | ||
| ->create($docComment, $context) | ||
| ->getTagsByName('param'); | ||
|
|
||
| $types = []; | ||
|
|
||
| foreach ($paramTags as $paramTag) { | ||
| /** @var \phpDocumentor\Reflection\DocBlock\Tags\Param $paramTag */ | ||
| $paramTypes = explode('|', (string)$paramTag->getType()); | ||
|
|
||
| foreach ($paramTypes as $paramType) { | ||
| $type = $typeResolver->resolve($paramType, $context); | ||
| $types[] = $type; | ||
| } | ||
| } | ||
|
|
||
| return $types; | ||
| } | ||
|
|
||
| /** | ||
| * @throws \Exception | ||
| */ | ||
| public static function createReflectionMethodFromName(string $className, string $methodName): ReflectionMethod | ||
| { | ||
| try { | ||
| return self::createClassFromName($className)->getMethod($methodName); | ||
| } catch (\ReflectionException) { | ||
| throw new \Exception(sprintf('Could not find class: %s', $className)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @throws \Exception | ||
| */ | ||
| public static function createReflectionFunctionFromName(string $functionName): \PHPStan\BetterReflection\Reflection\ReflectionFunction | ||
| { | ||
| return (new BetterReflection())->reflector()->reflectFunction($functionName); | ||
| } | ||
|
|
||
| /** | ||
| * @throws \ReflectionException | ||
| */ | ||
| public static function createClassFromName(string $className): ReflectionClass | ||
| { | ||
| return new ReflectionClass($className); | ||
| } | ||
|
|
||
| /** | ||
| * @throws \ReflectionException | ||
| */ | ||
| public static function createFunctionFromClosure(\Closure $closure): ReflectionFunction | ||
| { | ||
| return new ReflectionFunction($closure); | ||
| } | ||
|
|
||
| public static function createClassFromInstance(object $instance): ReflectionClass | ||
| { | ||
| return new ReflectionClass($instance); | ||
| } | ||
|
|
||
| public static function createPropertyFromName(string $className, string $propertyName): ReflectionProperty | ||
| { | ||
| return self::createClassFromName($className)->getProperty($propertyName); | ||
| } | ||
|
|
||
| public static function createPropertyFromInstance(object $instance, string $propertyName): ReflectionProperty | ||
| { | ||
| return self::createClassFromInstance($instance)->getProperty($propertyName); | ||
| } | ||
|
|
||
| /** | ||
| * @throws \ReflectionException | ||
| */ | ||
| public static function createParameterFromInstanceAndMethod(object $instance, string $methodName, string $parameterName): ReflectionParameter | ||
| { | ||
| $parameters = self::createClassFromInstance($instance)->getMethod($methodName)->getParameters(); | ||
|
|
||
| foreach ($parameters as $parameter) { | ||
| if ($parameter->getName() === $parameterName) { | ||
| return $parameter; | ||
| } | ||
| } | ||
|
|
||
| throw new \ReflectionException(sprintf('Could not find parameter: %s', $parameterName)); | ||
| } | ||
|
|
||
| public static function createParameterFromClassNameAndMethod(string $className, string $methodName, string $parameterName): ReflectionParameter | ||
| { | ||
| $parameters = self::createClassFromName($className)->getMethod($methodName)->getParameters(); | ||
|
|
||
| foreach ($parameters as $parameter) { | ||
| if ($parameter->getName() === $parameterName) { | ||
| return $parameter; | ||
| } | ||
| } | ||
|
|
||
| throw new \ReflectionException(sprintf('Could not find parameter: %s', $parameterName)); | ||
| } | ||
| } |
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sidux\PhpGenerator\Model; | ||
|
|
||
| use Sidux\PhpGenerator\Model\Contract\Member; | ||
|
|
||
| class EnumCase extends Member implements \Stringable | ||
| { | ||
| use Part\NamespaceAwareTrait; | ||
| use Part\CommentAwareTrait; | ||
| use Part\ValueAwareTrait; | ||
|
|
||
| public static function create(string $name): self | ||
| { | ||
| return new self($name); | ||
| } | ||
|
|
||
| public function __toString() | ||
| { | ||
| $output = ''; | ||
| $output .= "case {$this->getName()} = {$this->getValue()};"; | ||
|
|
||
| return $output; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed usages of this lib, to review and to remove once all done