Skip to content

Commit

Permalink
cs
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2022
1 parent a73c124 commit 8b0d808
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/PhpGenerator/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function extractMethodBodies(string $className): array

$res = [];
foreach ($nodeFinder->findInstanceOf($classNode, Node\Stmt\ClassMethod::class) as $methodNode) {
/** @var Node\Stmt\ClassMethod $methodNode */
assert($methodNode instanceof Node\Stmt\ClassMethod);
if ($methodNode->stmts) {
$res[$methodNode->name->toString()] = $this->getReformattedContents($methodNode->stmts, 2);
}
Expand All @@ -83,11 +83,11 @@ public function extractMethodBodies(string $className): array

public function extractFunctionBody(string $name): ?string
{
/** @var Node\Stmt\Function_ $functionNode */
$functionNode = (new NodeFinder)->findFirst(
$this->statements,
fn(Node $node) => $node instanceof Node\Stmt\Function_ && $node->namespacedName->toString() === $name,
);
assert($functionNode instanceof Node\Stmt\Function_);

return $this->getReformattedContents($functionNode->stmts, 1);
}
Expand Down
21 changes: 11 additions & 10 deletions src/PhpGenerator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function fromClassReflection(
\ReflectionClass $from,
bool $withBodies = false,
?bool $materializeTraits = null,
): ClassLike {
): ClassLike
{
if ($materializeTraits !== null) {
trigger_error(__METHOD__ . '() parameter $materializeTraits has been removed (is always false).', E_USER_DEPRECATED);
}
Expand Down Expand Up @@ -70,7 +71,7 @@ public function fromClassReflection(
}

$class->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
$class->setAttributes(self::getAttributes($from));
$class->setAttributes($this->getAttributes($from));
if ($from->getParentClass()) {
$class->setExtends($from->getParentClass()->name);
$class->setImplements(array_diff($class->getImplements(), $from->getParentClass()->getInterfaceNames()));
Expand Down Expand Up @@ -160,7 +161,7 @@ public function fromMethodReflection(\ReflectionMethod $from): Method
$method->setReturnReference($from->returnsReference());
$method->setVariadic($from->isVariadic());
$method->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
$method->setAttributes(self::getAttributes($from));
$method->setAttributes($this->getAttributes($from));
$method->setReturnType((string) $from->getReturnType());

return $method;
Expand All @@ -177,7 +178,7 @@ public function fromFunctionReflection(\ReflectionFunction $from, bool $withBody
$function->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
}

$function->setAttributes(self::getAttributes($from));
$function->setAttributes($this->getAttributes($from));
$function->setReturnType((string) $from->getReturnType());

if ($withBody) {
Expand All @@ -196,8 +197,8 @@ public function fromCallable(callable $from): Method|GlobalFunction|Closure
{
$ref = Nette\Utils\Callback::toReflection($from);
return $ref instanceof \ReflectionMethod
? self::fromMethodReflection($ref)
: self::fromFunctionReflection($ref);
? $this->fromMethodReflection($ref)
: $this->fromFunctionReflection($ref);
}


Expand Down Expand Up @@ -226,7 +227,7 @@ public function fromParameterReflection(\ReflectionParameter $from): Parameter
$param->setNullable($param->isNullable() && $param->getDefaultValue() !== null);
}

$param->setAttributes(self::getAttributes($from));
$param->setAttributes($this->getAttributes($from));
return $param;
}

Expand All @@ -238,7 +239,7 @@ public function fromConstantReflection(\ReflectionClassConstant $from): Constant
$const->setVisibility($this->getVisibility($from));
$const->setFinal(PHP_VERSION_ID >= 80100 ? $from->isFinal() : false);
$const->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
$const->setAttributes(self::getAttributes($from));
$const->setAttributes($this->getAttributes($from));
return $const;
}

Expand All @@ -248,7 +249,7 @@ public function fromCaseReflection(\ReflectionClassConstant $from): EnumCase
$const = new EnumCase($from->name);
$const->setValue($from->getValue()->value ?? null);
$const->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
$const->setAttributes(self::getAttributes($from));
$const->setAttributes($this->getAttributes($from));
return $const;
}

Expand All @@ -265,7 +266,7 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
$prop->setInitialized($from->hasType() && array_key_exists($prop->getName(), $defaults));
$prop->setReadOnly(PHP_VERSION_ID >= 80100 ? $from->isReadOnly() : false);
$prop->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
$prop->setAttributes(self::getAttributes($from));
$prop->setAttributes($this->getAttributes($from));
return $prop;
}

