Skip to content

Commit 8bd0712

Browse files
authored
Update nette php-generator (#1644)
1 parent 1bbb3b2 commit 8bd0712

File tree

9 files changed

+39
-160
lines changed

9 files changed

+39
-160
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
"license": "MIT",
55
"type": "library",
66
"require": {
7-
"php": "^7.2.5 || ^8.0",
7+
"php": "^8.2",
88
"ext-SimpleXML": "*",
99
"ext-dom": "*",
1010
"ext-json": "*",
1111
"friendsofphp/php-cs-fixer": "~3.24.0",
12-
"nette/php-generator": "^3.6.4",
13-
"nette/utils": "^3.0",
12+
"nette/php-generator": "^3.6 || ^4.1",
13+
"nette/utils": "^3.0 || ^4.0",
14+
"nikic/php-parser": "^4.0",
1415
"symfony/console": "^4.4 || ^5.0 || ^6.0 || ^7.0",
1516
"symfony/finder": "^4.4 || ^5.0 || ^6.0 || ^7.0",
1617
"symfony/http-client": "^4.4 || ^5.0 || ^6.0 || ^7.0",

src/File/Printer.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@
1717
*/
1818
class Printer extends BasePrinter
1919
{
20-
/**
21-
* @var string
22-
*/
23-
protected $indentation = ' ';
20+
public function __construct()
21+
{
22+
parent::__construct();
23+
24+
$this->indentation = ' ';
25+
$this->linesBetweenMethods = 1;
26+
$this->wrapLength = 1000;
27+
}
2428

2529
/**
26-
* @var int
30+
* @param ClassType $class
2731
*/
28-
protected $linesBetweenMethods = 1;
29-
30-
public function printClass(ClassType $class, ?PhpNamespace $namespace = null): string
32+
public function printClass($class, ?PhpNamespace $namespace = null): string
3133
{
3234
$methods = $class->getMethods();
3335
ksort($methods);

src/Generator/ExceptionGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public function generate(Operation $operation, ExceptionShape $shape): ClassName
6565
}
6666

6767
if ($shape->isSenderFault()) {
68-
$classBuilder->addExtend(ClientException::class);
68+
$classBuilder->setExtends(ClientException::class);
6969
$classBuilder->addUse(ClientException::class);
7070
} else {
71-
$classBuilder->addExtend(ServerException::class);
71+
$classBuilder->setExtends(ServerException::class);
7272
$classBuilder->addUse(ServerException::class);
7373
}
7474

src/Generator/InputGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function generate(Operation $operation): ClassName
298298

299299
$this->addUse($shape, $classBuilder);
300300

301-
$classBuilder->addExtend(Input::class);
301+
$classBuilder->setExtends(Input::class);
302302
$classBuilder->addUse(Input::class);
303303

304304
return $className;

src/Generator/PhpGenerator/ClassBuilder.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@ public function setExtends($names): self
6565
return $this;
6666
}
6767

68-
public function addExtend(string $name): self
69-
{
70-
$this->class->addExtend($name);
71-
72-
return $this;
73-
}
74-
7568
public function addImplement(string $name): self
7669
{
7770
$this->class->addImplement($name);
@@ -96,6 +89,10 @@ public function addConstant(string $name, $value): Constant
9689

9790
public function addMethod(string $name): Method
9891
{
92+
if ($this->class->hasMethod($name)) {
93+
$this->class->removeMethod($name);
94+
}
95+
9996
return $this->class->addMethod($name);
10097
}
10198

src/Generator/PhpGenerator/ClassFactory.php

Lines changed: 14 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@
55
namespace AsyncAws\CodeGenerator\Generator\PhpGenerator;
66

77
use Nette\PhpGenerator\ClassType;
8-
use Nette\PhpGenerator\Closure;
9-
use Nette\PhpGenerator\GlobalFunction;
10-
use Nette\PhpGenerator\Helpers;
11-
use Nette\PhpGenerator\Literal;
12-
use Nette\PhpGenerator\Method;
13-
use Nette\PhpGenerator\Parameter;
8+
use Nette\PhpGenerator\Factory;
149
use Nette\PhpGenerator\PhpNamespace;
15-
use Nette\PhpGenerator\Property;
1610

1711
/**
1812
* Generate Nette PhpNamespace from existing source class.
@@ -25,11 +19,24 @@
2519
*/
2620
class ClassFactory
2721
{
22+
/**
23+
* @var Factory
24+
*/
25+
private $factory;
26+
27+
public function __construct()
28+
{
29+
$this->factory = new Factory();
30+
}
31+
2832
/**
2933
* @param \ReflectionClass<object> $from
3034
*/
3135
public function fromClassReflection(\ReflectionClass $from): PhpNamespace
3236
{
37+
/** @var ClassType $class */
38+
$class = $this->factory->fromClassReflection($from, true);
39+
3340
$namespace = new PhpNamespace($from->getNamespaceName());
3441
$filename = $from->getFileName();
3542
$rows = file($filename);
@@ -46,136 +53,8 @@ public function fromClassReflection(\ReflectionClass $from): PhpNamespace
4653
}
4754
}
4855

49-
$class = $from->isAnonymous()
50-
? new ClassType()
51-
: new ClassType($from->getShortName(), $namespace);
52-
5356
$namespace->add($class);
54-
$class->setType($from->isInterface() ? $class::TYPE_INTERFACE : ($from->isTrait() ? $class::TYPE_TRAIT : $class::TYPE_CLASS));
55-
$class->setFinal($from->isFinal() && $class->isClass());
56-
$class->setAbstract($from->isAbstract() && $class->isClass());
57-
58-
$ifaces = $from->getInterfaceNames();
59-
foreach ($ifaces as $iface) {
60-
$ifaces = array_filter($ifaces, function (string $item) use ($iface): bool {
61-
return !is_subclass_of($iface, $item);
62-
});
63-
}
64-
$class->setImplements($ifaces);
65-
66-
$class->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
67-
if ($from->getParentClass()) {
68-
$class->setExtends($from->getParentClass()->getName());
69-
$class->setImplements(array_diff($class->getImplements(), $from->getParentClass()->getInterfaceNames()));
70-
}
71-
$props = $methods = [];
72-
foreach ($from->getProperties() as $prop) {
73-
if ($prop->isDefault() && $prop->getDeclaringClass()->getName() === $from->getName()) {
74-
$props[] = $this->fromPropertyReflection($prop);
75-
}
76-
}
77-
$class->setProperties($props);
78-
foreach ($from->getMethods() as $method) {
79-
if ($method->getDeclaringClass()->getName() === $from->getName()) {
80-
$methods[] = $this->fromMethodReflection($method);
81-
}
82-
}
83-
$class->setMethods($methods);
84-
$class->setConstants($from->getConstants());
8557

8658
return $namespace;
8759
}
88-
89-
public function fromMethodReflection(\ReflectionMethod $from): Method
90-
{
91-
$method = new Method($from->getName());
92-
$method->setParameters(array_map([$this, 'fromParameterReflection'], $from->getParameters()));
93-
$method->setStatic($from->isStatic());
94-
$isInterface = $from->getDeclaringClass()->isInterface();
95-
$method->setVisibility(
96-
$from->isPrivate()
97-
? ClassType::VISIBILITY_PRIVATE
98-
: ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ($isInterface ? null : ClassType::VISIBILITY_PUBLIC))
99-
);
100-
$method->setFinal($from->isFinal());
101-
$method->setAbstract($from->isAbstract() && !$isInterface);
102-
103-
$filename = $from->getFileName();
104-
$rows = file($filename);
105-
$body = implode('', array_map(function ($r) {
106-
return trim($r, ' ');
107-
}, \array_slice($rows, $from->getStartLine() + 1, $from->getEndLine() - $from->getStartLine() - 2)));
108-
109-
$method->setBody($from->isAbstract() ? null : print_r($body, true));
110-
$method->setReturnReference($from->returnsReference());
111-
$method->setVariadic($from->isVariadic());
112-
$method->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
113-
if ($from->hasReturnType() && ($type = $from->getReturnType()) instanceof \ReflectionNamedType) {
114-
$method->setReturnType($type->getName());
115-
$method->setReturnNullable($type->allowsNull());
116-
}
117-
118-
return $method;
119-
}
120-
121-
/**
122-
* @return GlobalFunction|Closure
123-
*/
124-
public function fromFunctionReflection(\ReflectionFunction $from)
125-
{
126-
$function = $from->isClosure() ? new Closure() : new GlobalFunction($from->getName());
127-
$function->setParameters(array_map([$this, 'fromParameterReflection'], $from->getParameters()));
128-
$function->setReturnReference($from->returnsReference());
129-
$function->setVariadic($from->isVariadic());
130-
if (!$from->isClosure()) {
131-
$function->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
132-
}
133-
if ($from->hasReturnType() && ($type = $from->getReturnType()) instanceof \ReflectionNamedType) {
134-
$function->setReturnType($type->getName());
135-
$function->setReturnNullable($type->allowsNull());
136-
}
137-
138-
return $function;
139-
}
140-
141-
public function fromParameterReflection(\ReflectionParameter $from): Parameter
142-
{
143-
$param = new Parameter($from->getName());
144-
$param->setReference($from->isPassedByReference());
145-
if ($from->hasType() && ($type = $from->getType()) instanceof \ReflectionNamedType) {
146-
$param->setType($type->getName());
147-
$param->setNullable($type->allowsNull());
148-
}
149-
if ($from->isDefaultValueAvailable()) {
150-
$param->setDefaultValue($from->isDefaultValueConstant()
151-
? new Literal($from->getDefaultValueConstantName())
152-
: $from->getDefaultValue());
153-
$param->setNullable($param->isNullable() && null !== $param->getDefaultValue());
154-
}
155-
156-
return $param;
157-
}
158-
159-
public function fromPropertyReflection(\ReflectionProperty $from): Property
160-
{
161-
$defaults = $from->getDeclaringClass()->getDefaultProperties();
162-
$prop = new Property($from->getName());
163-
if (isset($defaults[$prop->getName()])) {
164-
$prop->setValue($defaults[$prop->getName()]);
165-
}
166-
$prop->setStatic($from->isStatic());
167-
$prop->setVisibility(
168-
$from->isPrivate()
169-
? ClassType::VISIBILITY_PRIVATE
170-
: ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC)
171-
);
172-
if (\PHP_VERSION_ID >= 70400 && ($type = $from->getType()) instanceof \ReflectionNamedType) {
173-
$prop->setType($type->getName());
174-
$prop->setNullable($type->allowsNull());
175-
$prop->setInitialized(\array_key_exists($prop->getName(), $defaults));
176-
}
177-
$prop->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
178-
179-
return $prop;
180-
}
18160
}

