-
Notifications
You must be signed in to change notification settings - Fork 776
Extract CodeGen namespace from LintMixinCommand #1716
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-License-Identifier: MIT | ||
| * SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| * SPDX-FileContributor: Alexandre Gomes Gaigalas <[email protected]> | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Respect\Dev\CodeGen\Attributes; | ||
|
|
||
| use Attribute; | ||
|
|
||
| #[Attribute(Attribute::TARGET_CLASS)] | ||
| final readonly class Mixin | ||
| { | ||
| /** | ||
| * @param array<string> $exclude | ||
| * @param array<string> $include | ||
| */ | ||
| public function __construct( | ||
| public string|null $prefix = null, | ||
| public bool $prefixParameter = false, | ||
| public bool $requireInclusion = false, | ||
| public array $exclude = [], | ||
| public array $include = [], | ||
| ) { | ||
| } | ||
| } | ||
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,28 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-License-Identifier: MIT | ||
| * SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| * SPDX-FileContributor: Alexandre Gomes Gaigalas <[email protected]> | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Respect\Dev\CodeGen; | ||
|
|
||
| final readonly class InterfaceConfig | ||
| { | ||
| /** | ||
| * @param array<string> $rootExtends | ||
| * @param array<string> $rootUses | ||
| */ | ||
| public function __construct( | ||
| public string $suffix, | ||
| public string $returnType, | ||
| public bool $static = false, | ||
| public array $rootExtends = [], | ||
| public string|null $rootComment = null, | ||
| public array $rootUses = [], | ||
| ) { | ||
| } | ||
| } |
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,169 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-License-Identifier: MIT | ||
| * SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| * SPDX-FileContributor: Alexandre Gomes Gaigalas <[email protected]> | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Respect\Dev\CodeGen; | ||
|
|
||
| use Nette\PhpGenerator\Method; | ||
| use Nette\PhpGenerator\PhpNamespace; | ||
| use ReflectionClass; | ||
| use ReflectionNamedType; | ||
| use ReflectionParameter; | ||
| use ReflectionUnionType; | ||
|
|
||
| use function count; | ||
| use function implode; | ||
| use function in_array; | ||
| use function is_object; | ||
| use function lcfirst; | ||
| use function preg_replace; | ||
| use function sort; | ||
| use function str_starts_with; | ||
| use function ucfirst; | ||
|
|
||
| final class MethodBuilder | ||
| { | ||
| /** | ||
| * @param array<string> $excludedTypePrefixes | ||
| * @param array<string> $excludedTypeNames | ||
| */ | ||
| public function __construct( | ||
| private readonly array $excludedTypePrefixes = [], | ||
| private readonly array $excludedTypeNames = [], | ||
| ) { | ||
| } | ||
|
|
||
| public function build( | ||
| PhpNamespace $namespace, | ||
| ReflectionClass $validatorReflection, | ||
| string $returnType, | ||
| string|null $prefix = null, | ||
| bool $static = false, | ||
| ReflectionParameter|null $prefixParameter = null, | ||
| ): Method { | ||
| $originalName = $validatorReflection->getShortName(); | ||
| $name = $prefix ? $prefix . ucfirst($originalName) : lcfirst($originalName); | ||
|
|
||
| $method = new Method($name); | ||
| $method->setPublic()->setReturnType($returnType); | ||
|
|
||
| if ($static) { | ||
| $method->setStatic(); | ||
| } | ||
|
|
||
| if ($prefixParameter !== null) { | ||
| $this->addPrefixParameter($method, $prefixParameter); | ||
| } | ||
|
|
||
| $constructor = $validatorReflection->getConstructor(); | ||
| if ($constructor === null) { | ||
| return $method; | ||
| } | ||
|
|
||
| $comment = $constructor->getDocComment(); | ||
| if ($comment) { | ||
| $method->addComment(preg_replace('@(/\*\* *| +\* +| +\*/)@', '', $comment)); | ||
| } | ||
|
|
||
| foreach ($constructor->getParameters() as $reflectionParameter) { | ||
| $this->addParameter($method, $reflectionParameter, $namespace); | ||
| } | ||
|
|
||
| return $method; | ||
| } | ||
|
|
||
| private function addPrefixParameter(Method $method, ReflectionParameter $reflectionParameter): void | ||
| { | ||
| $type = $reflectionParameter->getType(); | ||
| $types = []; | ||
|
|
||
| if ($type instanceof ReflectionUnionType) { | ||
| foreach ($type->getTypes() as $subType) { | ||
| $types[] = $subType->getName(); | ||
| } | ||
|
|
||
| sort($types); | ||
| } elseif ($type instanceof ReflectionNamedType) { | ||
| $types[] = $type->getName(); | ||
| } | ||
|
|
||
| $method->addParameter($reflectionParameter->getName())->setType(implode('|', $types)); | ||
| } | ||
|
|
||
| private function addParameter( | ||
| Method $method, | ||
| ReflectionParameter $reflectionParameter, | ||
| PhpNamespace $namespace, | ||
| ): void { | ||
| if ($reflectionParameter->isVariadic()) { | ||
| $method->setVariadic(); | ||
| } | ||
|
|
||
| $type = $reflectionParameter->getType(); | ||
| $types = []; | ||
|
|
||
| if ($type instanceof ReflectionUnionType) { | ||
| foreach ($type->getTypes() as $subType) { | ||
| $types[] = $subType->getName(); | ||
| if ($subType->isBuiltin()) { | ||
| continue; | ||
| } | ||
|
|
||
| $namespace->addUse($subType->getName()); | ||
| } | ||
| } elseif ($type instanceof ReflectionNamedType) { | ||
| $types[] = $type->getName(); | ||
|
|
||
| if ($this->isExcludedType($type->getName())) { | ||
| return; | ||
| } | ||
|
|
||
| if (!$type->isBuiltin()) { | ||
| $namespace->addUse($type->getName()); | ||
| } | ||
| } | ||
|
|
||
| $parameter = $method->addParameter($reflectionParameter->getName()); | ||
| $parameter->setType(implode('|', $types)); | ||
|
|
||
alganet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!$reflectionParameter->isDefaultValueAvailable()) { | ||
| $parameter->setNullable($reflectionParameter->isOptional()); | ||
| } | ||
|
|
||
| if (count($types) > 1 || $reflectionParameter->isVariadic()) { | ||
| $parameter->setNullable(false); | ||
| } | ||
|
|
||
| if (!$reflectionParameter->isDefaultValueAvailable()) { | ||
| return; | ||
| } | ||
|
|
||
| $defaultValue = $reflectionParameter->getDefaultValue(); | ||
| if (is_object($defaultValue)) { | ||
| $parameter->setDefaultValue(null); | ||
| $parameter->setNullable(true); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $parameter->setDefaultValue($defaultValue); | ||
| $parameter->setNullable(false); | ||
| } | ||
|
|
||
| private function isExcludedType(string $typeName): bool | ||
| { | ||
| foreach ($this->excludedTypePrefixes as $excludedPrefix) { | ||
| if (str_starts_with($typeName, $excludedPrefix)) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return in_array($typeName, $this->excludedTypeNames, true); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.