Expand Down
21 changes: 11 additions & 10 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");

return $this->printDocComment($function)
. self::printAttributes($function->getAttributes())
. $this->printAttributes($function->getAttributes())
. $line
. $this->printParameters($function, strlen($line) + strlen($returnType) + 2) // 2 = parentheses
. $returnType
Expand All @@ -72,7 +72,7 @@ public function printClosure(Closure $closure, ?PhpNamespace $namespace = null):
$body = Helpers::simplifyTaggedNames($closure->getBody(), $this->namespace);
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");

return self::printAttributes($closure->getAttributes(), inline: true)
return $this->printAttributes($closure->getAttributes(), inline: true)
. 'function '
. ($closure->getReturnReference() ? '&' : '')
. $this->printParameters($closure)
Expand All @@ -93,7 +93,7 @@ public function printArrowFunction(Closure $closure, ?PhpNamespace $namespace =

$body = Helpers::simplifyTaggedNames($closure->getBody(), $this->namespace);

return self::printAttributes($closure->getAttributes())
return $this->printAttributes($closure->getAttributes())
. 'fn'
. ($closure->getReturnReference() ? '&' : '')
. $this->printParameters($closure)
Expand All @@ -120,7 +120,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
$braceOnNextLine = $this->bracesOnNextLine && !str_contains($params, "\n");

return $this->printDocComment($method)
. self::printAttributes($method->getAttributes())
. $this->printAttributes($method->getAttributes())
. $line
. $params
. $returnType
Expand All @@ -133,7 +133,8 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
public function printClass(
ClassType|InterfaceType|TraitType|EnumType $class,
?PhpNamespace $namespace = null,
): string {
): string
{
$this->namespace = $this->resolveTypes ? $namespace : null;
$class->validate();
$resolver = $this->namespace
Expand All @@ -159,7 +160,7 @@ public function printClass(
foreach ($class->getCases() as $case) {
$enumType ??= is_scalar($case->getValue()) ? get_debug_type($case->getValue()) : null;
$cases[] = $this->printDocComment($case)
. self::printAttributes($case->getAttributes())
. $this->printAttributes($case->getAttributes())
. 'case ' . $case->getName()
. ($case->getValue() === null ? '' : ' = ' . $this->dump($case->getValue()))
. ";\n";
Expand All @@ -174,7 +175,7 @@ public function printClass(
. 'const ' . $const->getName() . ' = ';

$consts[] = $this->printDocComment($const)
. self::printAttributes($const->getAttributes())
. $this->printAttributes($const->getAttributes())
. $def
. $this->dump($const->getValue(), strlen($def)) . ";\n";
}
Expand Down Expand Up @@ -205,7 +206,7 @@ public function printClass(
. '$' . $property->getName());

$properties[] = $this->printDocComment($property)
. self::printAttributes($property->getAttributes())
. $this->printAttributes($property->getAttributes())
. $def
. ($property->getValue() === null && !$property->isInitialized()
? ''
Expand Down Expand Up @@ -243,7 +244,7 @@ public function printClass(
$line[] = $class->getName() ? null : '{';

return $this->printDocComment($class)
. self::printAttributes($class->getAttributes())
. $this->printAttributes($class->getAttributes())
. implode(' ', array_filter($line))
. ($class->getName() ? "\n{\n" : "\n")
. ($members ? $this->indent(implode("\n", $members)) : '')
Expand Down Expand Up @@ -333,7 +334,7 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int
$promoted = $param instanceof PromotedParameter ? $param : null;
$params[] =
($promoted ? $this->printDocComment($promoted) : '')
. ($attrs = self::printAttributes($param->getAttributes(), inline: true))
. ($attrs = $this->printAttributes($param->getAttributes(), inline: true))
. ($promoted ?
($promoted->getVisibility() ?: 'public')
. ($promoted->isReadOnly() && $type ? ' readonly' : '')
Expand Down
3 changes: 1 addition & 2 deletions tests/PhpGenerator/Method.returnTypes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ namespace A
}
}

namespace
{
namespace {
use Nette\PhpGenerator\Method;
use Tester\Assert;

Expand Down

0 comments on commit 8b0d808

Please sign in to comment.