src/Generator/ResultGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private function generateResultClass(StructureShape $shape, Operation $operation
8181
}
8282

8383
$classBuilder->addUse(Result::class);
84-
$classBuilder->addExtend(Result::class);
84+
$classBuilder->setExtends(Result::class);
8585
$classBuilder->addUse(ResponseInterface::class);
8686
$classBuilder->addUse(HttpClientInterface::class);
8787

src/Generator/TestGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ private function createTestClass(ClassName $testClassName, ClassName $sourceClas
385385
$classBuilder->addUse($sourceClassName->getFqdn());
386386
$classBuilder->addUse(TestCase::class);
387387

388-
$classBuilder->addExtend(TestCase::class);
388+
$classBuilder->setExtends(TestCase::class);
389389

390390
return $classBuilder;
391391
}
@@ -399,7 +399,7 @@ private function createClientTestClass(ClassName $testClassName, Operation $oper
399399
$classBuilder->addUse($clientClassName->getFqdn());
400400
$classBuilder->addUse(NullProvider::class);
401401

402-
$classBuilder->addExtend(TestCase::class);
402+
$classBuilder->setExtends(TestCase::class);
403403
$classBuilder->addMethod('getClient')
404404
->setVisibility(ClassType::VISIBILITY_PRIVATE)
405405
->setReturnType($clientClassName->getFqdn())

src/Generator/WaiterGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private function generateWaiterResult(Waiter $waiter): ClassName
158158
$classBuilder->addUse($clientClass->getFqdn());
159159
$classBuilder->addUse(InvalidArgument::class);
160160

161-
$classBuilder->addExtend(WaiterResult::class);
161+
$classBuilder->setExtends(WaiterResult::class);
162162

163163
$classBuilder->addMethod('refreshState')
164164
->setReturnType(WaiterResult::class)

0 commit comments

Comments
 (0)